| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,20 @@ | |||
| 1 | 1 | package lambdasinaction.chap13; | |
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | + import java.util.function.Function; | ||
| 5 | + | ||
| 4 | 6 | 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 | + | ||
| 5 | 20 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments