Skip to content

Commit d36ed8a

Browse files
committed
snapshot
1 parent 7fe05d4 commit d36ed8a

File tree

6 files changed

+263
-23
lines changed

6 files changed

+263
-23
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.jnape.palatable.lambda;
2+
3+
import com.jnape.palatable.lambda.adt.Maybe;
4+
import com.jnape.palatable.lambda.adt.hlist.HList;
5+
import com.jnape.palatable.lambda.adt.hlist.Tuple3;
6+
import com.jnape.palatable.lambda.adt.hlist.Tuple4;
7+
import com.jnape.palatable.lambda.structural.Match;
8+
import com.jnape.palatable.lambda.structural.Struct;
9+
10+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Eq.eq;
11+
import static com.jnape.palatable.lambda.structural.Case.of;
12+
import static com.jnape.palatable.lambda.structural.Cases.cases;
13+
import static com.jnape.palatable.lambda.structural.CatchAll.__;
14+
import static com.jnape.palatable.lambda.structural.Struct.struct;
15+
16+
public class SPike {
17+
18+
public static void main(String[] args) {
19+
20+
class Foo implements Struct._4<String, Integer, String, Integer> {
21+
22+
@Override
23+
public Tuple4<String, Integer, String, Integer> unapply() {
24+
return HList.tuple(getBar(), getBaz(), getBar().toUpperCase(), getBaz() * 2);
25+
}
26+
27+
public String getBar() {
28+
return "foo";
29+
}
30+
31+
public Integer getBaz() {
32+
return 1;
33+
}
34+
}
35+
36+
Foo foo = new Foo();
37+
38+
39+
Struct._3<String, String, Integer> struct = struct(foo::getBar, foo::getBar, foo::getBaz);
40+
Match.Partial<Tuple3<String, String, Integer>, String> match = cases(of(eq("foo"), __, __, (x, y, z) -> x));
41+
Maybe<String> blah = struct.match(match);
42+
43+
Maybe<String> apply = match.apply(struct.unapply());
44+
45+
System.out.println("blah = " + blah);
46+
47+
48+
}
49+
}

src/main/java/com/jnape/palatable/lambda/functions/builtin/fn2/Into3.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public final class Into3<A, B, C, D> implements Fn2<Fn3<? super A, ? super B, ?
1818

1919
private static final Into3 INSTANCE = new Into3();
2020

21+
2122
@Override
2223
public D apply(Fn3<? super A, ? super B, ? super C, ? extends D> fn, Tuple3<A, B, C> tuple) {
2324
return tuple.into(fn);

src/main/java/com/jnape/palatable/lambda/functions/specialized/Predicate.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@ default <Z> Predicate<Z> diMapL(Function<? super Z, ? extends A> fn) {
4848
return Fn1.super.diMapL(fn)::apply;
4949
}
5050

51-
/**
52-
* {@inheritDoc}
53-
*/
54-
@Override
55-
default <Z> Predicate<Z> contraMap(Function<? super Z, ? extends A> fn) {
56-
return Fn1.super.contraMap(fn)::apply;
57-
}
58-
5951
/**
6052
* Override of {@link java.util.function.Predicate#and(java.util.function.Predicate)}, returning an instance of
6153
* <code>Predicate</code> for compatibility. Left-to-right composition.

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

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
55
import com.jnape.palatable.lambda.adt.hlist.SingletonHList;
66
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
7+
import com.jnape.palatable.lambda.adt.hlist.Tuple3;
8+
import com.jnape.palatable.lambda.adt.hlist.Tuple4;
79
import com.jnape.palatable.lambda.functions.Fn1;
8-
import com.jnape.palatable.lambda.functions.Fn2;
10+
import com.jnape.palatable.lambda.functions.Fn3;
11+
import com.jnape.palatable.lambda.functions.Fn4;
912
import com.jnape.palatable.lambda.functions.specialized.Predicate;
1013

14+
import java.util.function.BiFunction;
1115
import java.util.function.Function;
1216

1317
import static com.jnape.palatable.lambda.adt.Maybe.just;
1418
import static com.jnape.palatable.lambda.functions.builtin.fn2.Into.into;
1519
import static com.jnape.palatable.lambda.functions.builtin.fn2.Into1.into1;
20+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Into3.into3;
21+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Into4.into4;
1622

1723
public abstract class Case {
1824

@@ -48,33 +54,77 @@ public R apply(Fields fields) {
4854
}
4955
}
5056

51-
public static <A, B, R> Total<Tuple2<A, B>, R> of(Fn2<? super A, ? super B, ? extends R> fn) {
52-
return new Total<>(into(fn.toBiFunction()));
57+
public static <A, R> Total<SingletonHList<A>, R> of(Function<? super A, ? extends R> fn) {
58+
return new Total<>(into1(fn));
5359
}
5460

55-
public static <A, R> Total<SingletonHList<A>, R> of(CatchAll aPredicate,
56-
Fn1<A, R> fn) {
57-
return new Total<>(into1(fn));
61+
public static <A, B, R> Total<Tuple2<A, B>, R> of(BiFunction<? super A, ? super B, ? extends R> fn) {
62+
return new Total<>(into(fn));
5863
}
5964

60-
//todo: overload that explicitly takes Fn<Fields, R> ?
65+
public static <A, B, C, R> Total<Tuple3<A, B, C>, R> of(Fn3<? super A, ? super B, ? super C, ? extends R> fn) {
66+
return new Total<>(into3(fn));
67+
}
6168

62-
public static <A, R> Partial<SingletonHList<A>, R> of(Predicate<A> pred,
63-
Fn1<A, R> fn) {
69+
public static <A, B, C, D, R> Total<Tuple4<A, B, C, D>, R> of(
70+
Fn4<? super A, ? super B, ? super C, ? super D, ? extends R> fn) {
71+
return new Total<>(into4(fn));
72+
}
6473

65-
return new Partial<>(pred.contraMap(HCons::head), into1(fn));
74+
75+
public static <A, R> Total<SingletonHList<A>, R> of(CatchAll __, Function<? super A, ? extends R> fn) {
76+
return of(fn);
6677
}
6778

6879
public static <A, B, R> Total<Tuple2<A, B>, R> of(CatchAll __,
6980
CatchAll ___,
70-
Fn2<? super A, ? super B, ? extends R> fn) {
71-
return new Total<>(into(fn.toBiFunction()));
81+
BiFunction<? super A, ? super B, ? extends R> fn) {
82+
return of(fn);
83+
}
84+
85+
public static <A, B, C, R> Total<Tuple3<A, B, C>, R> of(CatchAll __,
86+
CatchAll ___,
87+
CatchAll ____,
88+
Fn3<? super A, ? super B, ? super C, ? extends R> fn) {
89+
return Case.<A, B, C, R>of(fn);
90+
}
91+
92+
public static <A, B, C, D, R> Total<Tuple4<A, B, C, D>, R> of(CatchAll __,
93+
CatchAll ___,
94+
CatchAll ____,
95+
CatchAll _____,
96+
Fn4<? super A, ? super B, ? super C, ? super D, ? extends R> fn) {
97+
return Case.<A, B, C, D, R>of(fn);
98+
}
99+
100+
101+
public static <A, R> Partial<SingletonHList<A>, R> of(Predicate<A> pred,
102+
Fn1<A, R> fn) {
103+
return new Partial<>(pred.contraMap(HCons::head), into1(fn));
72104
}
73105

74106
public static <A, B, R> Partial<Tuple2<A, B>, R> of(Predicate<? super A> aPredicate,
75107
Predicate<? super B> bPredicate,
76-
Fn2<? super A, ? super B, ? extends R> fn) {
77-
return new Partial<>(t -> aPredicate.test(t._1()) && bPredicate.test(t._2()),
78-
into(fn.toBiFunction()));
108+
BiFunction<? super A, ? super B, ? extends R> fn) {
109+
return new Partial<>(t -> aPredicate.test(t._1()) && bPredicate.test(t._2()), into(fn));
79110
}
111+
112+
113+
public static <A, B, C, R> Partial<Tuple3<A, B, C>, R> of(Predicate<? super A> aPredicate,
114+
Predicate<? super B> bPredicate,
115+
Predicate<? super C> cPredicate,
116+
Fn3<? super A, ? super B, ? super C, ? extends R> fn) {
117+
return new Partial<>(t -> aPredicate.test(t._1()) && bPredicate.test(t._2()) && cPredicate.test(t._3()),
118+
into3(fn));
119+
}
120+
121+
public static <A, B, C, D, R> Partial<Tuple4<A, B, C, D>, R> of(Predicate<? super A> aPredicate,
122+
Predicate<? super B> bPredicate,
123+
Predicate<? super C> cPredicate,
124+
Predicate<? super D> dPredicate,
125+
Fn4<? super A, ? super B, ? super C, ? super D, ? extends R> fn) {
126+
return new Partial<>(t -> aPredicate.test(t._1()) && bPredicate.test(t._2()) && cPredicate.test(t._3()) && dPredicate.test(t._4()),
127+
into4(fn));
128+
}
129+
80130
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.jnape.palatable.lambda.structural;
22

3+
import com.jnape.palatable.lambda.adt.Maybe;
34
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
5+
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
46

7+
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
58
import static com.jnape.palatable.lambda.functions.builtin.fn2.Cons.cons;
9+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Eq.eq;
10+
import static com.jnape.palatable.lambda.structural.Case.of;
11+
import static com.jnape.palatable.lambda.structural.CatchAll.__;
612
import static com.jnape.palatable.lambda.structural.Match.partial;
713
import static com.jnape.palatable.lambda.structural.Match.total;
14+
import static com.jnape.palatable.lambda.structural.Struct.struct;
815
import static java.util.Arrays.asList;
916
import static java.util.Collections.emptyList;
1017

@@ -13,6 +20,29 @@ public class Cases {
1320
private Cases() {
1421
}
1522

23+
public static void main(String[] args) {
24+
25+
class Foo implements Struct._2<String, Integer> {
26+
String getFoo() {
27+
return "hi";
28+
}
29+
30+
Integer getBar() {
31+
return 1;
32+
}
33+
34+
@Override
35+
public Tuple2<String, Integer> unapply() {
36+
return tuple(getFoo(), getBar());
37+
}
38+
}
39+
40+
Foo foo = new Foo();
41+
42+
Maybe<String> match = struct(foo::getFoo, foo::getBar).match(cases(of(__, eq(1), (foo11, bar1) -> foo11),
43+
of(__, eq(1), (foo11, bar1) -> foo11)));
44+
}
45+
1646
public static <Fields extends HCons, R> Match.Total<Fields, R> cases(Case.Partial<Fields, R> partialCase,
1747
Case.Total<Fields, R> totalCase) {
1848
return cases(partialCase).or(cases(totalCase));
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.jnape.palatable.lambda.structural;
2+
3+
import com.jnape.palatable.lambda.adt.Maybe;
4+
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
5+
import com.jnape.palatable.lambda.adt.hlist.SingletonHList;
6+
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
7+
import com.jnape.palatable.lambda.adt.hlist.Tuple3;
8+
import com.jnape.palatable.lambda.adt.hlist.Tuple4;
9+
import com.jnape.palatable.lambda.adt.hlist.Tuple5;
10+
import com.jnape.palatable.lambda.adt.hlist.Tuple6;
11+
import com.jnape.palatable.lambda.adt.hlist.Tuple7;
12+
import com.jnape.palatable.lambda.adt.hlist.Tuple8;
13+
14+
import java.util.function.Supplier;
15+
16+
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
17+
18+
public interface Struct<Fields extends HCons> {
19+
20+
Fields unapply();
21+
22+
default <R> Maybe<R> match(Match.Partial<Fields, R> match) {
23+
return match.apply(this.unapply());
24+
}
25+
26+
default <R> R match(Match.Total<Fields, R> match) {
27+
return match.apply(this.unapply());
28+
}
29+
30+
static <Fields extends HCons> Struct<Fields> struct(Fields fields) {
31+
return () -> fields;
32+
}
33+
34+
static <Fields extends HCons> Struct<Fields> struct(Supplier<Fields> fieldsSupplier) {
35+
return fieldsSupplier::get;
36+
}
37+
38+
static <A, B> Struct._2<A, B> struct(Supplier<A> aSupplier,
39+
Supplier<B> bSupplier) {
40+
return () -> tuple(aSupplier.get(), bSupplier.get());
41+
}
42+
43+
static <A, B, C> Struct._3<A, B, C> struct(Supplier<A> aSupplier,
44+
Supplier<B> bSupplier,
45+
Supplier<C> cSupplier) {
46+
return () -> tuple(aSupplier.get(), bSupplier.get(), cSupplier.get());
47+
}
48+
49+
static <A, B, C, D> Struct._4<A, B, C, D> struct(Supplier<A> aSupplier,
50+
Supplier<B> bSupplier,
51+
Supplier<C> cSupplier,
52+
Supplier<D> dSupplier) {
53+
return () -> tuple(aSupplier.get(), bSupplier.get(), cSupplier.get(), dSupplier.get());
54+
}
55+
56+
static <A, B, C, D, E> Struct._5<A, B, C, D, E> struct(Supplier<A> aSupplier,
57+
Supplier<B> bSupplier,
58+
Supplier<C> cSupplier,
59+
Supplier<D> dSupplier,
60+
Supplier<E> eSupplier) {
61+
return () -> tuple(aSupplier.get(), bSupplier.get(), cSupplier.get(), dSupplier.get(), eSupplier.get());
62+
}
63+
64+
static <A, B, C, D, E, F> Struct._6<A, B, C, D, E, F> struct(Supplier<A> aSupplier,
65+
Supplier<B> bSupplier,
66+
Supplier<C> cSupplier,
67+
Supplier<D> dSupplier,
68+
Supplier<E> eSupplier,
69+
Supplier<F> fSupplier) {
70+
return () -> tuple(aSupplier.get(), bSupplier.get(), cSupplier.get(), dSupplier.get(), eSupplier.get(), fSupplier.get());
71+
}
72+
73+
static <A, B, C, D, E, F, G> Struct._7<A, B, C, D, E, F, G> struct(Supplier<A> aSupplier,
74+
Supplier<B> bSupplier,
75+
Supplier<C> cSupplier,
76+
Supplier<D> dSupplier,
77+
Supplier<E> eSupplier,
78+
Supplier<F> fSupplier,
79+
Supplier<G> gSupplier) {
80+
return () -> tuple(aSupplier.get(), bSupplier.get(), cSupplier.get(), dSupplier.get(), eSupplier.get(), fSupplier.get(), gSupplier.get());
81+
}
82+
83+
static <A, B, C, D, E, F, G, H> Struct._8<A, B, C, D, E, F, G, H> struct(Supplier<A> aSupplier,
84+
Supplier<B> bSupplier,
85+
Supplier<C> cSupplier,
86+
Supplier<D> dSupplier,
87+
Supplier<E> eSupplier,
88+
Supplier<F> fSupplier,
89+
Supplier<G> gSupplier,
90+
Supplier<H> hSupplier) {
91+
return () -> tuple(aSupplier.get(), bSupplier.get(), cSupplier.get(), dSupplier.get(), eSupplier.get(), fSupplier.get(), gSupplier.get(),
92+
hSupplier.get());
93+
}
94+
95+
interface _1<A> extends Struct<SingletonHList<A>> {
96+
}
97+
98+
interface _2<A, B> extends Struct<Tuple2<A, B>> {
99+
}
100+
101+
interface _3<A, B, C> extends Struct<Tuple3<A, B, C>> {
102+
}
103+
104+
interface _4<A, B, C, D> extends Struct<Tuple4<A, B, C, D>> {
105+
}
106+
107+
interface _5<A, B, C, D, E> extends Struct<Tuple5<A, B, C, D, E>> {
108+
}
109+
110+
interface _6<A, B, C, D, E, F> extends Struct<Tuple6<A, B, C, D, E, F>> {
111+
}
112+
113+
interface _7<A, B, C, D, E, F, G> extends Struct<Tuple7<A, B, C, D, E, F, G>> {
114+
}
115+
116+
interface _8<A, B, C, D, E, F, G, H> extends Struct<Tuple8<A, B, C, D, E, F, G, H>> {
117+
}
118+
}

0 commit comments

Comments
 (0)