32 lines
963 B
Java
32 lines
963 B
Java
package fp;
|
|
|
|
import java.util.function.IntBinaryOperator;
|
|
import java.util.function.IntUnaryOperator;
|
|
|
|
public class IntOperatorsUtil {
|
|
public static IntUnaryOperator compose(IntUnaryOperator... functions) {
|
|
IntUnaryOperator f = i -> i;
|
|
for (IntUnaryOperator g : functions) {
|
|
f = f.andThen(g);
|
|
}
|
|
return f;
|
|
}
|
|
|
|
public static IntUnaryOperator partial(IntBinaryOperator f, int x) {
|
|
return i -> f.applyAsInt(x, i);
|
|
}
|
|
|
|
public static void main(String... args) {
|
|
IntUnaryOperator[] functions = {
|
|
compose(),
|
|
compose(x -> x + 1),
|
|
compose(x -> x * 2, x -> x + 1),
|
|
compose(x -> x + 1, x -> x * 2),
|
|
compose(x -> x + 1, x -> x * 2, x -> x + 1)
|
|
};
|
|
for (IntUnaryOperator f : functions) System.out.println(f.applyAsInt(1));
|
|
|
|
IntOperatorsUtil.partial((x, y) -> x-y, 1).applyAsInt(2); // -1
|
|
}
|
|
}
|