Skip to content

Commit 816475e

Browse files
committed
Using Matcher vs Predicate for better destructuring
1 parent d2f2028 commit 816475e

File tree

7 files changed

+373
-154
lines changed

7 files changed

+373
-154
lines changed
Lines changed: 105 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,141 @@
11
package com.jnape.palatable.lambda;
22

3+
import com.jnape.palatable.lambda.adt.Either;
34
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;
65
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;
1013
import com.jnape.palatable.lambda.structural.Struct;
1114

15+
import java.util.function.BiFunction;
16+
import java.util.function.Function;
17+
18+
import static com.jnape.palatable.lambda.adt.Either.right;
1219
import static com.jnape.palatable.lambda.adt.Maybe.just;
1320
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
21+
import static com.jnape.palatable.lambda.functions.Fn2.fn2;
1422
import static com.jnape.palatable.lambda.functions.builtin.fn2.Eq.eq;
1523
import static com.jnape.palatable.lambda.structural.Case.of;
1624
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.$__;
1828
import static com.jnape.palatable.lambda.structural.Struct.struct;
1929

2030
public class Spike {
2131

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();
2442
}
2543

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();
2850
}
2951

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+
30109
public static void main(String[] args) {
31110

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>> {
33115

34116
@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));
37119
}
38120

39121
public String getBar() {
40-
return "foo";
122+
return "123";
41123
}
42124

43-
public Integer getBaz() {
44-
return 1;
125+
public Maybe<Integer> getBaz() {
126+
return just(100);
45127
}
46128
}
47129

48130
Foo foo = new Foo();
49131

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)));
50138

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);
67140
}
68141
}

src/main/java/com/jnape/palatable/lambda/functions/Fn1.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import com.jnape.palatable.lambda.functor.Applicative;
44
import com.jnape.palatable.lambda.functor.Profunctor;
55
import com.jnape.palatable.lambda.monad.Monad;
6+
import com.jnape.palatable.lambda.traversable.Traversable;
67

78
import java.util.function.BiFunction;
89
import java.util.function.Function;
910

1011
import static com.jnape.palatable.lambda.functions.Fn2.fn2;
12+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
1113

1214
/**
1315
* A function taking a single argument. This is the core function type that all other function types extend and

0 commit comments

Comments
 (0)