Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbf28fb1

Browse files
committed
Splitting out coproducts interfaces from choice ADTs
1 parent749d16c commitbf28fb1

File tree

18 files changed

+1451
-1028
lines changed

18 files changed

+1451
-1028
lines changed

‎src/main/java/com/jnape/palatable/lambda/adt/Either.java‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
importcom.jnape.palatable.lambda.adt.coproduct.CoProduct2;
44
importcom.jnape.palatable.lambda.functions.specialized.checked.CheckedFn1;
55
importcom.jnape.palatable.lambda.functions.specialized.checked.CheckedSupplier;
6+
importcom.jnape.palatable.lambda.functor.Bifunctor;
7+
importcom.jnape.palatable.lambda.functor.Functor;
68

79
importjava.util.Objects;
810
importjava.util.Optional;
@@ -23,7 +25,7 @@
2325
* @param <L> The left parameter type
2426
* @param <R> The right parameter type
2527
*/
26-
publicabstractclassEither<L,R>implementsCoProduct2<L,R> {
28+
publicabstractclassEither<L,R>implementsCoProduct2<L,R>,Functor<R>,Bifunctor<L,R> {
2729

2830
privateEither() {
2931
}
@@ -146,8 +148,8 @@ public final Either<R, L> invert() {
146148
publicfinalEither<L,R>merge(BiFunction<?superL, ?superL, ?extendsL>leftFn,
147149
BiFunction<?superR, ?superR, ?extendsR>rightFn,
148150
Either<L,R>...others) {
149-
returnfoldLeft((x,y) ->x.match(l1 ->y.<Either<L,R>>match(l2 ->left(leftFn.apply(l1,l2)),r ->left(l1)),
150-
r1 ->y.<Either<L,R>>match(Either::left,r2 ->right(rightFn.apply(r1,r2)))),
151+
returnfoldLeft((x,y) ->x.match(l1 ->y.match(l2 ->left(leftFn.apply(l1,l2)),r ->left(l1)),
152+
r1 ->y.match(Either::left,r2 ->right(rightFn.apply(r1,r2)))),
151153
this,
152154
asList(others));
153155
}
@@ -200,13 +202,13 @@ public final <R2> Either<L, R2> fmap(Function<? super R, ? extends R2> fn) {
200202
@Override
201203
@SuppressWarnings("unchecked")
202204
publicfinal <L2>Either<L2,R>biMapL(Function<?superL, ?extendsL2>fn) {
203-
return (Either<L2,R>)CoProduct2.super.biMapL(fn);
205+
return (Either<L2,R>)Bifunctor.super.biMapL(fn);
204206
}
205207

206208
@Override
207209
@SuppressWarnings("unchecked")
208210
publicfinal <R2>Either<L,R2>biMapR(Function<?superR, ?extendsR2>fn) {
209-
return (Either<L,R2>)CoProduct2.super.biMapR(fn);
211+
return (Either<L,R2>)Bifunctor.super.biMapR(fn);
210212
}
211213

212214
@Override
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
packagecom.jnape.palatable.lambda.adt.choice;
2+
3+
importcom.jnape.palatable.lambda.adt.Either;
4+
importcom.jnape.palatable.lambda.adt.coproduct.CoProduct2;
5+
importcom.jnape.palatable.lambda.functor.Bifunctor;
6+
importcom.jnape.palatable.lambda.functor.Functor;
7+
8+
importjava.util.Objects;
9+
importjava.util.function.Function;
10+
11+
/**
12+
* Canonical ADT representation of {@link CoProduct2} that is also a {@link Functor} and {@link Bifunctor}. Unlike
13+
* {@link Either}, there is no concept of "success" or "failure", so the domain of reasonable function semantics is
14+
* more limited.
15+
*
16+
* @param <A> a type parameter representing the first possible type of this choice
17+
* @param <B> a type parameter representing the second possible type of this choice
18+
* @see Either
19+
* @see Choice3
20+
*/
21+
publicabstractclassChoice2<A,B>implementsCoProduct2<A,B>,Functor<B>,Bifunctor<A,B> {
22+
23+
privateChoice2() {
24+
}
25+
26+
@Override
27+
publicfinal <C>Choice3<A,B,C>diverge() {
28+
returnmatch(Choice3::a,Choice3::b);
29+
}
30+
31+
@Override
32+
publicfinal <C>Choice2<A,C>fmap(Function<?superB, ?extendsC>fn) {
33+
returnbiMapR(fn);
34+
}
35+
36+
@Override
37+
@SuppressWarnings("unchecked")
38+
publicfinal <C>Choice2<C,B>biMapL(Function<?superA, ?extendsC>fn) {
39+
return (Choice2<C,B>)Bifunctor.super.biMapL(fn);
40+
}
41+
42+
@Override
43+
@SuppressWarnings("unchecked")
44+
publicfinal <C>Choice2<A,C>biMapR(Function<?superB, ?extendsC>fn) {
45+
return (Choice2<A,C>)Bifunctor.super.biMapR(fn);
46+
}
47+
48+
@Override
49+
publicfinal <C,D>Choice2<C,D>biMap(Function<?superA, ?extendsC>lFn,
50+
Function<?superB, ?extendsD>rFn) {
51+
returnmatch(a ->a(lFn.apply(a)),b ->b(rFn.apply(b)));
52+
}
53+
54+
/**
55+
* Static factory method for wrapping a value of type <code>A</code> in a {@link Choice2}.
56+
*
57+
* @param a the value
58+
* @param <A> a type parameter representing the first possible type of this choice
59+
* @param <B> a type parameter representing the second possible type of this choice
60+
* @return the wrapped value as a Choice2&lt;A, B&gt;
61+
*/
62+
publicstatic <A,B>Choice2<A,B>a(Aa) {
63+
returnnew_A<>(a);
64+
}
65+
66+
/**
67+
* Static factory method for wrapping a value of type <code>B</code> in a {@link Choice2}.
68+
*
69+
* @param b the value
70+
* @param <A> a type parameter representing the first possible type of this choice
71+
* @param <B> a type parameter representing the second possible type of this choice
72+
* @return the wrapped value as a Choice2&lt;A, B&gt;
73+
*/
74+
publicstatic <A,B>Choice2<A,B>b(Bb) {
75+
returnnew_B<>(b);
76+
}
77+
78+
privatestaticfinalclass_A<A,B>extendsChoice2<A,B> {
79+
80+
privatefinalAa;
81+
82+
private_A(Aa) {
83+
this.a =a;
84+
}
85+
86+
@Override
87+
public <R>Rmatch(Function<?superA, ?extendsR>aFn,Function<?superB, ?extendsR>bFn) {
88+
returnaFn.apply(a);
89+
}
90+
91+
@Override
92+
publicbooleanequals(Objectother) {
93+
returnotherinstanceof_A
94+
&&Objects.equals(a, ((_A)other).a);
95+
}
96+
97+
@Override
98+
publicinthashCode() {
99+
returnObjects.hash(a);
100+
}
101+
102+
@Override
103+
publicStringtoString() {
104+
return"Choice2{" +
105+
"a=" +a +
106+
'}';
107+
}
108+
}
109+
110+
privatestaticfinalclass_B<A,B>extendsChoice2<A,B> {
111+
112+
privatefinalBb;
113+
114+
private_B(Bb) {
115+
this.b =b;
116+
}
117+
118+
@Override
119+
public <R>Rmatch(Function<?superA, ?extendsR>aFn,Function<?superB, ?extendsR>bFn) {
120+
returnbFn.apply(b);
121+
}
122+
123+
@Override
124+
publicbooleanequals(Objectother) {
125+
returnotherinstanceof_B
126+
&&Objects.equals(b, ((_B)other).b);
127+
}
128+
129+
@Override
130+
publicinthashCode() {
131+
returnObjects.hash(b);
132+
}
133+
134+
@Override
135+
publicStringtoString() {
136+
return"Choice2{" +
137+
"b=" +b +
138+
'}';
139+
}
140+
}
141+
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
packagecom.jnape.palatable.lambda.adt.choice;
2+
3+
importcom.jnape.palatable.lambda.adt.coproduct.CoProduct2;
4+
importcom.jnape.palatable.lambda.adt.coproduct.CoProduct3;
5+
importcom.jnape.palatable.lambda.functor.Bifunctor;
6+
importcom.jnape.palatable.lambda.functor.Functor;
7+
8+
importjava.util.Objects;
9+
importjava.util.function.Function;
10+
11+
/**
12+
* Canonical ADT representation of {@link CoProduct3} that is also a {@link Functor} and {@link Bifunctor}.
13+
*
14+
* @param <A> a type parameter representing the first possible type of this choice
15+
* @param <B> a type parameter representing the second possible type of this choice
16+
* @param <C> a type parameter representing the third possible type of this choice
17+
* @see Choice2
18+
* @see Choice4
19+
*/
20+
publicabstractclassChoice3<A,B,C>implementsCoProduct3<A,B,C>,Functor<C>,Bifunctor<B,C> {
21+
22+
privateChoice3() {
23+
}
24+
25+
@Override
26+
publicfinal <D>Choice4<A,B,C,D>diverge() {
27+
returnmatch(Choice4::a,Choice4::b,Choice4::c);
28+
}
29+
30+
@Override
31+
publicfinalChoice2<A,B>converge(Function<?superC, ?extendsCoProduct2<A,B>>convergenceFn) {
32+
returnmatch(Choice2::a,Choice2::b,convergenceFn.andThen(cp2 ->cp2.match(Choice2::a,Choice2::b)));
33+
}
34+
35+
@Override
36+
publicfinal <D>Choice3<A,B,D>fmap(Function<?superC, ?extendsD>fn) {
37+
returnbiMapR(fn);
38+
}
39+
40+
@Override
41+
@SuppressWarnings("unchecked")
42+
publicfinal <D>Choice3<A,D,C>biMapL(Function<?superB, ?extendsD>fn) {
43+
return (Choice3<A,D,C>)Bifunctor.super.biMapL(fn);
44+
}
45+
46+
@Override
47+
@SuppressWarnings("unchecked")
48+
publicfinal <D>Choice3<A,B,D>biMapR(Function<?superC, ?extendsD>fn) {
49+
return (Choice3<A,B,D>)Bifunctor.super.biMapR(fn);
50+
}
51+
52+
@Override
53+
publicfinal <D,E>Choice3<A,D,E>biMap(Function<?superB, ?extendsD>lFn,
54+
Function<?superC, ?extendsE>rFn) {
55+
returnmatch(Choice3::a,b ->b(lFn.apply(b)),c ->c(rFn.apply(c)));
56+
}
57+
58+
/**
59+
* Static factory method for wrapping a value of type <code>A</code> in a {@link Choice3}.
60+
*
61+
* @param a the value
62+
* @param <A> a type parameter representing the first possible type of this choice
63+
* @param <B> a type parameter representing the second possible type of this choice
64+
* @param <C> a type parameter representing the third possible type of this choice
65+
* @return the wrapped value as a Choice3&lt;A, B, C&gt;
66+
*/
67+
publicstatic <A,B,C>Choice3<A,B,C>a(Aa) {
68+
returnnew_A<>(a);
69+
}
70+
71+
/**
72+
* Static factory method for wrapping a value of type <code>A</code> in a {@link Choice3}.
73+
*
74+
* @param b the value
75+
* @param <A> a type parameter representing the first possible type of this choice
76+
* @param <B> a type parameter representing the second possible type of this choice
77+
* @param <C> a type parameter representing the third possible type of this choice
78+
* @return the wrapped value as a Choice3&lt;A, B, C&gt;
79+
*/
80+
publicstatic <A,B,C>Choice3<A,B,C>b(Bb) {
81+
returnnew_B<>(b);
82+
}
83+
84+
/**
85+
* Static factory method for wrapping a value of type <code>A</code> in a {@link Choice3}.
86+
*
87+
* @param c the value
88+
* @param <A> a type parameter representing the first possible type of this choice
89+
* @param <B> a type parameter representing the second possible type of this choice
90+
* @param <C> a type parameter representing the third possible type of this choice
91+
* @return the wrapped value as a Choice3&lt;A, B, C&gt;
92+
*/
93+
publicstatic <A,B,C>Choice3<A,B,C>c(Cc) {
94+
returnnew_C<>(c);
95+
}
96+
97+
privatestaticfinalclass_A<A,B,C>extendsChoice3<A,B,C> {
98+
99+
privatefinalAa;
100+
101+
private_A(Aa) {
102+
this.a =a;
103+
}
104+
105+
@Override
106+
public <R>Rmatch(Function<?superA, ?extendsR>aFn,Function<?superB, ?extendsR>bFn,
107+
Function<?superC, ?extendsR>cFn) {
108+
returnaFn.apply(a);
109+
}
110+
111+
@Override
112+
publicbooleanequals(Objectother) {
113+
returnotherinstanceof_A
114+
&&Objects.equals(a, ((_A)other).a);
115+
}
116+
117+
@Override
118+
publicinthashCode() {
119+
returnObjects.hash(a);
120+
}
121+
122+
@Override
123+
publicStringtoString() {
124+
return"Choice3{" +
125+
"a=" +a +
126+
'}';
127+
}
128+
}
129+
130+
privatestaticfinalclass_B<A,B,C>extendsChoice3<A,B,C> {
131+
132+
privatefinalBb;
133+
134+
private_B(Bb) {
135+
this.b =b;
136+
}
137+
138+
@Override
139+
public <R>Rmatch(Function<?superA, ?extendsR>aFn,Function<?superB, ?extendsR>bFn,
140+
Function<?superC, ?extendsR>cFn) {
141+
returnbFn.apply(b);
142+
}
143+
144+
@Override
145+
publicbooleanequals(Objectother) {
146+
returnotherinstanceof_B
147+
&&Objects.equals(b, ((_B)other).b);
148+
}
149+
150+
@Override
151+
publicinthashCode() {
152+
returnObjects.hash(b);
153+
}
154+
155+
@Override
156+
publicStringtoString() {
157+
return"Choice3{" +
158+
"b=" +b +
159+
'}';
160+
}
161+
}
162+
163+
privatestaticfinalclass_C<A,B,C>extendsChoice3<A,B,C> {
164+
165+
privatefinalCc;
166+
167+
private_C(Cc) {
168+
this.c =c;
169+
}
170+
171+
@Override
172+
public <R>Rmatch(Function<?superA, ?extendsR>aFn,Function<?superB, ?extendsR>bFn,
173+
Function<?superC, ?extendsR>cFn) {
174+
returncFn.apply(c);
175+
}
176+
177+
@Override
178+
publicbooleanequals(Objectother) {
179+
returnotherinstanceof_C
180+
&&Objects.equals(c, ((_C)other).c);
181+
}
182+
183+
@Override
184+
publicinthashCode() {
185+
returnObjects.hash(c);
186+
}
187+
188+
@Override
189+
publicStringtoString() {
190+
return"Choice3{" +
191+
"c=" +c +
192+
'}';
193+
}
194+
}
195+
196+
197+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp