Skip to content

Commit b598a23

Browse files
committed
Adding Either#filter overload supporting a function instead of supplier
1 parent 4014b80 commit b598a23

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1313
- `CoProduct5#diverge` and `Choice5#diverge`
1414
- `Into3` through `Into8`, for applying a `Tuple*` to an `Fn*`
1515
- `Times`, for successively accumulating a result by iterating a function over a value some number of times
16-
- `Slide`, for "sliding" a window of some number of elements across an `Iterable`
16+
- `Slide`, for "sliding" a window of some number of elements across an `Iterable`
17+
- `Either#filter` overload supporting a function from `R` to `L` in the failing predicate case
1718

1819
### Removed
1920
- `Either#toOptional`, deprecated in previous release

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public final <T extends Throwable> R orThrow(Function<? super L, ? extends T> th
7979
}
8080

8181
/**
82-
* If this is a right value, apply pred to it. If the result is true, return the same value; otherwise, return the
83-
* result of leftSupplier wrapped as a left value.
82+
* If this is a right value, apply <code>pred</code> to it. If the result is <code>true</code>, return the same
83+
* value; otherwise, return the result of <code>leftSupplier</code> wrapped as a left value.
8484
* <p>
8585
* If this is a left value, return it.
8686
*
@@ -90,7 +90,20 @@ public final <T extends Throwable> R orThrow(Function<? super L, ? extends T> th
9090
* a left
9191
*/
9292
public final Either<L, R> filter(Function<? super R, Boolean> pred, Supplier<L> leftSupplier) {
93-
return flatMap(r -> pred.apply(r) ? right(r) : left(leftSupplier.get()));
93+
return filter(pred, __ -> leftSupplier.get());
94+
}
95+
96+
/**
97+
* If this is a right value, apply <code>pred</code> to it. If the result is <code>true</code>, return the same
98+
* value; otherwise, return the results of applying the right value to <code>leftFn</code> wrapped as a left value.
99+
*
100+
* @param pred the predicate to apply to a right value
101+
* @param leftFn the function from the right value to a left value if pred fails
102+
* @return this is a left value or a right value that pred matches; otherwise, the result of leftFn applied to the
103+
* right value, wrapped in a left
104+
*/
105+
public final Either<L, R> filter(Function<? super R, Boolean> pred, Function<? super R, ? extends L> leftFn) {
106+
return flatMap(r -> pred.apply(r) ? right(r) : left(leftFn.apply(r)));
94107
}
95108

96109
/**

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ public void filterLiftsRight() {
8989
assertThat(right.filter(x -> false, () -> "bar"), is(left("bar")));
9090
}
9191

92+
@Test
93+
public void filterSupportsFunctionFromRToL() {
94+
Either<String, Integer> left = left("foo");
95+
Either<String, Integer> right = right(1);
96+
97+
assertThat(left.filter(x -> true, Object::toString), is(left));
98+
assertThat(left.filter(x -> false, Object::toString), is(left));
99+
assertThat(right.filter(x -> true, Object::toString), is(right));
100+
assertThat(right.filter(x -> false, Object::toString), is(left("1")));
101+
}
102+
92103
@Test
93104
public void monadicFlatMapLiftsRightAndFlattensBackToEither() {
94105
Either<String, Integer> left = left("foo");

0 commit comments

Comments
 (0)