Changeset 30564 in project


Ignore:
Timestamp:
03/14/14 21:24:47 (7 years ago)
Author:
certainty
Message:

data-generators: documented new API

File:
1 edited

Legend:

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

    r30559 r30564  
    2424
    2525=== Examples
     26==== data-generators
    2627<enscript highlight=scheme>
    2728(use json data-generators)
     
    7071</enscript>
    7172
     73==== data-generators-literals
     74<enscript highlight=scheme>
     75(use data-generators data-generators-literals)
     76
     77;; create a generator using the generator literal
     78(print (<- #g[0 .. 100]))
     79
     80;; create a generator using the range literal
     81(print (<- (gen-list-of (gen-fixnum #i[0 ... 100])))
     82
     83</enscript>
     84
    7285
    7386=== API
     
    98111<procedure>(gen-for-each n proc gen)</procedure>
    99112Invokes the generator '''gen''' '''n''' times and applies '''proc''' to each value in turn.
     113'''proc''' must be a procedure that takes two arguments. The first argument will be the value
     114and the second argument will be the round in which the value was generated.
    100115
    101116==== Ranges
     
    146161Generator that always returns '''value'''.
    147162
     163<procedure>(fixnums ...)</procedure>
     164Alias for '''gen-fixnum'''.
     165
    148166<procedure>(gen-fixnum)</procedure>
    149167Generator for a fixnum between '''gen-current-fixnum-min''' and
    150168'''gen-current-fixnum-max'''.
    151169
    152 <procedure>(gen-fixnum upper-bound)</procedure>
    153 Generator for a fixnum between '''gen-current-fixnum-min''' '''and upper-bound'''
    154 
    155170<procedure>(gen-fixnum range)</procedure>
    156171Generator for a fixnum within the given '''range'''
     
    159174Generator for a fixnum between '''lower-bound''' and '''upper-bound'''
    160175
     176
     177<procedure>(odd-fixnums ...)</procedure>
     178Alias for '''gen-odd-fixnum'''.
     179
    161180<procedure>(gen-odd-fixnum ...)</procedure>
    162181This generator supports all the variations for '''gen-fixnum''' but it will only yield odd numbers
    163182
     183<procedure>(even-fixnums ...)</procedure>
     184Alias for '''gen-even-fixnum'''.
     185
    164186<procedure>(gen-even-fixnum ...)</procedure>
    165187This generator supports all the variations for '''gen-fixnum''' but it will only yield even numbers
     
    186208Generator for a fixnum between -9223372036854775807 and 9223372036854775807.
    187209
    188 <procedure>(gen-uint8)</procedure>
     210<procedure>(gen-uint64)</procedure>
    189211Generator for a fixnum between 0 and 18446744073709551615.
    190 
    191212
    192213<procedure>(gen-real)</procedure>
    193214Generator for a real number between 0.0 and 1.0.
    194215
    195 <procedure>(gen-real upper-bound)</procedure>
    196 Generator for a real number between 0.0 and '''upper-bound'''.
    197 
    198216<procedure>(gen-real range)</procedure>
    199217Generator for a real number in the given '''range'''.
     
    201219<procedure>(gen-real lower-bound upper-bound)</procedure>
    202220Generator for a real number between '''lower-bound''' and '''upper-bound'''.
     221
     222<procedure>(chars ...)</procedure>
     223Alias for '''gen-char'''.
    203224
    204225<procedure>(gen-char)</procedure>
     
    219240(gen-char #\a #\z)
    220241</enscript>
     242
     243
     244<procedure>(booleans)</procedure>
     245Alias for '''gen-bool'''.
    221246
    222247<procedure>(gen-bool)</procedure>
     
    244269Generator for a tuple where each element is generated by the given generators.
    245270
    246 <procedure>(gen-string-of char-gen size #!optional (size (gen-current-default-size)))</procedure>
     271<procedure>(gen-string ...)</procedure>
     272Alias for '''gen-string-of'''.
     273
     274<procedure>(gen-string-of char-gen #!optional (size (gen-current-default-size)))</procedure>
    247275Generator for a string where each character is generated from '''char-gen'''.
     276The '''size''' parameter specifies the size of the strings that are generated.
     277It is expected to be a thunk that returns the size as a fixnum. See '''with-size''' for a more convenient way to adjust this.
     278
     279<procedure>(gen-symbol ...)</procedure>
     280Alias for '''gen-symbol-of'''
     281
     282<procedure>(gen-symbol-of #!optional (char-gen (gen-char char-set:letter+digit)) (size (gen-current-default-size)))</procedure>
     283Generator for a symbol where each character is generated from '''char-gen'''.
    248284The '''size''' parameter specifies the size of the strings that are generated.
    249285It is expected to be a thunk that returns the size as a fixnum. See '''with-size''' for a more convenient way to adjust this.
     
    264300It is expected to be a thunk that returns the size as a fixnum. See '''with-size''' for a more convenient way to adjust this.
    265301
    266 <procedure>(gen-hash-table-of key-gen value-gen #!optional (size (gen-current-default-size)))</procedure>
     302<procedure>(gen-hash-table-of key-gen value-gen #!optional (size (gen-current-default-size)) (equal? eq?))</procedure>
    267303Generator for a hash-table where each key is generated by '''key-gen''' and each value is generated by '''value-gen'''.
    268 The '''size''' parameter specifies the size of the hash-table that is generated.
     304The '''size''' parameter specifies the size of the hash-table that is generated. The '''equal?''' parameter is passed directly
     305to '''make-hash-table'''.
    269306It is expected to be a thunk that returns the size as a fixnum. See '''with-size''' for a more convenient way to adjust this.
    270307
     
    274311<procedure>(gen-transform transformer gen)</procedure>
    275312Generator that retrieves values from '''gen''' and applies '''transformer''' to this value.
    276 
    277313
    278314<syntax>(with-size size-spec ...)</syntax>
     
    294330</enscript>
    295331
     332==== Reader extensions
     333Optionally you can use '''data-generators-literals''' to use reader-extensions that allow you to create ranges and generators.
     334
     335<syntax>#i[x .. y]</syntax>
     336Creates a range from x to y inclusive.
     337
     338<syntax>#i[ .. y]</syntax>
     339Creates a range from (gen-current-fixnum-min) to y inclusive.
     340
     341<syntax>#i[x .. ]</syntax>
     342Creates a range from x to (gen-current-fixnum-max) inclusive.
     343
     344<syntax>#i[x ... y]</syntax>
     345Creates a range from x to y exclusive.
     346
     347<syntax>#i[ ... y]</syntax>
     348Creates a range from (gen-current-fixnum-min) to y exclusive.
     349
     350<syntax>#i[x ... ]</syntax>
     351Creates a range from x to (gen-current-fixnum-max) exclusive.
     352
     353<syntax>#g[range-exp]</syntax>
     354Creates a generator by applying '''gen''' to the given range-expression. The range-expression is the same
     355that the #i[]-reader-macro supports. It expands to (gen range-expression).
     356
     357Example:
     358
     359<enscript highlight=scheme>
     360(use data-generators data-generators-literals)
     361
     362(<- #g[1 .. 10])
     363(<- #g[#\a ... #\z])
     364(<- #g[1.0 .. 10.3])
     365
     366</enscript>
     367
     368
     369
     370
     371
    296372
    297373=== See also
Note: See TracChangeset for help on using the changeset viewer.