Changeset 30552 in project


Ignore:
Timestamp:
03/10/14 20:51:38 (7 years ago)
Author:
certainty
Message:

data-generators: added json generator as example

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/data-generators

    r30551 r30552  
    2525=== Examples
    2626<enscript highlight=scheme>
    27 (use data-generators)
    28 
    29 ;; create generator for a random fixnum
    30 (gen-fixnum)
    31 
    32 ;; make generator that generates a number n with gen-current-fixnum-min <= n <= 10
    33 (gen-fixnum 10)
    34 
    35 ;; make genenator that generates a number n with 1 <= n <= 10
    36 (gen-fixnum 1 10)
    37 
    38 
    39 ;; make generator that generates a random real
    40 (gen-real)
    41 
    42 ;; bake generator that generates a random character
    43 (gen-char)
    44 
    45 ;; make generator that generates a random character out of the given charset
    46 (gen-char char-set:digit)
    47 
    48 ;; make generator that generates a random character in the given range
    49 (gen-char #\a #\z)
    50 
    51 ;; == combinators ==
    52 
    53 ;; make a generator that generates a tuple where each member is generated by the given generator
    54 (gen-tuple-of (gen-fixnum 10) (gen-real))
    55 
    56 ;; make a generator that generates a list where each element is generated by the given generator
    57 (gen-list-of (gen-fixnum))
    58 
    59 ;; make a generator that generates a vector where each element is generated by the given generator
    60 (gen-vector-of (gen-real))
    61 
    62 ;; make generator that generates a list that has exactly 3 elements
    63 (gen-list-of (gen-fixnum) 3)
    64 (with-size 3 (gen-list-of (gen-fixnum)))
    65 
    66 ;; make a generator that generates a list that has 2 to 10 elements
    67 (with-size (2 . 10) (gen-list-of gen-fixnum))
    68 
    69 ;; make a generator that generates a list of fixnums between 0 and 10 which has 2 to 10
    70 elements
    71 (with-size (2 . 10) (gen-list-of (gen-fixnum 0 10)))
    72 
    73 ;; retrieve a value from the generator
    74 (<- (gen-fixnum))
    75 
    76 ;; retrieve n values from the generator
    77 (<-* 2 (gen-fixnum))
    78 
    79 ;; invoke the generator n times passing each value to proc
    80 (gen-for-each 3 (lambda (n) (+ n 3)) (gen-fixnum))
    81 
     27
     28(use json data-generators)
     29
     30;; generate a json document
     31(define (gen-json-doc #!optional (nesting 5))
     32  (generator
     33   (with-output-to-string
     34     (lambda ()
     35       (json-write (<- (gen-json-value nesting)))))))
     36
     37(define (gen-json-value nesting)
     38  (generator
     39   (if (positive? nesting)
     40       (<- (gen-sample-of (gen-json-string) (gen-json-array nesting) (gen-json-null) (gen-json-number) (gen-json-object nesting)))
     41       (<- (gen-sample-of (gen-json-string) (gen-json-null) (gen-json-number))))))
     42
     43(define (gen-json-string)
     44  (with-size (0 . 20)
     45    (gen-string-of (gen-char char-set:letter+digit))))
     46
     47(define (gen-json-object nesting)
     48  (with-size (0 . 5)
     49    (gen-vector-of (gen-pair-of (gen-json-string) (gen-json-value (sub1 nesting))))))
     50
     51(define (gen-json-null)
     52  (gen-constant (void)))
     53
     54(define (gen-json-number)
     55  (gen-sample-of (gen-fixnum) (gen-real)))
     56
     57(define (gen-json-array nesting)
     58  (with-size (0 . 5)
     59    (gen-list-of (gen-json-value (sub1 nesting)))))
     60
     61
     62(gen-for-each 10 print (gen-json-doc))
    8263</enscript>
    8364
Note: See TracChangeset for help on using the changeset viewer.