Skip to content

Commit 2c06e77

Browse files
authored
Merge pull request #260 from slnowak/support-for-either
Fixed for comprehension for javaslang.control.Either
2 parents ac1e7a7 + f444bcf commit 2c06e77

File tree

2 files changed

+127
-10
lines changed

2 files changed

+127
-10
lines changed

cyclops-javaslang/src/main/java/com/aol/cyclops/javaslang/comprehenders/EitherComprehender.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,17 @@
1212
public class EitherComprehender implements ValueComprehender<Either> {
1313

1414
public Object filter(Either t, Predicate p) {
15-
return t.right()
16-
.filter(x -> p.test(x));
15+
return t.filter(p);
1716
}
1817

1918
@Override
2019
public Object map(Either t, Function fn) {
21-
return t.right()
22-
.map(e -> fn.apply(e));
20+
return t.map(fn);
2321
}
2422

2523
@Override
2624
public Object flatMap(Either t, Function fn) {
27-
return t.right()
28-
.flatMap(e -> fn.apply(e));
25+
return t.flatMap(fn);
2926
}
3027

3128
@Override
@@ -44,9 +41,6 @@ public Class getTargetClass() {
4441
}
4542

4643
public Object resolveForCrossTypeFlatMap(Comprehender comp, Either apply) {
47-
if (apply.isRight())
48-
return comp.of(apply.right()
49-
.get());
50-
return comp.empty();
44+
return apply.isRight() ? comp.of(apply.get()) : comp.empty();
5145
}
5246
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.aol.cyclops.javaslang;
2+
3+
import com.aol.cyclops.control.For;
4+
import javaslang.control.Either;
5+
import org.junit.Test;
6+
7+
import static org.hamcrest.Matchers.equalTo;
8+
import static org.junit.Assert.assertThat;
9+
10+
public class EitherComprehensionTest {
11+
12+
/*
13+
def prepareCappuccino(): Either[Exception, String] = for {
14+
ground <- grind("arabica beans")
15+
water <- heatWater(Water(25))
16+
espresso <- brew(ground, water)
17+
foam <- frothMilk("milk")
18+
} yield combine(espresso, foam)
19+
*/
20+
21+
private static final Exception AN_EXCEPTION = new RuntimeException();
22+
23+
@Test
24+
public void shouldComprehendEitherExpressions() throws Exception {
25+
// when
26+
final Either<Exception, String> wrapsCappuccino = For
27+
.iterable(grind("arabica beans"))
28+
.iterable(ground -> heatWater(new Water(25)))
29+
.iterable(ground -> water -> brew(ground, water))
30+
.iterable(ground -> water -> espresso -> frothMilk("milk"))
31+
.yield(ground -> water -> espresso -> foam -> combine(espresso, foam))
32+
.unwrap();
33+
34+
// then
35+
assertThat(
36+
wrapsCappuccino.get(),
37+
equalTo("cappuccino")
38+
);
39+
}
40+
41+
@Test
42+
public void shouldComprehendEitherExpressionsWithFailedOne() throws Exception {
43+
// when
44+
final Either<Exception, String> wrapsException = For
45+
.iterable(grind("arabica beans"))
46+
.iterable(ground -> failToHeatWater(new Water(25)))
47+
.iterable(ground -> water -> brew(ground, water))
48+
.iterable(ground -> water -> espresso -> frothMilk("milk"))
49+
.yield(ground -> water -> espresso -> foam -> combine(espresso, foam))
50+
.unwrap();
51+
52+
// then
53+
assertThat(
54+
wrapsException.getLeft(),
55+
equalTo(AN_EXCEPTION)
56+
);
57+
}
58+
59+
@Test
60+
public void shouldComprehendEitherProjectionExpressions() throws Exception {
61+
// when
62+
final Either.RightProjection<Exception, String> wrapsCappuccino = For
63+
.iterable(rightProjectionOf(grind("arabica beans")))
64+
.iterable(ground -> rightProjectionOf(heatWater(new Water(25))))
65+
.iterable(ground -> water -> rightProjectionOf(brew(ground, water)))
66+
.iterable(ground -> water -> espresso -> rightProjectionOf(frothMilk("milk")))
67+
.yield(ground -> water -> espresso -> foam -> combine(espresso, foam))
68+
.unwrap();
69+
70+
// then
71+
assertThat(
72+
wrapsCappuccino.get(),
73+
equalTo("cappuccino")
74+
);
75+
}
76+
77+
@Test
78+
public void shouldComprehendEitherExpressionsMixedWithEitherProjectionExpressions() throws Exception {
79+
// when
80+
final Either<Exception, String> wrapsCappuccino = For
81+
.iterable(grind("arabica beans"))
82+
.iterable(ground -> heatWater(new Water(25)))
83+
.iterable(ground -> water -> brew(ground, water))
84+
.iterable(ground -> water -> espresso -> rightProjectionOf(frothMilk("milk")))
85+
.yield(ground -> water -> espresso -> foam -> combine(espresso, foam))
86+
.unwrap();
87+
88+
// then
89+
assertThat(
90+
wrapsCappuccino.get(),
91+
equalTo("cappuccino")
92+
);
93+
}
94+
95+
96+
Either<Exception, String> grind(String beans) {
97+
return Either.right("ground " + beans);
98+
}
99+
100+
Either<Exception, Water> heatWater(Water water) {
101+
return Either.right(water.withTemperature(85));
102+
}
103+
104+
Either<Exception, Water> failToHeatWater(Water water) {
105+
return Either.left(AN_EXCEPTION);
106+
}
107+
108+
Either<Exception, String> brew(String coffee, Water heatedWater) {
109+
return Either.right("espresso");
110+
}
111+
112+
Either<Exception, String> frothMilk(String milk) {
113+
return Either.right("frothed " + milk);
114+
}
115+
116+
String combine(String espresso, String frothedMilk) {
117+
return "cappuccino";
118+
}
119+
120+
private static <L, R> Either.RightProjection<L, R> rightProjectionOf(Either<L, R> either) {
121+
return either.right();
122+
}
123+
}

0 commit comments

Comments
 (0)