This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
PF3/hw3/src/fp/IntOperatorsUtil.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
}
}