Skip to content

Commit d2f2028

Browse files
committed
destructuring predicate spike
1 parent d36ed8a commit d2f2028

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

src/main/java/com/jnape/palatable/lambda/SPike.java renamed to src/main/java/com/jnape/palatable/lambda/Spike.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
package com.jnape.palatable.lambda;
22

33
import com.jnape.palatable.lambda.adt.Maybe;
4-
import com.jnape.palatable.lambda.adt.hlist.HList;
4+
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
55
import com.jnape.palatable.lambda.adt.hlist.Tuple3;
66
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;
79
import com.jnape.palatable.lambda.structural.Match;
810
import com.jnape.palatable.lambda.structural.Struct;
911

12+
import static com.jnape.palatable.lambda.adt.Maybe.just;
13+
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
1014
import static com.jnape.palatable.lambda.functions.builtin.fn2.Eq.eq;
1115
import static com.jnape.palatable.lambda.structural.Case.of;
1216
import static com.jnape.palatable.lambda.structural.Cases.cases;
1317
import static com.jnape.palatable.lambda.structural.CatchAll.__;
1418
import static com.jnape.palatable.lambda.structural.Struct.struct;
1519

16-
public class SPike {
20+
public class Spike {
21+
22+
public static <A> Case.DestructuringPredicate<Maybe<A>, A> $just(Predicate<A> predicate) {
23+
return m -> m.filter(predicate);
24+
}
25+
26+
public static <A> Case.DestructuringPredicate<Maybe<A>, A> $just(A a) {
27+
return $just(eq(a));
28+
}
1729

1830
public static void main(String[] args) {
1931

2032
class Foo implements Struct._4<String, Integer, String, Integer> {
2133

2234
@Override
2335
public Tuple4<String, Integer, String, Integer> unapply() {
24-
return HList.tuple(getBar(), getBaz(), getBar().toUpperCase(), getBaz() * 2);
36+
return tuple(getBar(), getBaz(), getBar().toUpperCase(), getBaz() * 2);
2537
}
2638

2739
public String getBar() {
@@ -40,6 +52,13 @@ public Integer getBaz() {
4052
Match.Partial<Tuple3<String, String, Integer>, String> match = cases(of(eq("foo"), __, __, (x, y, z) -> x));
4153
Maybe<String> blah = struct.match(match);
4254

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+
4362
Maybe<String> apply = match.apply(struct.unapply());
4463

4564
System.out.println("blah = " + blah);

src/main/java/com/jnape/palatable/lambda/structural/Case.java

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.function.Function;
1616

1717
import static com.jnape.palatable.lambda.adt.Maybe.just;
18+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
1819
import static com.jnape.palatable.lambda.functions.builtin.fn2.Into.into;
1920
import static com.jnape.palatable.lambda.functions.builtin.fn2.Into1.into1;
2021
import static com.jnape.palatable.lambda.functions.builtin.fn2.Into3.into3;
@@ -103,12 +104,26 @@ public static <A, R> Partial<SingletonHList<A>, R> of(Predicate<A> pred,
103104
return new Partial<>(pred.contraMap(HCons::head), into1(fn));
104105
}
105106

106-
public static <A, B, R> Partial<Tuple2<A, B>, R> of(Predicate<? super A> aPredicate,
107-
Predicate<? super B> bPredicate,
108-
BiFunction<? super A, ? super B, ? extends R> fn) {
109-
return new Partial<>(t -> aPredicate.test(t._1()) && bPredicate.test(t._2()), into(fn));
107+
// public static <A, B, R> Partial<Tuple2<A, B>, R> of(Predicate<? super A> aPredicate,
108+
// Predicate<? super B> bPredicate,
109+
// BiFunction<? super A, ? super B, ? extends R> fn) {
110+
// return new Partial<>(t -> aPredicate.test(t._1()) && bPredicate.test(t._2()), into(fn));
111+
// }
112+
113+
public static <A, APrime, B, BPrime, R> Partial<Tuple2<A, B>, R>of(Predicate<? super A> x,
114+
Predicate<? super B> y,
115+
BiFunction<? super A, ? super B, ? extends R> z) {
116+
throw new UnsupportedOperationException();
110117
}
111118

119+
public static <A, APrime, B, BPrime, R> Partial<Tuple2<A, B>, R> of(
120+
DestructuringPredicate<? super A, ? extends APrime> aPredicate,
121+
DestructuringPredicate<? super B, ? extends BPrime> bPredicate,
122+
BiFunction<? super APrime, ? super BPrime, ? extends R> fn) {
123+
return new Partial<>(t -> aPredicate.test(t._1()) && bPredicate.test(t._2()),
124+
into((a, b) -> fn.apply(aPredicate.destructure(a).orElse(null),
125+
bPredicate.destructure(b).orElse(null))));
126+
}
112127

113128
public static <A, B, C, R> Partial<Tuple3<A, B, C>, R> of(Predicate<? super A> aPredicate,
114129
Predicate<? super B> bPredicate,
@@ -118,6 +133,36 @@ public static <A, B, C, R> Partial<Tuple3<A, B, C>, R> of(Predicate<? super A> a
118133
into3(fn));
119134
}
120135

136+
@FunctionalInterface
137+
public static interface DestructuringPredicate<A, B> extends Predicate<A> {
138+
Maybe<B> destructure(A a);
139+
140+
@Override
141+
default Boolean apply(A a) {
142+
return destructure(a).fmap(constantly(true)).orElse(false);
143+
}
144+
}
145+
146+
public static <A, APrime, B, C, R> Partial<Tuple3<A, B, C>, R> of(
147+
DestructuringPredicate<? super A, ? extends APrime> aPredicate,
148+
Predicate<? super B> bPredicate,
149+
Predicate<? super C> cPredicate,
150+
Fn3<? super APrime, ? super B, ? super C, ? extends R> fn) {
151+
return new Partial<>(t -> aPredicate.test(t._1()) && bPredicate.test(t._2()) && cPredicate.test(t._3()),
152+
into3((ap, b, c) -> fn.apply(aPredicate.destructure(ap).orElseGet(null), b, c)));
153+
}
154+
155+
public static <A, APrime, B, BPrime, C, R> Partial<Tuple3<A, B, C>, R> of(
156+
DestructuringPredicate<? super A, ? extends APrime> aPredicate,
157+
DestructuringPredicate<? super B, ? extends BPrime> bPredicate,
158+
Predicate<? super C> cPredicate,
159+
Fn3<? super APrime, ? super BPrime, ? super C, ? extends R> fn) {
160+
return new Partial<>(t -> aPredicate.test(t._1()) && bPredicate.test(t._2()) && cPredicate.test(t._3()),
161+
into3((a, b, c) -> fn.apply(aPredicate.destructure(a).orElseGet(null),
162+
bPredicate.destructure(b).orElseGet(null),
163+
c)));
164+
}
165+
121166
public static <A, B, C, D, R> Partial<Tuple4<A, B, C, D>, R> of(Predicate<? super A> aPredicate,
122167
Predicate<? super B> bPredicate,
123168
Predicate<? super C> cPredicate,

src/main/java/com/jnape/palatable/lambda/structural/Cases.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public Tuple2<String, Integer> unapply() {
3939

4040
Foo foo = new Foo();
4141

42-
Maybe<String> match = struct(foo::getFoo, foo::getBar).match(cases(of(__, eq(1), (foo11, bar1) -> foo11),
43-
of(__, eq(1), (foo11, bar1) -> foo11)));
42+
// Maybe<String> match = struct(foo::getFoo, foo::getBar).match(cases(of(__, eq(1), (foo11, bar1) -> foo11),
43+
// of(__, eq(1), (foo11, bar1) -> foo11)));
4444
}
4545

4646
public static <Fields extends HCons, R> Match.Total<Fields, R> cases(Case.Partial<Fields, R> partialCase,

0 commit comments

Comments
 (0)