Class StreamFunctionSet
java.lang.Object
com.singularsys.jep.misc.OneShotComponent
com.singularsys.jep.misc.FunctionSet
com.singularsys.extensions.stream.StreamFunctionSet
- All Implemented Interfaces:
JepComponent,Serializable
Adds stream processing functions.
Use an ! in the property value to prevent the function being added, e.g.
- Generators:
iterate- creates a stream using an initial value, a unary lambda function giving the next value and an optional predicate lambda function to determine when to end the stream.iterate(0, x=>x+1, x=>x<10) . count()oriterate(1, x=>x*2) . limit(10) . toList()
- Intermediate functions:
map- maps a stream using a lambda function:[1..5] . map(x=>x*x)filter- filters a stream using a lambda function a predicate:[1..10] . filter(x=>x%2==0)accumulate- accumulate value of sequence, returning a stream of intermediate values:[1..5] . accumulate(0,[tot,val]=>tot+val)produces[1,3,6,10,15]sort- sorts a stream by a lambda function:[3,1,4,2] . sort((a,b)=>a>b)dsort- sorts a stream by numeric value:[3,1,4,2] . dsort()rsort- sorts a stream by reverse numeric value:[3,1,4,2] . rsort()limit- limits a stream to a number of elements:[1..100] . limit(10)skip- skips a number of elements in a stream:[1..100] . skip(90)while- takes elements from a stream while a predicate is true:[1..100] . while(x=>x<10)append- concatenates two streams together:[1..5] . append([6..10])merge- merge two streams together pairwise using a lambda function to define how to merge elements:[1..5] . merge((a,b)=>a+b, [6..10])loop- loops a stream indefinitely:[1..5] . loop() . limit(12)produces[1,2,3,4,5,1,2,3,4,5,1,2]distinct- return a stream with duplicate elements removed:[1,2,3,2,1] . distinct()produces[1,2,3]order is not guaranteed.
- Terminal functions:
fold- folds a stream using a binary lambda function:[1..5]. fold((a,b)=>a+b)foldOr- folds a stream using a binary lambda function, returning a default value if the stream is empty[1..5] . foldOr((a,b)=>a+b, -1)reduce- reduces a stream using a binary lambda function and an initial value[1..5] . reduce(1, (a,b)=>a*b)first- returns the first element of a stream[1..5] . first()firstOr- returns the first element of a stream or a default value if the stream is empty[1..5] . firstOr(-1)last- returns the last element of a stream[1..5] . last()count- counts the number of elements in a stream[1..5] . count()min- finds the minimum numeric value of a stream:[1..5] . min()max- finds the maximum numeric value of a stream:[1..5] . max()toList- turns a stream into a Jep list[1..5] . toList()anyMatch- returns true if any item match a predicate[1..5] . anyMatch(x => x%2==0)allMatch- returns true if all items match a predicate[1..5] . allMatch(x => x%2==0)noneMatch- returns true if no items match a predicate[1..5] . noneMatch(x => x%2==0)firstMatchOr- finds the first matching item or a default value[1..5] . firstMatchOr(x => x%2==0, -1)
- Other functions:
apply- applies a lambda function, treating the input as a single variable, converting the input streams to lists.[1..10] . map(x=>x^2) . apply(v=>avg(v)) . apply(x=>sqrt(x))chained- applies a lambda function, treating the input as a single variable[1..5] . chained(seq => seq . map(x=>x^2)) .sum()define- defines regular Jep function using a lambda function:define(mysum, [x,y] => x+y); mysum(2,3);clone- clone a stream so multiple copies can be used within a lambda function:[1..5] . chained(seq => clone(seq) . append(clone(seq)))car- get the first element of a streamclone- get a stream with the first element removed:[3..7] . chained(seq=>[car(seq),cdr(seq)])
These functions can either be used in a chain,
e.g. [1..10] . filter(x=>x%2==0) . map(x=>x*x) . toList()
or with the stream as an argument, e.g. filter(x=>x%2==0,[1..10]) .
For most functions the preceding item in the chain
becomes the last item in the argument list. So
[1..10] .reduce(1, (a,b)=>a*b) is the same as reduce(1, (a,b)=>a*b, [1..10]) .
There are two exceptions
append-[1..5] . append([6..10])is the same asappend([1..5], [6..10])merge-[1..5] . merge((a,b)=>a+b, [6..10])is the same asmerge((a,b)=>a+b, [1..5], [6..10])
Names used can be changed in the properties file
com.singularsys.extensions.messages.properties
StreamFunctionSet.map = map
StreamFunctionSet.map = !skip
- Since:
- Jep 4.1 extensions 2.2
- See Also:
-
Field Summary
Fields inherited from class com.singularsys.jep.misc.FunctionSet
functions -
Constructor Summary
ConstructorsConstructorDescriptionCreates a set of stream enabled higher order functions.StreamFunctionSet(FieldI field) Creates a set of stream enable higher order functions for a specific field. -
Method Summary
Methods inherited from class com.singularsys.jep.misc.FunctionSet
firstInit, put, putIfSetMethods inherited from class com.singularsys.jep.misc.OneShotComponent
getLightWeightInstance, init
-
Constructor Details
-
StreamFunctionSet
public StreamFunctionSet()Creates a set of stream enabled higher order functions. The implementations usesDouble.compareTo(Double)to compare numeric values in thensortandrsortfunctions;StreamMinMaxto find the minimum and maximum numeric values in theminandmaxfunctions; andAddto sum numeric values in thesumfunction. -
StreamFunctionSet
Creates a set of stream enable higher order functions for a specific field. The implementations usesFieldComparatorin thensortandrsortfunctions;ElementMinMaxto find the minimum and maximum numeric values in theminandmaxfunctions; and theFieldI.add(Object, Object)method to sum numeric values in thesumfunction.- Parameters:
field- Field to use for numeric comparisons and sums.
-