Apama 10.15.3 | Developing Apama Applications | Developing Apama Applications in EPL | Using Functional Operations in EPL | Generators
 
Generators
The Functional EPL Library provides a concept of generators. Generators are objects which lazily calculate an infinite list, returning one value at a time. For example, the Fn.count generator counts upwards yielding the numbers 1, 2, 3, and so on forever.
The generators returned by the EPL Functional Library are represented in the API by an event called Generator. It is also possible to use your own events as a generator, they just need an action with signature generate() returns TYPE that returns the next value.
The simplest form of a generator is a current value (sometimes known as an accumulator) and a functor action which takes the previous value and calculates the next value. To get the next value, the generate() action is called, which steps the generator and returns the next value. You can use most of the functional operators described in Functional operators with the output of a generator. This results in a new generator that lazily evaluates the function when each value is requested. To create a generator from an existing action, you can use the generator static function on Fn:
Generator g := Fn.generator(Fn.increment);
print g.generate().toString(); // returns 1
print g.generate().toString(); // returns 2
g := <Generator> Fn.filter(g, Fn.even);
print g.generate().toString(); // returns 4
print g.generate().toString(); // returns 6
There are also several static functions which create predefined generators on Fn. For example:
Generator g := Fn.count(); // increments from 0
Generator g := Fn.repeat("A"); // an infinite series of "A"
Each of these static functions also exists as a static function on Functional which returns a Functional object which can have operators called on them fluently:
integer evenSum := <integer> Functional.count().filter(Fn.even).sliceTo(10).reduce(Fn.sum); // sum of the first 10 even numbers
The following table lists the generator functions on Fn and Functional:
Generator
Arguments
Returns
generator
action<TYPE> returns TYPE
Returns the result of calling the functor action on the previous value, starting from a default-initialized TYPE.
generatorFrom
Initial value.
action<TYPE> returns TYPE
Returns the result of calling the functor action on the previous value, starting from the initial value.
count
None.
A sequence of increasing integers, counting upwards from 1.
repeat
A value.
The given value repeated infinitely.
random
An integer, float or decimal.
Random values of the given type, between 0 and the given value.
cycle
A sequence.
Generates each value in the sequence in turn, going back to the first element after completing the sequence.
range
Start integer.
End integer (End>start).
Number to skip each time (1+).
Returns a finite sequence, not a generator, of the numbers in the given range.
sequenceOf
A value.
The number of times to repeat the value.
Returns a finite sequence, not a generator, containing the given value a given number of times.