com.apamax.functional
Event Functional


Wrapper event for usage of Fn in a fluent style. Contains instance methods for operators, static methods for creation, all of which return values wrapped in a Functional for further calls. Functors and predicates are still used from Fn. The underlying value can be retrieved at the end of the chain with the get action.

eg: Functional.count().filter(Fn.even)).map((integer i)->i*2).slice(2, 8, 2);

You can create a Functional either using one of the static methods, or by wrapping a sequence, dictionary or generator directly.

eg: Functional([1,2,3,4]).filter(Fn.even).reduce(Fn.sum)
Action summary
 com.apamax.functional.Functionalaccumulate(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.
 com.apamax.functional.Functionalargmap(any func)

Use the contents as a list of arguments with which to invoke a function.
 com.apamax.functional.Functionalconsume(integer n)

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

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

Iterates through a sequence of items and continues looping forever.
 com.apamax.functional.Functionalfilter(any func)

Filters the contents using a boolean predicate func.
 anygenerate()

If this Functional is wrapping a generator, generate one item with the underlying generator and return it.
 com.apamax.functional.Functionalstatic 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.Functionalstatic generatorFrom(any start, any func)

Create a generator from a function, which is passed the previous value and should return the next value.
 anyget()

Return the wrapped container.
 voidstatic 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.
 com.apamax.functional.FunctionallistenForAnyOf(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.
 com.apamax.functional.Functionalmap(any func)

Maps the contents using a function.
 integerquantify(any pred)

Counts how many times a predicate is true for the values of a sequence or dictionary. Works on a sequence or a dictionary. The result is an actual value, not a Functional, since it's no longer a container.
 com.apamax.functional.Functionalstatic range(integer start, integer end, integer stride)

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

Accumulates all of the contents using an accumulator function, returning the final result.
 com.apamax.functional.Functionalstatic repeat(any item)

Repeats the given element forever.
 com.apamax.functional.Functionalstatic sequenceOf(any item, integer n)

Returns a sequence containing the given item the given number of times.
 com.apamax.functional.Functionalslice(integer start, integer end, integer stride)

Returns a given sub-range as a finite sequence.
 voidwaitForAllCompleted(string typeName, string fieldName, float timeout, any onCompleted, any onTimeout)

Wait for completion of several asynchronous tasks.
 
Action detail

accumulate

            com.apamax.functional.Functional accumulate(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:
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, wrapped in a Functional.
See Also:
Fn.accumulate - 

argmap

            com.apamax.functional.Functional argmap(any func)
        
Use the contents as a list of arguments with which to invoke a function.

Works on a sequence or a generator.

These inner sequences will be treated as a list of arguments to the given function, or a single argument to that function. Each item in the result will be the result of calling func with the next set of arguments.
Parameters:
func - A function which takes each item in container and returns a result.
See Also:
Fn.argmap - 

consume

            com.apamax.functional.Functional consume(integer n)
        
Consume a given number of items from the contained generator and discard them.

Only works on generators.
Parameters:
n - The number of times to step the generator.
Returns:
This Functional operator with the generator stepped n times.

count

            com.apamax.functional.Functional static count()
        
Returns a Functional generator producing sequential integers with the first one being 0.
See Also:
Fn.count - 

cycle

            com.apamax.functional.Functional 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, wrapped in a Functional
See Also:
Fn.cycle - 

filter

            com.apamax.functional.Functional filter(any func)
        
Filters the contents using a boolean predicate func.

Works on dictionary, sequence or generator.

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

Semantics are as Fn.filter
Parameters:
func - A boolean predicate function or lambda.
Returns:
A Functional container filtered using the predicate function.
See Also:
Fn.filter - 

generate

            any generate()
        
If this Functional is wrapping a generator, generate one item with the underlying generator and return it.

Only works on generators.
Returns:
The next item from the wrapped generator.
See Also:
Generator.generate - 

generator

            com.apamax.functional.Functional 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: Functional.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, wrapped in a Functional.
See Also:
Fn.generator - 

generatorFrom

            com.apamax.functional.Functional 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: Functional.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, wrapped in a functional.
See Also:
Fn.generator - 

get

            any get()
        
Return the wrapped container.

getAllEvents

            void 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.
See Also:
Fn.getAllEvents - 

listenForAnyOf

            com.apamax.functional.Functional listenForAnyOf(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:
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.
See Also:
Fn.listenForAnyOf - 

map

            com.apamax.functional.Functional map(any func)
        
Maps the contents using a function.

Works on dictionary, sequence or generator.

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

Semantics are as Fn.map
Parameters:
func - A function or lambda from old value type to new value type.
Returns:
The container with all the values run through func.
See Also:
Fn.map - 

quantify

            integer quantify(any pred)
        
Counts how many times a predicate is true for the values of a sequence or dictionary. Works on a sequence or a dictionary. The result is an actual value, not a Functional, since it's no longer a container.

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

range

            com.apamax.functional.Functional static range(integer start, integer end, integer stride)
        
Returns a Functional range of integers from start (inclusive) to end (exclusive), incrementing stride each time.
Parameters:
start
end
stride
See Also:
Fn.range - 

reduce

            any reduce(any func)
        
Accumulates all of the contents using an accumulator function, returning the final result.

Works on a sequence or a dictionary. The result is the actual value, not a Functional, since it's no longer a container.

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

Semantics are as Fn.reduce
Parameters:
func - An accumulator function or lambda.
Returns:
The final result of the accumulator function.
See Also:
Fn.reduce - 

repeat

            com.apamax.functional.Functional 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, wrapped in a Functional.
See Also:
Fn.repeat - 

sequenceOf

            com.apamax.functional.Functional 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, wrapped in a Functional.
See Also:
Fn.sequenceOf - 

slice

            com.apamax.functional.Functional slice(integer start, integer end, integer stride)
        
Returns a given sub-range as a finite sequence.

Works on sequence or generator. If it is a generator, it will invoke the generator sufficient times immediately to satisfy the slice.

eg: numbers.slice(5, 20, 2)
Parameters:
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 Functional containing the selected range of values.
See Also:
Fn.slice - 

waitForAllCompleted

            void waitForAllCompleted(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:
typeName - The type of event to wait for.
fieldName - The field which will contain the values.
timeout - The timeout to wait for all the values to arrive.
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.
See Also:
Fn.waitForAllCompleted -