In this problem, you need to complete the implementations of two functions.
- The
reduceIntOperatorthat accepts an initial value (seed) and a combiner function and returns a new function that combines all values from the given integer range into a single integer value (it's a simple form of reduction) using a given operator. The seed is used as the very first element in reducing. - Based on
reduceIntOperator, implement theproductOperatoroperator that multiplies integer values in the range.
There is also an example: the sumOperator that is based on reduceIntOperator. It uses 0 as the seed value and the function x + y and just sums numbers in the range.
Let's take a look at two example of how the operators should work. The left boundary <= the right boundary.
Example 1. Left boundary = 1, right boundary = 4.
To check reduceIntOperator testing system creates and passes an object with type IntBinaryOperator and a seed value. reduceIntOperator produces an object of IntBinaryOperator type, which is used to produce a final int result.
Passed IntBinaryOperator is sum operator.
IntBinaryOperator sum = (x, y) -> x + y;
IntBinaryOperator resultWithSumOperator = reduceIntOperator.apply(5, sum);
resultWithSumOperator.applyAsInt(1, 4); // 15 = 5 + (1 + 2 + 3 + 4)
Passed IntBinaryOperator is production operator.
IntBinaryOperator multiply = (x, y) -> x * y;
IntBinaryOperator resultWitMultiplyOperator = reduceIntOperator.apply(5, multiply);
resultWitMultiplyOperator.applyAsInt(1, 4); // 120 = 5 * (1 * 2 * 3 * 4)
sumOperator and productOperator produces int result:
sumOperator.applyAsInt(1, 4); // 10 = 1 + 2 + 3 + 4
productOperator.applyAsInt(1, 4); // 24 = 1 * 2 * 3 * 4
Example 2. Left boundary = 5, right boundary = 6.
Passed IntBinaryOperator is sum operator.
IntBinaryOperator sum = (x, y) -> x + y;
IntBinaryOperator resultWithSumOperator = reduceIntOperator.apply(2, sum);
resultWithSumOperator.applyAsInt(5, 6); // 13 = 2 + (5 + 6)
Passed IntBinaryOperator is production operator.
IntBinaryOperator multiply = (x, y) -> x * y;
IntBinaryOperator resultWitMultiplyOperator = reduceIntOperator.apply(2, multiply);
resultWitMultiplyOperator.applyAsInt(5, 6); // 60 = 2 * (5 * 6)
sumOperator and productOperator produces int result:
sumOperator.applyAsInt(5, 6); // 11 = 5 + 6
productOperator.applyAsInt(5, 6); // 30 = 5 * 6