JetBrains Academy: Currying - Kamil-Jankowski/Learning-JAVA GitHub Wiki
JetBrains Academy: Currying
Curried function - 1:
Write a curried form of the function f(x, y, z) = x + y * y + z * z * z using lambda expressions in Java 8 style. The result and x, y, z must be integer numbers.
x -> y -> z -> x + y * y + z * z * z;
Curried function - 2:
Write a curried function (using lambdas) that accepts four string arguments and concatenated all in one string following the rules:
- String cases: in the result string, first and second arguments must be in lower cases and third and fourth in upper cases.
- Order of arguments concatenation: first, third, second, fourth.
x -> y -> z -> u -> x.toLowerCase() + z.toUpperCase() + y.toLowerCase() + u.toUpperCase();
Multifunctional mapper:
Write three functions:
- Multifunctional mapper (transformer) that accepts a list of operators (mappers) and returns a new operator. The returned operator accepts a list of integer numbers and sequentially applies each mapper to each number in the list (performs multiple transformations). The result is a list with transformed values.
- In terms of the multifunctional mapper define an operator that multiplies by two each integer number and then add one to it. The operator is applied to each number in the input list.
- In terms of the multifunctional mapper define an operator that squares each integer number and then calculates the next even number following it. The operator is also applied to each number in the input list.
To simplify the problem all function (represented by objects) are declared, you need to finish their realization. Look carefully at definition of each function.
Also there is an example: identity operation that is defined in terms of the multifunctional mapper. It doesn't changes values in the input list. It repeats identity transformation three times just for example.
During testing all operators will be tested (including identity).
TBD
Reducer operator:
Write three operators:
- A reduce operator that accepts an initial value (seed) and a combiner function and then returns a new function that combines all values in the given integer range (inclusively) into one integer value (it's a simple form of reduction).
- In terms of the reduce operator define a sum operator for summing integer values in the given range.
- In terms of the reduce operator define a product operator for multiplying integer values in the given range.
Try not to use Stream API. Write the reducer yourself.
To simplify the problem all functions are declared, you need to finish their realization. Look carefully at definition of each operator.
During testing all three operators will be tested. The left boundary <= the right boundary.
/**
* The operator combines all values in the given range into one value
* using combiner and initial value (seed)
*/
public static final BiFunction<Integer, IntBinaryOperator, IntBinaryOperator> reduceIntOperator = (seed, f) -> (left, right) -> {
int result = seed;
while (left <= right){
result = f.applyAsInt(result, left);
left++;
}
return result;
};
/**
* The operator calculates the sum in the given range (inclusively)
*/
public static final IntBinaryOperator sumOperator = reduceIntOperator.apply(0, (x, y) -> x + y); // or: "Integer::sum"
/**
* The operator calculates the product in the given range (inclusively)
*/
public static final IntBinaryOperator productOperator = reduceIntOperator.apply(1, (x, y) -> x * y);