Reducer operator

Report a typo

Wow! This problem is kind of tricky. If you're ready to put your thinking cap on, brace yourself and good luck! Otherwise, you can skip it for now and return any time later.

In this problem, you need to complete the implementations of two functions.

  1. The reduceIntOperator that 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.
  2. Based on reduceIntOperator, implement the productOperator operator 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
Write a program in Java 17
import java.util.Scanner;
import java.util.function.*;
import java.util.stream.*;

class CustomReducer {

/**
* The operator combines all values in the given range into one value
* using combiner and initial value (seed)
*/
final BiFunction<Integer, IntBinaryOperator, IntBinaryOperator> reduceIntOperator =
// write your code here

/**
* The operator calculates the sum in the given range (inclusively)
*/
final IntBinaryOperator sumOperator = reduceIntOperator.apply(0, (x, y) -> x + y);

/**
* The operator calculates the product in the given range (inclusively)
*/
final IntBinaryOperator productOperator = // write your code here

// Don't change the code below
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
String[] values = scanner.nextLine().split(" ");

int l = Integer.parseInt(values[0]);
int r = Integer.parseInt(values[1]);

CustomReducer reducer = new CustomReducer();

int sumReducer = reducer.reduceIntOperator.apply(0, Integer::sum).applyAsInt(l, r);
int sum = reducer.sumOperator.applyAsInt(l, r);
___

Create a free account to access the full topic