Functions: FnReduce

1. Overview

Reduce (also known as Fold) functions receive, as a parameter, a function that is applied to two elements on each execution: the function executes on two consecutive elements of the target list and returns another one that will be used as the first target element for the next execution of the function.

FnReduce is a parameterizable function hub class and, before giving you access to the real functions, you will have to specify the type parameter, which will give you access to the functions containing the reduce operations:


These Reduce operations are executed by means of the reduce method as follows:

Op.on(...)...reduce(FnReduce.onX().Y(...))...;

as in

/* In this case, we insert an initial value: 13 */ 
Op.on(new Integer[] {2, 3, 4}).toList().reduce(FnReduce.onBigDecimal().avg(), BigDecimal.valueOf(13)).get(); 
            
            
Op.on(new Float[] {Float.valueOf(3), Float.valueOf(5), Float.valueOf(8), Float.valueOf(9)}) 
    .toList().reduce(FnReduce.onFloat().div(MathContext.DECIMAL32)).get(); 
            
 
/* In this case, we insert an initial value: 100 */ 
Op.on(new Integer[] {Integer.valueOf(3), Integer.valueOf(5), Integer.valueOf(8), Integer.valueOf(9)}) 
    .toList().reduce(FnReduce.onFloat().div(MathContext.DECIMAL32), Float.valueOf(100)).get();

Note that you can specify an initial value (as in the first and third examples above) which is initially combined with the first target element in the list (as op4j implements left fold).

FnReduceOnBigDecimal

These functions are to be applied on Numbers and all of them return a BigDecimal. Let's see some examples:

/*  The code below would return 3.25 as avg(2, 3, 4) -> avg(avg(2, 3), 4) -> avg(2.5, 4) -> 3.25 */ 
Op.on(new Integer[] {2, 3, 4}).toList() 
    .forEach().exec(FnNumber.toBigDecimal()).endFor()              
    .reduce(FnReduce.onBigDecimal().avg(MathContext.DECIMAL32)).get(); 
                

/*  The code below would return 3 as avg(0, 2, 3, 4) -> avg(avg(avg(0, 2), 3), 4) -> avg(avg(1, 3), 4) -> avg(2, 4) -> 3 */ 
Op.on(new Integer[] {2, 3, 4}).toList()                             
    .reduce(FnReduce.onBigDecimal().avg(MathContext.DECIMAL32), BigDecimal.valueOf(0)).get();
Function nameTypeParamsDescription
avgFunction<ValuePair<Number,Number>,BigDecimal>
avgFunction<ValuePair<Number,Number>,BigDecimal>RoundingMode roundingMode
avgFunction<ValuePair<Number,Number>,BigDecimal>MathContext mathContext
divFunction<ValuePair<Number,Number>,BigDecimal>
divFunction<ValuePair<Number,Number>,BigDecimal>RoundingMode roundingMode
divFunction<ValuePair<Number,Number>,BigDecimal>MathContext mathContext
multFunction<ValuePair<Number,Number>,BigDecimal>
remainderFunction<ValuePair<Number,Number>,BigDecimal>
remainderFunction<ValuePair<Number,Number>,BigDecimal>MathContext mathContext
subtFunction<ValuePair<Number,Number>,BigDecimal>

FnReduceOnBigInteger

These functions are to be applied on Numbers and all of them return an Integer. Let's see some examples:

Op.on(new Integer[] {Integer.valueOf(3), Integer.valueOf(5), Integer.valueOf(8), Integer.valueOf(9)}).toList()                      
    .reduce(FnReduce.onBigInteger().div(MathContext.DECIMAL32), BigInteger.valueOf(100)).get();
  
            
Op.on(new BigInteger[] {BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(8), BigInteger.valueOf(9)}).toList()                          
    .reduce(FnReduce.onBigInteger().div(MathContext.DECIMAL32)).get();
Function nameTypeParamsDescription
avgFunction<ValuePair<Number,Number>,BigInteger>
avgFunction<ValuePair<Number,Number>,BigInteger>RoundingMode roundingMode
avgFunction<ValuePair<Number,Number>,BigInteger>MathContext mathContext
divFunction<ValuePair<Number,Number>,BigInteger>
divFunction<ValuePair<Number,Number>,BigInteger>RoundingMode roundingMode
divFunction<ValuePair<Number,Number>,BigInteger>MathContext mathContext
multFunction<ValuePair<Number,Number>,BigInteger>
remainderFunction<ValuePair<Number,Number>,BigInteger>
remainderFunction<ValuePair<Number,Number>,BigInteger>MathContext mathContext
subtFunction<ValuePair<Number,Number>,BigInteger>

FnReduceOnBoolean

These functions are to be applied on Boolean elements and all of them return a Boolean.

Function nameTypeParamsDescription
orFunction<ValuePair<Boolean,Boolean>,Boolean>

FnReduceOnByte

These functions are to be applied on Numbers and all of them return a Byte.

Function nameTypeParamsDescription
avgFunction<ValuePair<Number,Number>,Byte>
avgFunction<ValuePair<Number,Number>,Byte>RoundingMode roundingMode
avgFunction<ValuePair<Number,Number>,Byte>MathContext mathContext
divFunction<ValuePair<Number,Number>,Byte>
divFunction<ValuePair<Number,Number>,Byte>RoundingMode roundingMode
divFunction<ValuePair<Number,Number>,Byte>MathContext mathContext
multFunction<ValuePair<Number,Number>,Byte>
remainderFunction<ValuePair<Number,Number>,Byte>
remainderFunction<ValuePair<Number,Number>,Byte>MathContext mathContext
subtFunction<ValuePair<Number,Number>,Byte>

FnReduceOnDouble

These functions are to be applied on Numbers and all of them return a Double. Let's see some examples:

/*  The code below would return 3.25 as avg(2, 3, 4) -> avg(avg(2, 3), 4) -> avg(2.5, 4) -> 3.25 */ 
Op.on(new Integer[] {2, 3, 4}).toList() 
    .forEach().exec(FnNumber.toDouble()).endFor()                
    .reduce(FnReduce.onDouble().avg(MathContext.DECIMAL32)).get(); 
    
            
/*  The code below would return 3.525 as avg(4.2, 2, 3, 4) -> avg(avg(avg(4.2, 2), 3), 4) -> avg(avg(3.1, 3), 4) -> avg(3.05, 4) -> 3.525 */ 
Op.on(new Integer[] {2, 3, 4}).toList()                             
    .reduce(FnReduce.onDouble().avg(MathContext.DECIMAL32), Double.valueOf(4.2)).get();
Function nameTypeParamsDescription
avgFunction<ValuePair<Number,Number>,Double>
avgFunction<ValuePair<Number,Number>,Double>RoundingMode roundingMode
avgFunction<ValuePair<Number,Number>,Double>MathContext mathContext
divFunction<ValuePair<Number,Number>,Double>
divFunction<ValuePair<Number,Number>,Double>RoundingMode roundingMode
divFunction<ValuePair<Number,Number>,Double>MathContext mathContext
multFunction<ValuePair<Number,Number>,Double>
remainderFunction<ValuePair<Number,Number>,Double>
remainderFunction<ValuePair<Number,Number>,Double>MathContext mathContext
subtFunction<ValuePair<Number,Number>,Double>

FnReduceOnFloat

These functions are to be applied on Numbers and all of them return a Float.

Function nameTypeParamsDescription
avgFunction<ValuePair<Number,Number>,Float>
avgFunction<ValuePair<Number,Number>,Float>RoundingMode roundingMode
avgFunction<ValuePair<Number,Number>,Float>MathContext mathContext
divFunction<ValuePair<Number,Number>,Float>
divFunction<ValuePair<Number,Number>,Float>RoundingMode roundingMode
divFunction<ValuePair<Number,Number>,Float>MathContext mathContext
multFunction<ValuePair<Number,Number>,Float>
remainderFunction<ValuePair<Number,Number>,Float>
remainderFunction<ValuePair<Number,Number>,Float>MathContext mathContext
subtFunction<ValuePair<Number,Number>,Float>

FnReduceOnInteger

These functions are to be applied on Numbers and all of them return an Integer.

Function nameTypeParamsDescription
avgFunction<ValuePair<Number,Number>,Integer>
avgFunction<ValuePair<Number,Number>,Integer>RoundingMode roundingMode
avgFunction<ValuePair<Number,Number>,Integer>MathContext mathContext
divFunction<ValuePair<Number,Number>,Integer>
divFunction<ValuePair<Number,Number>,Integer>RoundingMode roundingMode
divFunction<ValuePair<Number,Number>,Integer>MathContext mathContext
multFunction<ValuePair<Number,Number>,Integer>
remainderFunction<ValuePair<Number,Number>,Integer>
remainderFunction<ValuePair<Number,Number>,Integer>MathContext mathContext
subtFunction<ValuePair<Number,Number>,Integer>

FnReduceOnLong

These functions are to be applied on Numbers and all of them return a Long.

Function nameTypeParamsDescription
avgFunction<ValuePair<Number,Number>,Long>
avgFunction<ValuePair<Number,Number>,Long>RoundingMode roundingMode
avgFunction<ValuePair<Number,Number>,Long>MathContext mathContext
divFunction<ValuePair<Number,Number>,Long>
divFunction<ValuePair<Number,Number>,Long>RoundingMode roundingMode
divFunction<ValuePair<Number,Number>,Long>MathContext mathContext
multFunction<ValuePair<Number,Number>,Long>
remainderFunction<ValuePair<Number,Number>,Long>
remainderFunction<ValuePair<Number,Number>,Long>MathContext mathContext
subtFunction<ValuePair<Number,Number>,Long>

FnReduceOnShort

These functions are to be applied on Numbers and all of them return a Short.

Function nameTypeParamsDescription
avgFunction<ValuePair<Number,Number>,Short>
avgFunction<ValuePair<Number,Number>,Short>RoundingMode roundingMode
avgFunction<ValuePair<Number,Number>,Short>MathContext mathContext
divFunction<ValuePair<Number,Number>,Short>
divFunction<ValuePair<Number,Number>,Short>RoundingMode roundingMode
divFunction<ValuePair<Number,Number>,Short>MathContext mathContext
multFunction<ValuePair<Number,Number>,Short>
remainderFunction<ValuePair<Number,Number>,Short>
remainderFunction<ValuePair<Number,Number>,Short>MathContext mathContext
subtFunction<ValuePair<Number,Number>,Short>

FnReduceOnString

These functions are to be applied on Strings and all of them return a String as well.

Function nameTypeParamsDescription
joinFunction<ValuePair<String,String>,String>String separator

FnReduceOn

The functions in FnReduceOn are applied on objects of any type. FnReduceOn is a generic class and, by calling FnReduce.onCharacter(), FnReduce.onCalendar(), FnReduce.onDate(), FnReduce.onObject() or FnReduce.on(final Type<T> type), the type parameter can be replaced with the type you need at any time so that chaining functions becomes easier and less error-prone.

Op.on(new Calendar[] {Calendar.getInstance(), Calendar.getInstance()}).toList()                         
    .reduce(FnReduce.onCalendar().min()).get();
    
            
Op.on(aListOfComprables))                      
    .reduce(FnReduce.on(Types.forClass(MyClassOfComparables.class)).min()).get());
Function nameTypeParamsDescription
maxFunction<ValuePair<T,T>,T>
maxBy<X extends Comparable<? super X>> Function<ValuePair<T,T>,T>IFunction<? super T,X> function
minFunction<ValuePair<T,T>,T>
minBy<X extends Comparable<? super X>> Function<ValuePair<T,T>,T>IFunction<? super T,X> function