Package com.singularsys.extensions.stream
Stream in Jep. Either in a chained function notation
[1..10] . map(x=>x^2) . fold([x,y] =>x+y) -> total
chained
package. Or in a traditions notations
total = fold([x,y]=>x+y, map(x=>x^2, [1..10]))
The individual PFMCs either return a Stream,
take a Stream as argument or both.
Using streams allows for indefinite sequences
iterate(2, n=>n+2) . limit(20)
There are three main types of function in this package:
- Generators: return a stream
- Intermediate functions: take a stream as input perform some operation on it and return another stream
- Terminal Functions: take a stream as input and return some other data type, often a number or a list.
The StreamFunctionSet contains a number of standard
stream functions including the generator iterate,
the intermediate functions map, filter and sort
and the terminal function fold.
The StreamOperatorTable defines the operators used with streams:
the chaining operator ., the lambda function definition operator =>,
the range operator .., and the right-hand assignment operator ->.
It also redfines the left hand assingment operator = and
optionally the ternary conditional operator ? : .
These are implements using a number of classes, which can also be user to add new functions.
Generators:
StreamRangeproduces a finite sequence of numbers,[1..10]StreamIterateproduces either a finite or infinite sequenceiterate(2, n=>n+2, n=>n<20)oriterate(2, n=>n+2)StreamSourcePfmcuses an external stream as source, say reading from a file or databaseFieldStreamRangeproduces a finite sequence of numbers using aFieldIto calculate the next value and determine when to end the stream.
Intermediate functions:
StreamMapperforms an operation on each element of the stream, {[1..10] . map(x => x^2)StreamExternalMapuses an external function which implementsFunction<Object, Object>or aUnaryFunctionlikeSquareRoot{[1..10] . sqrt()StreamFilterfilters elements of a stream using a lambda function[1..10] . filter(x=>x%2 ==0)StreamExternalFilterfilters elements of a stream using a function which implementsPredicate<Object>or aUnaryFunctionwhich returns a Boolean.[1..10] . even()StreamSortsorts elements of a stream using a lambda functionstream([3,2,7,6,8]). sort([x,y]=>x-y).StreamExternalSortsorts elements of a stream using a function which implementsComparator<Object>or aBinaryFunctionwhich returns a Integer.StreamLimitreturn only the first n elements from a stream,StreamSkipskips the first n elements from a stream,StreamWhilereturn elements from a stream while a predicate is true,StreamAppendconcatenates two streams together,StreamMergemerge two streams together pairwise using a lambda functionStreamLooploops a stream indefinitely.
Terminal Functions:
StreamFoldreduces elements of a stream using a lambda function[3,2,7,6,8] . fold([x,y]=>x+y). By default throws an exception if the stream is empty.StreamFoldOrreduces elements of a stream using a lambda function, returning a default value if the stream is empty[3,2,7,6,8] . foldOr([x,y]=>x+y, -1). Returns -1 if the stream is empty.StreamReducereduces elements of a stream using a lambda function and an initial value[3,2,7,6,8] . reduce(1, [x,y]=>x*y). Returns 1 if the stream is empty.StreamFirstreturns the first element of a streamStreamFirstOrreturns the first element of a stream or a default value if the stream is empty.StreamLastreturns the last element of a stream.StreamCountcounts the number of elements in a stream.StreamMinMaxfinds the minimum or maximum numeric value of a stream.StreamTerminalFunctionuses a Jep function to perform a terminal operation on a stream,StreamTerminalNaryBinaryFunctionuses a Jep Nary-Binary function to perform a terminal operation on a stream.- {link StreamCollector} uses a Java Collector to perform a terminal operation on a stream.
Functions for operators:
StreamAssignan left hand assignment operator,=that converts streams to lists.v = [1..10]. map(x=>x^2)StreamSendToan right hand assignment operator->that converts streams to lists.[1..10]. map(x=>x^2) -> vStreamRangeused in the range syntax[1..10], returns a stream of values between the endpoints.FieldStreamRangeused in the range syntax[1..10], returns a stream of values calculated using aFieldI.
StreamEvaluator. An evaluator which converts final stream result to a List. This decorates an other evaluator.StreamListProcessor. Helper functions to convert arguments to and from streams and lists.
Typically the stream processing functions will used adapters in the
chained package
to build functions that can be used in a chain
[1..10] . map(x=>x^2) . foldOr([x,y] =>x+y,-1)
jep.addFunction("map",new ChainableBinaryFunction(new StreamMap()));
jep.addFunction("foldOr",
new ChainableGeneralFunction(
new ChainedNaryFunction(new StreamFoldOr()),
new StreamFoldOr(),
n -> n.jjtGetNumChildren() == 2));
Most functions mirror those in the lambda package
but are written to use methods in the Stream
class.
The terminal functions
StreamFirst, StreamFold, StreamLast, StreamMinMax and StreamTerminalNaryBinaryFunction
all, by default, throw an exception if the stream is empty. These all implement
ZeroLengthBehaviourI
so can be configured to return a default value instead.
Simple setup
TheStreamOperatorTable creates a set of
operators for lambda functions, and the range operator.
The ConfigurableParserWithRange adds support for parsing the range
operator syntax [1..10].
The StreamFunctionSet adds the various stream enabled higher order functions
the StreamListProcessor converts resutls to and from streams and lists,
and the StreamEvaluator converts stream results to lists.
var cot = new StreamOperatorTable("=>", "..", "->", true);
var cp = new ConfigurableParserWithRange("..");
FunctionSet lfs = new StreamFunctionSet();
lp = new StreamListProcessor();
StreamEvaluator se = new StreamEvaluator(new FastEvaluator());
jep = new Jep(cot, cp, lp, se, lfs);
- Since:
- Jep 4.1, extension 2.2
- See Also:
-
ClassesClassDescriptionA function which returns an increasing sequence using a
FieldI.Accumulate values in a sequence returning a sequence of intermediate values.Tests a predicate against sequence.Concatenates two streams into a single stream.Apply a lambda function, converting stream arguments to lists and passing them as a single argument.An assignment operator that converts rhs streams values toVector<Object>using theList.Extract the first element from a stream.Extract a stream with the first element removed.Create a new stream with the same elements as the original stream.Collects the elements of a stream into a list using a providedCollector.Counts the number of elements in a stream and returns it as a Number.Return only distinct elements of a stream.An evaluator that works with stream converting the final result ofStreamEvaluator.evaluate(Node)to a list.Filters elements of a stream based on a externally defined predicate.Applies an externally defined mapping function to each element of a stream.Sorts elements of a stream based on a comparator.Filter a list using a lambda function returning only those elements which meet a criteria.Returns the first element from a stream.Finds the first element that matches a predicate or return a default value.Returns the first element from a stream or a default value if the stream is empty.Fold a list using a lambda function to reduce the list down to a single argument.Fold or reduce a list using a lambda function and an defualt value returned when the stream is empty.Adds stream processing functions.Generate an infinite sequence using iteration using an initial value and a lambda function to calculate the next value and optionally a lambda function to determine when to end the stream.Returns the last element from a stream.Return only the first n elements of a stream.A ListProcessor implementation that can process lists, vectors and streams.Takes a stream and returns a stream that loops over the same elements indefinitely.Apply a lambda function to a list of arguments returning a list.Joins two sequences together, element by element.Returns the minimum or maximum element from a stream.An OperatorTable for stream package.A function which returns an increasing sequence of double values all the items between the end points inclusive.Reduce a list to a single value using a lambda function and an initial identity value.An assignment operator so we can do3+4 -> xThis function implements the CallbackEvaluationI interface so that it handles setting the value of a variable.An assignment operator allowing unclosed steams to be saved to a variable.Skip the first n elements of a stream.Use a lambda functions to sort items in a stream.A PostfixMathCommandI which returns a stream specified in the constructor.A terminal function that takes a Stream as its single argument, takes all elements from the stream and passes them as arguments to the inner functions.Terminal function that reduces a stream to a single value using anNaryBinaryFunction.Convert the Stream to a list using the ListProcessor.Filter a stream so only the elements from the start of the stream that match a criteria are returned.