Skip to content

Commit 75b1fb7

Browse files
committed
Adding MaybeT#or for choosing the first present effect result
1 parent 269cbbc commit 75b1fb7

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1111

1212
### Added
1313
- `MergeHMaps`, a `Monoid` that merges `HMap`s by merging the values via key-specified `Semigroup`s
14+
- `Id#id` overload that accepts an argument and returns it
15+
- `MaybeT#or`, choose the first `MaybeT` that represents an effect around `just` a value
16+
- `StateMatcher, StateTMatcher, WriterTMatcher`
1417

1518
## [5.1.0] - 2019-10-13
1619
### Changed

src/main/java/com/jnape/palatable/lambda/monad/transformer/builtin/MaybeT.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ public MaybeT<M, A> filter(Fn1<? super A, ? extends Boolean> predicate) {
5555
return maybeT(mma.fmap(ma -> ma.filter(predicate)));
5656
}
5757

58+
/**
59+
* Returns the first {@link MaybeT} that is an effect around {@link Maybe#just(Object) just} a result.
60+
*
61+
* @param other the other {@link MaybeT}
62+
* @return the first present {@link MaybeT}
63+
*/
64+
public MaybeT<M, A> or(MaybeT<M, A> other) {
65+
MonadRec<Maybe<A>, M> mMaybeA = runMaybeT();
66+
return maybeT(mMaybeA.flatMap(maybeA -> maybeA.match(constantly(other.runMaybeT()),
67+
a -> mMaybeA.pure(just(a)))));
68+
}
69+
5870
/**
5971
* {@inheritDoc}
6072
*/
@@ -145,7 +157,7 @@ public <B> MaybeT<M, A> discardR(Applicative<B, MaybeT<M, ?>> appB) {
145157

146158
@Override
147159
public boolean equals(Object other) {
148-
return other instanceof MaybeT<?, ?> && Objects.equals(mma, ((MaybeT) other).mma);
160+
return other instanceof MaybeT<?, ?> && Objects.equals(mma, ((MaybeT<?, ?>) other).mma);
149161
}
150162

151163
@Override

src/test/java/com/jnape/palatable/lambda/monad/transformer/builtin/MaybeTTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import static com.jnape.palatable.lambda.functor.builtin.Identity.pureIdentity;
2828
import static com.jnape.palatable.lambda.functor.builtin.Lazy.lazy;
2929
import static com.jnape.palatable.lambda.io.IO.io;
30-
import static com.jnape.palatable.lambda.monad.transformer.builtin.MaybeT.liftMaybeT;
31-
import static com.jnape.palatable.lambda.monad.transformer.builtin.MaybeT.maybeT;
32-
import static com.jnape.palatable.lambda.monad.transformer.builtin.MaybeT.pureMaybeT;
30+
import static com.jnape.palatable.lambda.monad.transformer.builtin.MaybeT.*;
3331
import static com.jnape.palatable.traitor.framework.Subjects.subjects;
3432
import static org.junit.Assert.assertEquals;
3533

@@ -79,4 +77,16 @@ public void filter() {
7977
assertEquals(maybeT(new Identity<>(just(1))), maybeT.filter(gt(0)));
8078
assertEquals(maybeT(new Identity<>(nothing())), maybeT.filter(lt(0)));
8179
}
80+
81+
@Test
82+
public void orSelectsFirstPresentValueInsideEffect() {
83+
assertEquals(maybeT(new Identity<>(just(1))),
84+
maybeT(new Identity<>(just(1))).or(maybeT(new Identity<>(nothing()))));
85+
86+
assertEquals(maybeT(new Identity<>(just(1))),
87+
maybeT(new Identity<>(nothing())).or(maybeT(new Identity<>(just(1)))));
88+
89+
assertEquals(maybeT(new Identity<>(just(1))),
90+
maybeT(new Identity<>(just(1))).or(maybeT(new Identity<>(just(2)))));
91+
}
8292
}

0 commit comments

Comments
 (0)