|
1 | 1 | package com.jnape.palatable.lambda; |
2 | 2 |
|
| 3 | +import com.jnape.palatable.lambda.adt.Either; |
3 | 4 | import com.jnape.palatable.lambda.adt.Maybe; |
4 | | -import com.jnape.palatable.lambda.adt.hlist.Tuple2; |
5 | | -import com.jnape.palatable.lambda.adt.hlist.Tuple3; |
6 | 5 | import com.jnape.palatable.lambda.adt.hlist.Tuple4; |
7 | | -import com.jnape.palatable.lambda.functions.specialized.Predicate; |
8 | | -import com.jnape.palatable.lambda.structural.Case; |
9 | | -import com.jnape.palatable.lambda.structural.Match; |
| 6 | +import com.jnape.palatable.lambda.functions.Fn3; |
| 7 | +import com.jnape.palatable.lambda.functions.Fn4; |
| 8 | +import com.jnape.palatable.lambda.functions.Fn5; |
| 9 | +import com.jnape.palatable.lambda.functions.Fn6; |
| 10 | +import com.jnape.palatable.lambda.functions.Fn7; |
| 11 | +import com.jnape.palatable.lambda.functions.Fn8; |
| 12 | +import com.jnape.palatable.lambda.functor.Applicative; |
10 | 13 | import com.jnape.palatable.lambda.structural.Struct; |
11 | 14 |
|
| 15 | +import java.util.function.BiFunction; |
| 16 | +import java.util.function.Function; |
| 17 | + |
| 18 | +import static com.jnape.palatable.lambda.adt.Either.right; |
12 | 19 | import static com.jnape.palatable.lambda.adt.Maybe.just; |
13 | 20 | import static com.jnape.palatable.lambda.adt.hlist.HList.tuple; |
| 21 | +import static com.jnape.palatable.lambda.functions.Fn2.fn2; |
14 | 22 | import static com.jnape.palatable.lambda.functions.builtin.fn2.Eq.eq; |
15 | 23 | import static com.jnape.palatable.lambda.structural.Case.of; |
16 | 24 | import static com.jnape.palatable.lambda.structural.Cases.cases; |
17 | | -import static com.jnape.palatable.lambda.structural.CatchAll.__; |
| 25 | +import static com.jnape.palatable.lambda.structural.Matcher.$; |
| 26 | +import static com.jnape.palatable.lambda.structural.Matchers.$just; |
| 27 | +import static com.jnape.palatable.lambda.structural.Matchers.Any.$__; |
18 | 28 | import static com.jnape.palatable.lambda.structural.Struct.struct; |
19 | 29 |
|
20 | 30 | public class Spike { |
21 | 31 |
|
22 | | - public static <A> Case.DestructuringPredicate<Maybe<A>, A> $just(Predicate<A> predicate) { |
23 | | - return m -> m.filter(predicate); |
| 32 | + public static <A, R, App extends Applicative, AppR extends Applicative<R, App>> AppR liftA(Applicative<A, App> appA, |
| 33 | + Function<? super A, ? extends R> fn) { |
| 34 | + return appA.<R>fmap(fn).coerce(); |
| 35 | + } |
| 36 | + |
| 37 | + public static <A, B, R, App extends Applicative, AppR extends Applicative<R, App>> AppR liftA( |
| 38 | + Applicative<A, App> appA, |
| 39 | + Applicative<B, App> appB, |
| 40 | + BiFunction<? super A, ? super B, ? extends R> fn) { |
| 41 | + return appB.<R>zip(appA.fmap(fn2(fn))).coerce(); |
24 | 42 | } |
25 | 43 |
|
26 | | - public static <A> Case.DestructuringPredicate<Maybe<A>, A> $just(A a) { |
27 | | - return $just(eq(a)); |
| 44 | + public static <A, B, C, R, App extends Applicative, AppR extends Applicative<R, App>> AppR liftA( |
| 45 | + Applicative<A, App> appA, |
| 46 | + Applicative<B, App> appB, |
| 47 | + Applicative<C, App> appC, |
| 48 | + Fn3<? super A, ? super B, ? super C, ? extends R> fn) { |
| 49 | + return appC.<R>zip(liftA(appA, appB, fn.toBiFunction())).coerce(); |
28 | 50 | } |
29 | 51 |
|
| 52 | + public static <A, B, C, D, R, App extends Applicative, AppR extends Applicative<R, App>> AppR liftA( |
| 53 | + Applicative<A, App> appA, |
| 54 | + Applicative<B, App> appB, |
| 55 | + Applicative<C, App> appC, |
| 56 | + Applicative<D, App> appD, |
| 57 | + Fn4<? super A, ? super B, ? super C, ? super D, ? extends R> fn) { |
| 58 | + return appD.<R>zip(liftA(appA, appB, appC, fn)).coerce(); |
| 59 | + } |
| 60 | + |
| 61 | + public static <A, B, C, D, E, R, App extends Applicative, AppR extends Applicative<R, App>> AppR liftA( |
| 62 | + Applicative<A, App> appA, |
| 63 | + Applicative<B, App> appB, |
| 64 | + Applicative<C, App> appC, |
| 65 | + Applicative<D, App> appD, |
| 66 | + Applicative<E, App> appE, |
| 67 | + Fn5<? super A, ? super B, ? super C, ? super D, ? super E, ? extends R> fn) { |
| 68 | + return appE.<R>zip(liftA(appA, appB, appC, appD, fn)).coerce(); |
| 69 | + } |
| 70 | + |
| 71 | + public static <A, B, C, D, E, F, R, App extends Applicative, AppR extends Applicative<R, App>> AppR liftA( |
| 72 | + Applicative<A, App> appA, |
| 73 | + Applicative<B, App> appB, |
| 74 | + Applicative<C, App> appC, |
| 75 | + Applicative<D, App> appD, |
| 76 | + Applicative<E, App> appE, |
| 77 | + Applicative<F, App> appF, |
| 78 | + Fn6<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, ? extends R> fn) { |
| 79 | + return appF.<R>zip(liftA(appA, appB, appC, appD, appE, fn)).coerce(); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + public static <A, B, C, D, E, F, G, R, App extends Applicative, AppR extends Applicative<R, App>> AppR liftA( |
| 84 | + Applicative<A, App> appA, |
| 85 | + Applicative<B, App> appB, |
| 86 | + Applicative<C, App> appC, |
| 87 | + Applicative<D, App> appD, |
| 88 | + Applicative<E, App> appE, |
| 89 | + Applicative<F, App> appF, |
| 90 | + Applicative<G, App> appG, |
| 91 | + Fn7<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, ? super G, ? extends R> fn) { |
| 92 | + return appG.<R>zip(liftA(appA, appB, appC, appD, appE, appF, fn)).coerce(); |
| 93 | + } |
| 94 | + |
| 95 | + public static <A, B, C, D, E, F, G, H, R, App extends Applicative, AppR extends Applicative<R, App>> AppR liftA( |
| 96 | + Applicative<A, App> appA, |
| 97 | + Applicative<B, App> appB, |
| 98 | + Applicative<C, App> appC, |
| 99 | + Applicative<D, App> appD, |
| 100 | + Applicative<E, App> appE, |
| 101 | + Applicative<F, App> appF, |
| 102 | + Applicative<G, App> appG, |
| 103 | + Applicative<H, App> appH, |
| 104 | + Fn8<? super A, ? super B, ? super C, ? super D, ? super E, ? super F, ? super G, ? super H, ? extends R> fn) { |
| 105 | + return appH.<R>zip(liftA(appA, appB, appC, appD, appE, appF, appG, fn)).coerce(); |
| 106 | + } |
| 107 | + |
| 108 | + |
30 | 109 | public static void main(String[] args) { |
31 | 110 |
|
32 | | - class Foo implements Struct._4<String, Integer, String, Integer> { |
| 111 | + Either<Object, Integer> foo1 = liftA(right(1), right(2), right(3), right(4), (a, b, c, d) -> a + b + c + d); |
| 112 | + System.out.println(foo1); |
| 113 | + |
| 114 | + class Foo implements Struct._4<String, Maybe<Integer>, String, Maybe<Integer>> { |
33 | 115 |
|
34 | 116 | @Override |
35 | | - public Tuple4<String, Integer, String, Integer> unapply() { |
36 | | - return tuple(getBar(), getBaz(), getBar().toUpperCase(), getBaz() * 2); |
| 117 | + public Tuple4<String, Maybe<Integer>, String, Maybe<Integer>> unapply() { |
| 118 | + return tuple(getBar(), getBaz(), getBar().toUpperCase(), getBaz().fmap(x -> x * 2)); |
37 | 119 | } |
38 | 120 |
|
39 | 121 | public String getBar() { |
40 | | - return "foo"; |
| 122 | + return "123"; |
41 | 123 | } |
42 | 124 |
|
43 | | - public Integer getBaz() { |
44 | | - return 1; |
| 125 | + public Maybe<Integer> getBaz() { |
| 126 | + return just(100); |
45 | 127 | } |
46 | 128 | } |
47 | 129 |
|
48 | 130 | Foo foo = new Foo(); |
49 | 131 |
|
| 132 | + Integer match = struct(foo::getBar, foo::getBaz).match(cases( |
| 133 | + of($(eq("123")), $just(), (bar, baz) -> Integer.parseInt(bar) + baz), |
| 134 | + of($(eq("foo")), $__(), (bar, baz) -> baz.orElse(-2)), |
| 135 | + of($(eq("foo")), $(just(1)), (bar, baz) -> baz.orElse(-2)), |
| 136 | + of($(eq("foo")), $just(1), (bar, baz) -> baz), |
| 137 | + of($__(), $__(), (bar, baz) -> -1))); |
50 | 138 |
|
51 | | - Struct._3<String, String, Integer> struct = struct(foo::getBar, foo::getBar, foo::getBaz); |
52 | | - Match.Partial<Tuple3<String, String, Integer>, String> match = cases(of(eq("foo"), __, __, (x, y, z) -> x)); |
53 | | - Maybe<String> blah = struct.match(match); |
54 | | - |
55 | | - Case.DestructuringPredicate<Maybe<Integer>, Integer> aPredicate = $just(eq(1)); |
56 | | - Case.DestructuringPredicate<Maybe<Integer>, Integer> bPredicate = $just(eq(2)); |
57 | | - Case.Partial<Tuple2<Maybe<Integer>, Maybe<Integer>>, Integer> of = of(aPredicate, bPredicate, (x, y) -> x + y); |
58 | | - Maybe<Integer> result = cases(of) |
59 | | - .apply(tuple(just(1), just(2))); |
60 | | - System.out.println(result); |
61 | | - |
62 | | - Maybe<String> apply = match.apply(struct.unapply()); |
63 | | - |
64 | | - System.out.println("blah = " + blah); |
65 | | - |
66 | | - |
| 139 | + System.out.println("match = " + match); |
67 | 140 | } |
68 | 141 | } |
0 commit comments