Skip to content

Commit dc888bc

Browse files
committed
combinators chap 13
1 parent 3c9c9cc commit dc888bc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package lambdasinaction.chap13;
22

33

4+
import java.util.function.Function;
5+
46
public class Combinators {
7+
8+
public static void main(String[] args) {
9+
System.out.println(repeat(3, (Integer x)->2*x).apply(10));
10+
}
11+
12+
static <A,B,C> Function<A,C> compose(Function<B,C> g, Function<A,B> f) {
13+
return x -> g.apply(f.apply(x));
14+
}
15+
16+
static <A> Function<A,A> repeat(int n, Function<A,A> f) {
17+
return n==0?x->x : compose(f, (Function<A,A>) repeat(n-1, f));
18+
}
19+
520
}

0 commit comments

Comments
 (0)