Class ChainableFunctionSet

All Implemented Interfaces:
JepComponent, Serializable

public class ChainableFunctionSet extends FunctionSet
Adds chainable list processing functions.
  • Generators:
    • iterate - creates a stream using an initial value, a unary lambda function giving the next value and a predicate lambda function to determine when to end the stream. iterate(0, x=>x+1, x=>x<10) . count()
  • 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)
    • 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] )
    • distinct - return a list with duplicate elements removed: [1,2,3,2,1] . distinct() produces [1,2,3] order is not guaranteed.
    • flatten - flatten a list of lists to a single list. [[1,2],[3,4]] . flatten() produces [1,2,3,4].
    • 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]
  • 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()
    • 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 to a single value [1..10] . map(x=>x^2) . fold([x,y]=>x+y) . apply(x=>sqrt(x))
    • chained - applies a lambda function taking multiple values to a sequence [1..5] . chained([n,seq]=>map(x=>x^n,seq), 3)
    • define - defines regular Jep function using a lambda function define(mysum, [x,y] => x+y); mysum(2,3);

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 as append([1..5], [6..10])
  • merge - [1..5] . merge((a,b)=>a+b, [6..10]) is the same as merge((a,b)=>a+b, [1..5], [6..10])
as the swapped versions are semantically more intuitive.

Names used can be changed in the properties file

ChainableFunctionSet.map=map
Use an ! in the property value to prevent the function being added, e.g.
ChainableFunctionSet.map=! skip
See Also:
  • Constructor Details

    • ChainableFunctionSet

      public ChainableFunctionSet()
      Creates a set of chainable higher order functions. The implementations uses Double.compareTo(Double) to compare numeric values in the nsort and rsort functions; MinMax to find the minimum and maximum numeric values in the min and max functions; and Add to sum numeric values in the sum function.
    • ChainableFunctionSet

      public ChainableFunctionSet(FieldI field)
      Creates a set of chained higher order functions for a specific field. The implementations uses FieldComparator in the nsort and rsort functions; ElementMinMax to find the minimum and maximum numeric values in the min and max functions; and the FieldI.add(Object, Object) method to sum numeric values in the sum function.
      Parameters:
      field - Field to use for numeric comparisons and sums.