Namespace Popcorn.Core


namespace Popcorn.Core
'Core' namespace defines the basic generators for JavaScript primitive types and objects.
The generator library is build on top of the monadic combinators 'return' and 'bind' called 'gen' and 'chain'. However for better performance most internal generators have imperative implementations.
Authors:
Adam Smyczek

Class Summary
Extensible library class for random generators.

Object Summary
Random generator object.

Function Summary
generator array(function g)
Group generator results into array
any cConcat(any r, any n)
'cConcat' combinator function concatenates the results of generators executed in sequence into one array.
any cJoin(any r, any n)
'cJoin' combinator function joins the results of generators executed in sequence into one string.
generator chain(generator g, function f)
generator chain(generator[] gs, function f)
Use 'chain' to execute generators in sequence.
object[] circulate(object gen, object base, int count, object state, function result_transformer)
Same as 'permutate', 'circulate' executes the argument generator 'gen' on the default object 'base' and returns the resulting object array.
In difference to 'permutate', the results of all attribute generators are combined together.
generator current(function f)
'current' provides access to current generated object.
generator gen(any inp)
'gen' creates a generator that returns the argument value 'v' when executed on any input object.
any id(any value)
Identity
Lazy generator evaluation wrapper used for example by random generators.
generator[] mapGen(any[] vs)
'mapGen' is a helper function that converts all elements of an array to generators.
'mutate' executes generators 'gs' on the input object o.
'mutateOnAttribute' executes generators 'gs' on an object property of name 'name'.
object[] permutate(object gen, object base, int count, object state, function result_transformer)
'permutate' is one of two supported JSON object generation functions.
generator property(string name)
'property' applies generator 'g' on an object property of name 'name' and returns the resulting object.
random random(integer seed)
Creates a linear congruential random generator object using an optional 'seed' value.
any[] repeat(integer n, any v)
Repeats value 'v' 'n'-times and returns the resulting array.
'replicate' executes generator 'g' 'n'-times and combines the results using combinator function 'f'.
generator seq(generator[] gs, combinator f, any init)
'seq' executes the generators 'gs' and combines the result using combinator function 'f'.
generator setVar(string name, generator g)
'setVar' stores the result of one generator that can be used as input to another generator in same generation process.
generator[] toGenArray(value/generator/[generator] value)
Wrappes a value into generator array, the default input for 'mutate'.
Executes generators 'gs' on an input object and returns the updated object.
generator varGen(string name)
Creates a generator that returns the value of the variable 'name' when executed.
generator withState(function f)
'withState' provides access to the state object.
generator withVar(string name, function f)
'withVar' provides access to variables stored using setVar generator.

Function Details

function array

generator array(function g)
Group generator results into array
Parameters:
g - the generator to wrap
Defined in Popcorn.Core

function cConcat

any cConcat(any r, any n)
'cConcat' combinator function concatenates the results of generators executed in sequence into one array.
For example:
   seq([gen(1), gen(2)], cConcat)() => [1,2]
 
'cConcat' can be used in combination with and {@link replicate.
Parameters:
r - combined result of previous executions.
n - result of current generator evaluation.
Defined in Popcorn.Core

function cJoin

any cJoin(any r, any n)
'cJoin' combinator function joins the results of generators executed in sequence into one string.
For example:
   seq([gen(1), gen(2)], cJoin)() => '12'
 
'cJoin' can be used in combination with and {@link replicate.
Parameters:
r - combined result of previous executions.
n - result of current generator evaluation.
Defined in Popcorn.Core

function chain

generator chain(generator g, function f)
generator chain(generator[] gs, function f)
Use 'chain' to execute generators in sequence. 'chain' executes the argument generator or generator array 'g' and passes the result to the parameter function 'f'. 'f' may evaluate the result but always has to return a new generator. 'chain' itself is a generator that can be evaluated or passed to another chain or other generator function.
For example:
   var g = chain(gen(1), function(r) { return gen(r + 1); });
   g('any input') will evaluate to 2.
 
Parameters:
g - a generator
f - a function that takes the result of the generator 'g' and returns a new generator as result.
gs - generator array
f - a function that takes the array result of all generators 'gs' and returns a new generator as result.
Returns:
the 'chain' generator.
Defined in Popcorn.Core

function circulate

object[] circulate(object gen, object base, int count, object state, function result_transformer)
Same as 'permutate', 'circulate' executes the argument generator 'gen' on the default object 'base' and returns the resulting object array.
In difference to 'permutate', the results of all attribute generators are combined together. If no count is provided, circulate generates object array of the length of the longest attribute generator. All other attribute values circulate, for example:
   var base = { name: 'Woody', age:2 }; 
   var gen  = { name: list('Buzz', 'Slinky'), age: range(2,5) }; 
   generate(gen, base) will return the array: 
    [{ name: 'Buzz',   age: 2 },
     { name: 'Slinky', age: 3 }, 
     { name: 'Buzz',   age: 4 }, 
     { name: 'Slinky', age: 5 }]
 
Parameters:
gen - the generator object.
base - the base test case object.
count - an optional result count
state - an optional state object that can be read and manipulated by any generator.
result_transformer - an optional function which takes the result generator object { result:r, state:s } as argument and returns the desired result. If not defined, the default transformer returns just the result array. To return the result and state use 'id' function.
Defined in Popcorn.Core

function current

generator current(function f)
'current' provides access to current generated object. It's experimental, use with care!!! For example:
   var o = { name: 'Woody', age:2 };
   var g = { name : list('Buzz', 'Woody'), 
             description : current(function(r) { return r.name + ' is ' + r.age; }) }
   permutate(g, o) will return 
   [{ name:'Buzz', age:2, description: 'Buzz is 2' }, 
    { name:'Woody', age:2, description: 'Woody is 2' }]
 
Parameters:
f - a callback function that takes the current generated objects as parameter and returns this attribute value.
Defined in Popcorn.Core

function gen

generator gen(any inp)
'gen' creates a generator that returns the argument value 'v' when executed on any input object. 'gen' is basically a generator constructor that wraps any value into a generator.
For example:
   var g = gen('test');
   g('any input') will return 'test'.
 
Parameters:
inp - any variable, object etc.
Returns:
a generator which executed returns the value 'v'.
Defined in Popcorn.Core

function id

any id(any value)
Identity
Parameters:
value - any value
Returns:
the value
Defined in Popcorn.Core

function lazy

generator lazy(g)
Lazy generator evaluation wrapper used for example by random generators.
For example:
   var g1 = gen(Math.random());
   var g2 = lazy(function() { return gen(Math.random()); });
   g1 will always return the same value and g2 a different one
   when executed.
 
Parameters:
g - the generator to wrap.
Defined in Popcorn.Core

function mapGen

generator[] mapGen(any[] vs)
'mapGen' is a helper function that converts all elements of an array to generators.
magGen(as) -> [gen(as[i])]
Parameters:
vs - array.
Defined in Popcorn.Core

function mutate

generator mutate(generator[] gs)
'mutate' executes generators 'gs' on the input object o. For example:
   mutate([gen('Buzz'), gen('Woody')](o) will return 
   ['Buzz', 'Woody']
 
Parameters:
gs - generator array.
Defined in Popcorn.Core

function mutateOnAttribute

generator mutateOnAttribute(string name, generator[] gs)
'mutateOnAttribute' executes generators 'gs' on an object property of name 'name'. The result is an array that contains one result object for every generator result value.
For example:
   var o = { name: 'Woody', age:2 };
   mutateOnAttribute('name', [gen('Buzz'), gen('Woody')](o) will return 
   the object array [{ name:'Buzz', age:2 }, { name:'Woody', age:2 }]
 
Parameters:
name - object property name.
gs - generator array.
Defined in Popcorn.Core

function permutate

object[] permutate(object gen, object base, int count, object state, function result_transformer)
'permutate' is one of two supported JSON object generation functions. It executes the argument generator 'gen' on the default object 'base' and returns the resulting object array.
This generation method permutates over all results of all attribute generator, for example:
   var base = { name: 'Woody', age:2 }; 
   var gen  = { name: list('Buzz', 'Slinky'), age: range(2,4) }; 
   generate(gen, base) will return the array: 
    [{ name: 'Buzz',   age: 2 },
     { name: 'Buzz',   age: 3 }, 
     { name: 'Buzz',   age: 4 }, 
     { name: 'Slinky', age: 2 }, 
     { name: 'Slinky', age: 3 }, 
     { name: 'Slinky', age: 4 }]
 
Parameters:
gen - the generator object.
base - the base test case object.
count - an optional result count
state - an optional state object that can be read and manipulated by any generator.
result_transformer - an optional function which takes the result generator object { result:r, state:s } as argument and returns the desired result. If not defined, the default transformer returns just the result array. To return the result and state use 'id' function.
Defined in Popcorn.Core

function property

generator property(string name)
'property' applies generator 'g' on an object property of name 'name' and returns the resulting object. 'property' acts as a property selector and is a generator itself that can be evaluated or passed to other generators.
For example:
   var o    = { name:'Woody', toy:'cowboy' };
   var name = property('name');
   name(gen('Buzz'))(o) will return the object { name:'Buzz', toy:'cowboy' }
 
Parameters:
name - object property name.
Defined in Popcorn.Core

function random

random random(integer seed)
Creates a linear congruential random generator object using an optional 'seed' value. If 'seed' is not defined, the default JavaScript Math.random() function is used. See Wikipedia LCR for details.
Parameters:
seed - an optional seed.
Returns:
the random generator object.
Defined in Popcorn.Core

function repeat

any[] repeat(integer n, any v)
Repeats value 'v' 'n'-times and returns the resulting array. If parameter 'v' is an array, the subsequent results are concatenated. 'repeat' is commonly used with object generators, for example:
   var gen = {
     name : repeat(3, gen('abc'))
   }
   will generate three objects { name : 'abc' } when evaluated with generate.
 
See Popcorn.Core.generate for details.
Parameters:
n - repeat count.
v - object or generator to repeat.
Defined in Popcorn.Core

function replicate

generator replicate(generator g, combinator f, any init)
'replicate' executes generator 'g' 'n'-times and combines the results using combinator function 'f'. This function is a specialization of seq.
Example:
   var overflow = replicate(gen('A'), cJoin, '');
   overflow(5) generates 'AAAAA'
 
Parameters:
g - a generator.
f - combinator function.
init - the initial value for the combinator function 'f'.
Returns:
the replicator generator
See also:
Defined in Popcorn.Core

function seq

generator seq(generator[] gs, combinator f, any init)
'seq' executes the generators 'gs' and combines the result using combinator function 'f'. Combinator 'f' is called on every step with the result of previous executions ('init' value on first call) and the result of the current generator evaluation.
For example:
   var g = seq([gen('a'), gen('b'), gen('c')], cJoin, 0);
   g('any input') will return 'abc'.
 
Parameters:
gs - generator array.
f - combinator function.
init - the initial value passed on first call to the combinator.
Returns:
the sequence generator that evaluates the generators 'gs' when executed.
Defined in Popcorn.Core

function setVar

generator setVar(string name, generator g)
'setVar' stores the result of one generator that can be used as input to another generator in same generation process.
For example:
   var g = {
     int   : setVar('my_rand', random().int()),
     plus1 : withVar('my_rand', function(i) { return gen(i + 1); })
   }
 
The random integer value generated for attribute 'int' is stored in the state object under the attribute name 'my_rand' and referenced using withVar function.
Parameters:
name - variable name the generator value is stored for.
g - the generator to execute.
Returns:
result of the generator 'g'.
Defined in Popcorn.Core

function toGenArray

generator[] toGenArray(value/generator/[generator] value)
Wrappes a value into generator array, the default input for 'mutate'.
Parameters:
value
Defined in Popcorn.Core

function update

generator update(generator[] gs)
Executes generators 'gs' on an input object and returns the updated object.
For example:
   var o    = { name: 'Woody', toy: 'cowboy' };
   var name = property('name');
   var toy  = property('toy');
   update([name(gen('Buzz')), toy(gen('action figure'))])(o) will return the object 
   { name:'Buzz', toy:'action figure' }
 
Parameters:
gs - generator array.
Defined in Popcorn.Core

function varGen

generator varGen(string name)
Creates a generator that returns the value of the variable 'name' when executed. For example:
   var g = {
     att1 : setVar('my_rand', random().int()),
     att2 : varGen('my_rand')
   }
 
will generate an object with same value for both attributes.
Parameters:
name - variable name.
Defined in Popcorn.Core

function withState

generator withState(function f)
'withState' provides access to the state object. The function 'f' takes the state object as argument and returns another generator as result. All modifications to the state object are visible to other generators. For example:
   var g = {
     int : withState(function(s) { s.init = 10; return random().int(); } ),
     add : withState(function(s) { return gen(s.init + 10); } )
   }
 
Parameters:
f - takes the state object as argument and returns a new generator as result.
Defined in Popcorn.Core

function withVar

generator withVar(string name, function f)
'withVar' provides access to variables stored using setVar generator. It takes the variable name and a function as argument. The value of the variable is passed to the function that has to return another generator as result. For an example see setVar generator.
Parameters:
name - variable name.
f - takes the variable as argument and returns a new generator as result.
Defined in Popcorn.Core