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).
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 name | Type | Params | Description |
|---|---|---|---|
| avg | Function<ValuePair<Number,Number>,BigDecimal> | ||
| avg | Function<ValuePair<Number,Number>,BigDecimal> | RoundingMode roundingMode | |
| avg | Function<ValuePair<Number,Number>,BigDecimal> | MathContext mathContext | |
| div | Function<ValuePair<Number,Number>,BigDecimal> | ||
| div | Function<ValuePair<Number,Number>,BigDecimal> | RoundingMode roundingMode | |
| div | Function<ValuePair<Number,Number>,BigDecimal> | MathContext mathContext | |
| mult | Function<ValuePair<Number,Number>,BigDecimal> | ||
| remainder | Function<ValuePair<Number,Number>,BigDecimal> | ||
| remainder | Function<ValuePair<Number,Number>,BigDecimal> | MathContext mathContext | |
| subt | Function<ValuePair<Number,Number>,BigDecimal> | ||
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 name | Type | Params | Description |
|---|---|---|---|
| avg | Function<ValuePair<Number,Number>,BigInteger> | ||
| avg | Function<ValuePair<Number,Number>,BigInteger> | RoundingMode roundingMode | |
| avg | Function<ValuePair<Number,Number>,BigInteger> | MathContext mathContext | |
| div | Function<ValuePair<Number,Number>,BigInteger> | ||
| div | Function<ValuePair<Number,Number>,BigInteger> | RoundingMode roundingMode | |
| div | Function<ValuePair<Number,Number>,BigInteger> | MathContext mathContext | |
| mult | Function<ValuePair<Number,Number>,BigInteger> | ||
| remainder | Function<ValuePair<Number,Number>,BigInteger> | ||
| remainder | Function<ValuePair<Number,Number>,BigInteger> | MathContext mathContext | |
| subt | Function<ValuePair<Number,Number>,BigInteger> | ||
These functions are to be applied on Boolean elements and all of them return a Boolean.
| Function name | Type | Params | Description |
|---|---|---|---|
| or | Function<ValuePair<Boolean,Boolean>,Boolean> |
These functions are to be applied on Numbers and all of them return a Byte.
| Function name | Type | Params | Description |
|---|---|---|---|
| avg | Function<ValuePair<Number,Number>,Byte> | ||
| avg | Function<ValuePair<Number,Number>,Byte> | RoundingMode roundingMode | |
| avg | Function<ValuePair<Number,Number>,Byte> | MathContext mathContext | |
| div | Function<ValuePair<Number,Number>,Byte> | ||
| div | Function<ValuePair<Number,Number>,Byte> | RoundingMode roundingMode | |
| div | Function<ValuePair<Number,Number>,Byte> | MathContext mathContext | |
| mult | Function<ValuePair<Number,Number>,Byte> | ||
| remainder | Function<ValuePair<Number,Number>,Byte> | ||
| remainder | Function<ValuePair<Number,Number>,Byte> | MathContext mathContext | |
| subt | Function<ValuePair<Number,Number>,Byte> | ||
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 name | Type | Params | Description |
|---|---|---|---|
| avg | Function<ValuePair<Number,Number>,Double> | ||
| avg | Function<ValuePair<Number,Number>,Double> | RoundingMode roundingMode | |
| avg | Function<ValuePair<Number,Number>,Double> | MathContext mathContext | |
| div | Function<ValuePair<Number,Number>,Double> | ||
| div | Function<ValuePair<Number,Number>,Double> | RoundingMode roundingMode | |
| div | Function<ValuePair<Number,Number>,Double> | MathContext mathContext | |
| mult | Function<ValuePair<Number,Number>,Double> | ||
| remainder | Function<ValuePair<Number,Number>,Double> | ||
| remainder | Function<ValuePair<Number,Number>,Double> | MathContext mathContext | |
| subt | Function<ValuePair<Number,Number>,Double> | ||
These functions are to be applied on Numbers and all of them return a Float.
| Function name | Type | Params | Description |
|---|---|---|---|
| avg | Function<ValuePair<Number,Number>,Float> | ||
| avg | Function<ValuePair<Number,Number>,Float> | RoundingMode roundingMode | |
| avg | Function<ValuePair<Number,Number>,Float> | MathContext mathContext | |
| div | Function<ValuePair<Number,Number>,Float> | ||
| div | Function<ValuePair<Number,Number>,Float> | RoundingMode roundingMode | |
| div | Function<ValuePair<Number,Number>,Float> | MathContext mathContext | |
| mult | Function<ValuePair<Number,Number>,Float> | ||
| remainder | Function<ValuePair<Number,Number>,Float> | ||
| remainder | Function<ValuePair<Number,Number>,Float> | MathContext mathContext | |
| subt | Function<ValuePair<Number,Number>,Float> | ||
These functions are to be applied on Numbers and all of them return an Integer.
| Function name | Type | Params | Description |
|---|---|---|---|
| avg | Function<ValuePair<Number,Number>,Integer> | ||
| avg | Function<ValuePair<Number,Number>,Integer> | RoundingMode roundingMode | |
| avg | Function<ValuePair<Number,Number>,Integer> | MathContext mathContext | |
| div | Function<ValuePair<Number,Number>,Integer> | ||
| div | Function<ValuePair<Number,Number>,Integer> | RoundingMode roundingMode | |
| div | Function<ValuePair<Number,Number>,Integer> | MathContext mathContext | |
| mult | Function<ValuePair<Number,Number>,Integer> | ||
| remainder | Function<ValuePair<Number,Number>,Integer> | ||
| remainder | Function<ValuePair<Number,Number>,Integer> | MathContext mathContext | |
| subt | Function<ValuePair<Number,Number>,Integer> | ||
These functions are to be applied on Numbers and all of them return a Long.
| Function name | Type | Params | Description |
|---|---|---|---|
| avg | Function<ValuePair<Number,Number>,Long> | ||
| avg | Function<ValuePair<Number,Number>,Long> | RoundingMode roundingMode | |
| avg | Function<ValuePair<Number,Number>,Long> | MathContext mathContext | |
| div | Function<ValuePair<Number,Number>,Long> | ||
| div | Function<ValuePair<Number,Number>,Long> | RoundingMode roundingMode | |
| div | Function<ValuePair<Number,Number>,Long> | MathContext mathContext | |
| mult | Function<ValuePair<Number,Number>,Long> | ||
| remainder | Function<ValuePair<Number,Number>,Long> | ||
| remainder | Function<ValuePair<Number,Number>,Long> | MathContext mathContext | |
| subt | Function<ValuePair<Number,Number>,Long> | ||
These functions are to be applied on Numbers and all of them return a Short.
| Function name | Type | Params | Description |
|---|---|---|---|
| avg | Function<ValuePair<Number,Number>,Short> | ||
| avg | Function<ValuePair<Number,Number>,Short> | RoundingMode roundingMode | |
| avg | Function<ValuePair<Number,Number>,Short> | MathContext mathContext | |
| div | Function<ValuePair<Number,Number>,Short> | ||
| div | Function<ValuePair<Number,Number>,Short> | RoundingMode roundingMode | |
| div | Function<ValuePair<Number,Number>,Short> | MathContext mathContext | |
| mult | Function<ValuePair<Number,Number>,Short> | ||
| remainder | Function<ValuePair<Number,Number>,Short> | ||
| remainder | Function<ValuePair<Number,Number>,Short> | MathContext mathContext | |
| subt | Function<ValuePair<Number,Number>,Short> | ||
These functions are to be applied on Strings and all of them return a String as well.
| Function name | Type | Params | Description |
|---|---|---|---|
| join | Function<ValuePair<String,String>,String> | String separator |
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 name | Type | Params | Description |
|---|---|---|---|
| max | Function<ValuePair<T,T>,T> | ||
| maxBy | <X extends Comparable<? super X>> Function<ValuePair<T,T>,T> | IFunction<? super T,X> function | |
| min | Function<ValuePair<T,T>,T> | ||
| minBy | <X extends Comparable<? super X>> Function<ValuePair<T,T>,T> | IFunction<? super T,X> function | |