Skip to content

Commit 8115e0a

Browse files
committed
performance improvements for filter and mapLeft
1 parent 753fd91 commit 8115e0a

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/main/java/io/jbock/util/Either.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ public final <R2> Either<L, R2> flatMap(
116116
* @param predicate a function that acts as a filter predicate
117117
* @return filter result
118118
*/
119-
public final Either<L, R> filter(Function<? super R, LeftOptional<? extends L>> predicate) {
120-
return fold(Either::left, r -> narrow(predicate.apply(r).orElseRight(() -> r)));
121-
}
119+
public abstract Either<L, R> filter(Function<? super R, LeftOptional<? extends L>> predicate);
122120

123121
/**
124122
* If this is a Left, returns a Left containing the result of applying the mapper function to the LHS value.
@@ -129,9 +127,7 @@ public final Either<L, R> filter(Function<? super R, LeftOptional<? extends L>>
129127
* @return an equivalent instance if this is a Right, otherwise a Left containing
130128
* the result of applying {@code mapper} to the LHS value
131129
*/
132-
public final <L2> Either<L2, R> mapLeft(Function<? super L, ? extends L2> mapper) {
133-
return fold(l -> left(mapper.apply(l)), Either::right);
134-
}
130+
public abstract <L2> Either<L2, R> mapLeft(Function<? super L, ? extends L2> mapper);
135131

136132
/**
137133
* If this is a Left, returns the result of applying the mapper function to the LHS value.

src/main/java/io/jbock/util/Left.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public Optional<R> getRight() {
3232
return Optional.empty();
3333
}
3434

35+
@Override
36+
public Either<L, R> filter(Function<? super R, LeftOptional<? extends L>> predicate) {
37+
return same();
38+
}
39+
40+
@Override
41+
public <L2> Either<L2, R> mapLeft(Function<? super L, ? extends L2> mapper) {
42+
return create(mapper.apply(value));
43+
}
44+
3545
@Override
3646
public <L2> Either<L2, R> flatMapLeft(Function<? super L, ? extends Either<? extends L2, ? extends R>> mapper) {
3747
return narrow(mapper.apply(value));

src/main/java/io/jbock/util/Right.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ public Optional<R> getRight() {
3232
return Optional.of(value);
3333
}
3434

35+
@Override
36+
public Either<L, R> filter(Function<? super R, LeftOptional<? extends L>> predicate) {
37+
LeftOptional<? extends L> test = predicate.apply(value);
38+
if (test.isEmpty()) {
39+
return same();
40+
}
41+
return Left.create(test.orElseThrow());
42+
}
43+
44+
@Override
45+
public <L2> Either<L2, R> mapLeft(Function<? super L, ? extends L2> mapper) {
46+
return same();
47+
}
48+
3549
@Override
3650
public <L2> Either<L2, R> flatMapLeft(Function<? super L, ? extends Either<? extends L2, ? extends R>> mapper) {
3751
return same();

0 commit comments

Comments
 (0)