com.apamax.functional
Event Fn


Provides functional operations on EPL containers, such as map, reduce, filter and slice.

Also provides generators which can be used as input to the functional operations.

eg: Fn.slice(Fn.map(Fn.filter(Fn.count(), (integer i)->i%2=0), (integer i)->i*2), 2, 8, 2);
Listens:
any - 

Action summary
 booleanstatic _all(any seq)

Logical 'AND' over a sequence of booleans
 booleanstatic _any(any seq)

Logical 'OR' over a sequence of booleans
 action<any> returns booleanstatic _not(any pred)

A predicate which inverts the value of another predicate.
 com.apamax.functional.Generatorstatic accumulate(any container, any func)

Returns a generator which accumulates over the underlying container, returning the result after accumulating each item. Similar to reduce, but returns a generator for each item in turn, rather than just the final total. The initial value of the accumulator is the default value of func's return type (TYPEB).
 com.apamax.functional.Generatorstatic accumulateFrom(any start, any container, any func)

Returns a generator which accumulates over the underlying container, returning the result after accumulating each item. Similar to reduce, but returns a generator for each item in turn, rather than just the final total.
 anystatic argmap(any container, any func)

Iterates through a sequence, or a generator and uses the result as a list of arguments with which to invoke a function. These inner sequences will be treated as a list of arguments to the given function, or a single argument to that function. Each generated item will be the result of calling func with the next set of arguments.
 anystatic callAction(string actionName, sequence<any> args, any value)

Functor to call the named action on the value.
 stringstatic concat(string acc, string i)

Action which concatenates strings using accumulate or reduce.
 anystatic consume(any generator, integer n)

Consume a given number of items from a generator and discard them.
 com.apamax.functional.Generatorstatic count()

Returns a generator producing sequential integers with the first one being 0.
 com.apamax.functional.Generatorstatic cycle(any seq)

Iterates through a sequence of items and continues looping forever.
 booleanstatic even(integer i)

A predicate which returns true if the argument is even.
 integerstatic filter(any container, any func)

Takes either a sequence, a dictionary or a generator and filters the contents using a boolean predicate func.
 com.apamax.functional.Generatorstatic generator(any func)

Create a generator from a function, which is passed the previous value and should return the next value. The initial value will be a default-initialized TYPE and the result of first generation will be the result of invoking func on that default-initialized value. To specify the initial value, use generateFrom.
 com.apamax.functional.Generatorstatic generatorFrom(any start, any func)

Create a generator from a function, which is passed the previous value and should return the next value.
 anystatic getAllEvents(string valueEventName, dictionary<string, any> valueEventFields, string endEventName, dictionary<string, any> endEventFields, float timeout, any onComplete, any onTimeout)

Listens for all of the given events and then calls the given function once a second event arrives.
 integerstatic increment(integer i)

Increments the given integer
 booleanstatic istrue(boolean b)

A predicate which returns true if the argument is true.
 sequence<listener>static listenForAnyOf(any seq, string typeName, string fieldName, dictionary<string, any> otherFields, any arrivedAction)

Takes a sequence of values and starts a listener one for each of them, with an action to call if any of those events arrive.
 integerstatic map(any container, any func)

Takes either a sequence, a dictionary or a generator and maps the contents using a function.
 anystatic mul(any acc, any i)

Action for multiplying together the values in a sequence or generator of integer, float or decimal using accumulate or reduce.
 booleanstatic negative(any i)

A predicate which returns true if the argument is negative.
 booleanstatic odd(integer i)

A predicate which returns true if the argument is odd.
 com.apamax.functional.Partialstatic partial(any func, any args)

Partially execute a function, providing some arguments, to be later executed with the remaining arguments. Arguments must be provided left-to-right for the function call to be invoked. eg: Partial p := Fn.partial(concatFn, "Hello"); <string>p.exec(" world"); Partial objects can be passed to any of the other actions on Fn where an action would be expected.
 booleanstatic positive(any i)

A predicate which returns true if the argument is positive (not including 0).
 integerstatic quantify(any container, any pred)

Counts how many times a predicate is true for the values of a sequence or dictionary.
 sequence<integer>static range(integer start, integer end, integer stride)

Returns a range of integers from start (inclusive) to end (exclusive), incrementing stride each time.
 anystatic reduce(any container, any func)

Takes either a sequence or a dictionary and accumulates all of the values using an accumulator function, returning the final result.
 anystatic reduceFrom(any accumulator, any container, any func)

Takes either a sequence or a dictionary and accumulates all of the values using an accumulator function, returning the final result.
 com.apamax.functional.Generatorstatic repeat(any item)

Repeats the given element forever.
 anystatic sequenceOf(any item, integer n)

Returns a sequence containing the given item the given number of times.
 anystatic slice(any container, integer start, integer end, integer stride)

Takes either a sequence or a generator and returns a given sub-range as a finite sequence. If it is passed a generator, it will invoke the generator sufficient times immediately to satisfy the slice.
 anystatic sum(any acc, any i)

Action for adding up a sequence or generator of integer, float or decimal using accumulate or reduce.
 anystatic waitForAllCompleted(any seq, string typeName, string fieldName, float timeout, any onCompleted, any onTimeout)

Wait for completion of several asynchronous tasks.
 booleanstatic whole(any i)

A predicate which returns true if the argument is a whole number.
 
Action detail

_all

            boolean static _all(any seq)
        
Logical 'AND' over a sequence of booleans
Parameters:
seq - sequence of booleans. Return true iff none of the items in the sequence are false. If seq is empty, returns true.

_any

            boolean static _any(any seq)
        
Logical 'OR' over a sequence of booleans
Parameters:
seq - sequence of booleans. Return false iff none of the items in the sequence are true. If seq is empty, returns false.

_not

            action<any> returns boolean static _not(any pred)
        
A predicate which inverts the value of another predicate.
Parameters:
pred

accumulate

            com.apamax.functional.Generator static accumulate(any container, any func)
        
Returns a generator which accumulates over the underlying container, returning the result after accumulating each item. Similar to reduce, but returns a generator for each item in turn, rather than just the final total. The initial value of the accumulator is the default value of func's return type (TYPEB).
Parameters:
container - The sequence, dictionary or generator to accumulate.
func - An action<TYPEB,TYPEA> returns TYPEB action or lambda which accumulates each item in turn and returns the new total.
Returns:
A generator which returns each accumulated item in turn.
See Also:
reduce - 

accumulateFrom

            com.apamax.functional.Generator static accumulateFrom(any start, any container, any func)
        
Returns a generator which accumulates over the underlying container, returning the result after accumulating each item. Similar to reduce, but returns a generator for each item in turn, rather than just the final total.
Parameters:
start - The initial value of the accumulator; must have the same type as func's return type (TYPEB).
container - The sequence, dictionary or generator to accumulate.
func - An action<TYPEB,TYPEA> returns TYPEB action or lambda which accumulates each item in turn and returns the new total.
Returns:
A generator which returns each accumulated item in turn.
See Also:
reduceFrom - 

argmap

            any static argmap(any container, any func)
        
Iterates through a sequence, or a generator and uses the result as a list of arguments with which to invoke a function. These inner sequences will be treated as a list of arguments to the given function, or a single argument to that function. Each generated item will be the result of calling func with the next set of arguments.
Parameters:
container - A sequence or a generator.
func - A function which takes each item in container and returns a result.

callAction

            any static callAction(string actionName, sequence<any> args, any value)
        
Functor to call the named action on the value.
Parameters:
actionName - The name of the action to call.
args - The arguments to call the action with.
value - The object to call the action on.

concat

            string static concat(string acc, string i)
        
Action which concatenates strings using accumulate or reduce.
Parameters:
acc
i

consume

            any static consume(any generator, integer n)
        
Consume a given number of items from a generator and discard them.
Parameters:
generator - The generator to step.
n - The number of times to step it.
Returns:
a reference to the original generator, stepped n times.

count

            com.apamax.functional.Generator static count()
        
Returns a generator producing sequential integers with the first one being 0.

cycle

            com.apamax.functional.Generator static cycle(any seq)
        
Iterates through a sequence of items and continues looping forever.
Parameters:
seq - The sequence of values to iterate over
Returns:
A generator which iterates over seq

even

            boolean static even(integer i)
        
A predicate which returns true if the argument is even.
Parameters:
i

filter

            integer static filter(any container, any func)
        
Takes either a sequence, a dictionary or a generator and filters the contents using a boolean predicate func.

eg: Fn.filter(numbers, (integer i)->i%2=0)

Specifically, given:
Parameters:
container - A sequence, dictionary or generator.
func - A boolean predicate function or lambda.
Returns:
The container filtered using the predicate function.

generator

            com.apamax.functional.Generator static generator(any func)
        
Create a generator from a function, which is passed the previous value and should return the next value. The initial value will be a default-initialized TYPE and the result of first generation will be the result of invoking func on that default-initialized value. To specify the initial value, use generateFrom.

eg: Fn.generator((integer i)->i+1)
Parameters:
func - An action<TYPE> returns TYPE action or lambda which takes the previous value and returns the next value.
Returns:
A generator for TYPE.

generatorFrom

            com.apamax.functional.Generator static generatorFrom(any start, any func)
        
Create a generator from a function, which is passed the previous value and should return the next value.

eg: Fn.generatorFrom(100, (integer i)->i+1)
Parameters:
start - The value to pass to func to generate the first generation.
func - An action<TYPE> returns TYPE action or lambda which takes the previous value and returns the next value.
Returns:
A generator for TYPE.

getAllEvents

            any static getAllEvents(string valueEventName, dictionary<string, any> valueEventFields, string endEventName, dictionary<string, any> endEventFields, float timeout, any onComplete, any onTimeout)
        
Listens for all of the given events and then calls the given function once a second event arrives.

Equivalent to writing:

sequence; on all ValueEventName(fields=valueEventFields) as t1 and not (EndEventName(fields=endEventFields) or wait(timeout) { seq.append(t1); } on EndEventName(fields=endEventFields) { onComplete(seq); }
Parameters:
valueEventName - The name of the value event type.
valueEventFields - The fields in valueEventName to filter on.
endEventName - The event which signals all of the valueEventNames have been received.
endEventFields
timeout - The maximum time to wait for endEventName.
onComplete
onTimeout - an action> to be called with all of the valueEventNames if the timeout occurs before endEventName has been received.
Listens:
any - 

increment

            integer static increment(integer i)
        
Increments the given integer
Parameters:
i

istrue

            boolean static istrue(boolean b)
        
A predicate which returns true if the argument is true.
Parameters:
b

listenForAnyOf

            sequence<listener> static listenForAnyOf(any seq, string typeName, string fieldName, dictionary<string, any> otherFields, any arrivedAction)
        
Takes a sequence of values and starts a listener one for each of them, with an action to call if any of those events arrive.

Equivalent to writing:

on all TypeName(fieldName=seq[0], otherfields=...) or TypeName(fieldName=seq[1], otherfields=...) or ... { arrivedAction(t); }
Parameters:
seq - sequence of different values to listen to.
typeName - The event type to listen for.
fieldName - The name of the field for the values.
otherFields - Names and values of other fields which will be the same for each listener.
arrivedAction - The action to call if any matching events arrive.

map

            integer static map(any container, any func)
        
Takes either a sequence, a dictionary or a generator and maps the contents using a function.

eg: Fn.map(numbers, (integer i)->i*2)

Specifically, given:
Parameters:
container - A sequence, dictionary or generator.
func - A function or lambda from old value type to new value type.
Returns:
The container with all the values run through func.

mul

            any static mul(any acc, any i)
        
Action for multiplying together the values in a sequence or generator of integer, float or decimal using accumulate or reduce.

eg: integer product := Fn.reduce(numbers, Fn.mul);
Parameters:
acc
i

negative

            boolean static negative(any i)
        
A predicate which returns true if the argument is negative.
Parameters:
i

odd

            boolean static odd(integer i)
        
A predicate which returns true if the argument is odd.
Parameters:
i

partial

            com.apamax.functional.Partial static partial(any func, any args)
        
Partially execute a function, providing some arguments, to be later executed with the remaining arguments. Arguments must be provided left-to-right for the function call to be invoked. eg: Partial p := Fn.partial(concatFn, "Hello"); <string>p.exec(" world"); Partial objects can be passed to any of the other actions on Fn where an action would be expected.
Parameters:
func - A function (including an earlier partial()) to execute once all arguments are provided.
args - The arguments to store for later execution. May be a single argument, or a sequence of several arguments.

positive

            boolean static positive(any i)
        
A predicate which returns true if the argument is positive (not including 0).
Parameters:
i

quantify

            integer static quantify(any container, any pred)
        
Counts how many times a predicate is true for the values of a sequence or dictionary.

eg: Fn.quantify(numbers, Fn.even)
Parameters:
container - A sequence or dictionary.
pred - An action returns boolean.
Returns:
The number of values in the container for which the predicate returns true.

range

            sequence<integer> static range(integer start, integer end, integer stride)
        
Returns a range of integers from start (inclusive) to end (exclusive), incrementing stride each time.
Parameters:
start
end
stride

reduce

            any static reduce(any container, any func)
        
Takes either a sequence or a dictionary and accumulates all of the values using an accumulator function, returning the final result.

eg: Fn.reduce(numbers, (integer a, integer i)->a+i)

Specifically, given:
Parameters:
container - A sequence or dictionary.
func - An accumulator function or lambda.
Returns:
The final result of the accumulator function.

reduceFrom

            any static reduceFrom(any accumulator, any container, any func)
        
Takes either a sequence or a dictionary and accumulates all of the values using an accumulator function, returning the final result.

eg: Fn.reduceFrom(42, numbers, (integer a, integer i)->a+i)

Specifically, given:
Parameters:
accumulator - initial value, of TYPEB
container - A sequence or dictionary.
func - An accumulator function or lambda.
Returns:
The final result of the accumulator function.

repeat

            com.apamax.functional.Generator static repeat(any item)
        
Repeats the given element forever.
Parameters:
item - The item to return.
Returns:
A generator which returns item over and over again.

sequenceOf

            any static sequenceOf(any item, integer n)
        
Returns a sequence containing the given item the given number of times.
Parameters:
item - The element to put in the sequence.
n - The number of times to repeat the element.
Returns:
A sequence<ITEMTYPE> with n items in it.

slice

            any static slice(any container, integer start, integer end, integer stride)
        
Takes either a sequence or a generator and returns a given sub-range as a finite sequence. If it is passed a generator, it will invoke the generator sufficient times immediately to satisfy the slice.

eg: Fn.slice(numbers, 5, 20, 2)
Parameters:
container - A sequence<TYPE> or generator which produces TYPE.
start - The first item to return.
end - The last item to return (-1 = all, only permitted for sequences).
stride - The number of items to increment each time (1 = all).
Returns:
A sequence<TYPE> containing the selected values from container

sum

            any static sum(any acc, any i)
        
Action for adding up a sequence or generator of integer, float or decimal using accumulate or reduce.

eg: integer sum := Fn.reduce(numbers, Fn.sum);
Parameters:
acc
i

waitForAllCompleted

            any static waitForAllCompleted(any seq, string typeName, string fieldName, float timeout, any onCompleted, any onTimeout)
        
Wait for completion of several asynchronous tasks.

Equivalent to writing:

on TypeName(fieldName=seq[0]) and TypeName(fieldName=seq[1]) and ... and not wait(timeout) { onCompleted(); }
Parameters:
seq - sequence of values (typically IDs) to wait for.
typeName - The type of event to wait for.
fieldName - The field which will contain the values.
timeout
onCompleted - an action<> to be called if all the events arrive within the timeout.
onTimeout - an action> to be called if some of the events don't arrive within the timeout. The argument will be all the values which did not arrive.
Listens:
any - 

whole

            boolean static whole(any i)
        
A predicate which returns true if the argument is a whole number.
Parameters:
i