Skip to content

Commit f978218

Browse files
committed
deprecating dyadic Either#flatMap in favor of match
1 parent 36f8abd commit f978218

File tree

6 files changed

+12
-18
lines changed

6 files changed

+12
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
2525

2626
### Deprecated
2727
- `AddAll` semigroup, in favor of the monoid that no longer mutates any argument
28+
- Dyadic `Either#flatMap()`, in favor of `Either#match`
2829

2930
## [3.1.0] - 2018-07-16
3031
### Added

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public final Either<L, R> filter(Function<? super R, ? extends Boolean> pred,
123123
*/
124124
@Override
125125
public <R2> Either<L, R2> flatMap(Function<? super R, ? extends Monad<R2, Either<L, ?>>> rightFn) {
126-
return flatMap(Either::left, rightFn.andThen(Applicative::coerce));
126+
return match(Either::left, rightFn.andThen(Applicative::coerce));
127127
}
128128

129129
/**
@@ -136,15 +136,17 @@ public <R2> Either<L, R2> flatMap(Function<? super R, ? extends Monad<R2, Either
136136
* @param <L2> the new left parameter type
137137
* @param <R2> the new right parameter type
138138
* @return the result of either rightFn or leftFn, depending on whether this is a right or a left
139+
* @deprecated in favor of {@link Either#match(Function, Function)}
139140
*/
141+
@Deprecated
140142
public final <L2, R2> Either<L2, R2> flatMap(Function<? super L, ? extends Either<L2, R2>> leftFn,
141143
Function<? super R, ? extends Either<L2, R2>> rightFn) {
142144
return match(leftFn, rightFn);
143145
}
144146

145147
@Override
146148
public final Either<R, L> invert() {
147-
return flatMap(Either::right, Either::left);
149+
return match(Either::right, Either::left);
148150
}
149151

150152
/**

src/main/java/com/jnape/palatable/lambda/semigroup/builtin/LeftAll.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private LeftAll() {
3737

3838
@Override
3939
public Semigroup<Either<L, R>> apply(Semigroup<L> lSemigroup) {
40-
return (x, y) -> x.flatMap(xL -> y.flatMap(yL -> left(lSemigroup.apply(xL, yL)), Either::right), Either::right);
40+
return (x, y) -> x.match(xL -> y.match(yL -> left(lSemigroup.apply(xL, yL)), Either::right), Either::right);
4141
}
4242

4343
@SuppressWarnings("unchecked")

src/main/java/com/jnape/palatable/lambda/semigroup/builtin/LeftAny.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ private LeftAny() {
3636

3737
@Override
3838
public Semigroup<Either<L, R>> apply(Semigroup<L> lSemigroup) {
39-
return (x, y) -> x.flatMap(xL -> y.flatMap(yL -> left(lSemigroup.apply(xL, yL)),
40-
yR -> left(xL)),
41-
xR -> y);
39+
return (x, y) -> x.match(xL -> y.match(yL -> left(lSemigroup.apply(xL, yL)),
40+
yR -> left(xL)),
41+
xR -> y);
4242
}
4343

4444
@SuppressWarnings("unchecked")

src/main/java/com/jnape/palatable/lambda/semigroup/builtin/RightAny.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ private RightAny() {
3737

3838
@Override
3939
public Semigroup<Either<L, R>> apply(Semigroup<R> rSemigroup) {
40-
return (x, y) -> x.flatMap(constantly(y),
41-
xR -> y.flatMap(constantly(right(xR)),
42-
rSemigroup.apply(xR).andThen(Either::right)));
40+
return (x, y) -> x.match(constantly(y),
41+
xR -> y.match(constantly(right(xR)),
42+
rSemigroup.apply(xR).andThen(Either::right)));
4343
}
4444

4545
@SuppressWarnings("unchecked")

src/test/java/com/jnape/palatable/lambda/adt/EitherTest.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,6 @@ public void monadicFlatMapLiftsRightAndFlattensBackToEither() {
110110
assertThat(right.flatMap(r -> right(r + 1)), is(right(2)));
111111
}
112112

113-
@Test
114-
public void dyadicFlatMapDuallyLiftsAndFlattensBackToEither() {
115-
Either<String, Integer> left = left("foo");
116-
Either<String, Integer> right = right(1);
117-
118-
assertThat(left.flatMap(l -> left(l + "bar"), r -> right(r + 1)), is(left("foobar")));
119-
assertThat(right.flatMap(l -> left(l + "bar"), r -> right(r + 1)), is(right(2)));
120-
}
121-
122113
@Test
123114
public void mergeDuallyLiftsAndCombinesBiasingLeft() {
124115
Either<String, Integer> left1 = left("foo");

0 commit comments

Comments
 (0)