Skip to content

Commit a52d18e

Browse files
committed
Adding IdentityT, a monad transformer for Identity
1 parent 5186c11 commit a52d18e

5 files changed

Lines changed: 174 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ might need to be reworked, and subtyping is obviously no longer supported.
2020
- `MonadT`, a general interface representing monad transformers
2121
- `MaybeT`, a monad transformer for `Maybe`
2222
- `EitherT`, a monad transformer for `Either`
23+
- `IdentityT`, a monad transformer for `Identity`
2324
- `Endo`, a monoid formed by `Fn1` under composition
2425
- `State`, the state `Monad`
2526

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ public String toString() {
115115
return "EitherT{melr=" + melr + '}';
116116
}
117117

118+
/**
119+
* Static factory method for lifting a <code>{@link Monad}&lt;{@link Either}&lt;L, R&gt;, M&gt;</code> into an
120+
* {@link EitherT}.
121+
*
122+
* @param melr the {@link Monad}&lt;{@link Either}&lt;L, R&gt;, M&gt;
123+
* @param <M> the outer {@link Monad} unification parameter
124+
* @param <L> the left type
125+
* @param <R> the right type
126+
* @return the {@link EitherT}
127+
*/
118128
public static <M extends Monad<?, M>, L, R> EitherT<M, L, R> eitherT(Monad<Either<L, R>, M> melr) {
119129
return new EitherT<>(melr);
120130
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.jnape.palatable.lambda.monad.transformer.builtin;
2+
3+
import com.jnape.palatable.lambda.functor.Applicative;
4+
import com.jnape.palatable.lambda.functor.builtin.Compose;
5+
import com.jnape.palatable.lambda.functor.builtin.Identity;
6+
import com.jnape.palatable.lambda.functor.builtin.Lazy;
7+
import com.jnape.palatable.lambda.monad.Monad;
8+
import com.jnape.palatable.lambda.monad.transformer.MonadT;
9+
10+
import java.util.Objects;
11+
import java.util.function.Function;
12+
13+
/**
14+
* A {@link MonadT monad transformer} for {@link Identity}.
15+
*
16+
* @param <M> the outer {@link Monad}
17+
* @param <A> the carrier type
18+
*/
19+
public final class IdentityT<M extends Monad<?, M>, A> implements MonadT<M, Identity<?>, A> {
20+
21+
private final Monad<Identity<A>, M> mia;
22+
23+
private IdentityT(Monad<Identity<A>, M> mia) {
24+
this.mia = mia;
25+
}
26+
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
@Override
31+
public <GA extends Monad<A, Identity<?>>, FGA extends Monad<GA, M>> FGA run() {
32+
return mia.<GA>fmap(Identity::coerce).coerce();
33+
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
@Override
39+
public <B> IdentityT<M, B> flatMap(Function<? super A, ? extends Monad<B, MonadT<M, Identity<?>, ?>>> f) {
40+
return identityT(mia.flatMap(identityA -> f.apply(identityA.runIdentity()).<IdentityT<M, B>>coerce().run()));
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
@Override
47+
public <B> IdentityT<M, B> pure(B b) {
48+
return identityT(mia.pure(new Identity<>(b)));
49+
}
50+
51+
/**
52+
* {@inheritDoc}
53+
*/
54+
@Override
55+
public <B> IdentityT<M, B> fmap(Function<? super A, ? extends B> fn) {
56+
return MonadT.super.<B>fmap(fn).coerce();
57+
}
58+
59+
/**
60+
* {@inheritDoc}
61+
*/
62+
@Override
63+
public <B> IdentityT<M, B> zip(Applicative<Function<? super A, ? extends B>, MonadT<M, Identity<?>, ?>> appFn) {
64+
return MonadT.super.zip(appFn).coerce();
65+
}
66+
67+
/**
68+
* {@inheritDoc}
69+
*/
70+
@Override
71+
public <B> Lazy<IdentityT<M, B>> lazyZip(
72+
Lazy<? extends Applicative<Function<? super A, ? extends B>, MonadT<M, Identity<?>, ?>>> lazyAppFn) {
73+
return new Compose<>(mia)
74+
.lazyZip(lazyAppFn.fmap(maybeT -> new Compose<>(
75+
maybeT.<IdentityT<M, Function<? super A, ? extends B>>>coerce()
76+
.<Identity<Function<? super A, ? extends B>>,
77+
Monad<Identity<Function<? super A, ? extends B>>, M>>run())))
78+
.fmap(compose -> identityT(compose.getCompose()));
79+
}
80+
81+
/**
82+
* {@inheritDoc}
83+
*/
84+
@Override
85+
public <B> IdentityT<M, B> discardL(Applicative<B, MonadT<M, Identity<?>, ?>> appB) {
86+
return MonadT.super.discardL(appB).coerce();
87+
}
88+
89+
/**
90+
* {@inheritDoc}
91+
*/
92+
@Override
93+
public <B> IdentityT<M, A> discardR(Applicative<B, MonadT<M, Identity<?>, ?>> appB) {
94+
return MonadT.super.discardR(appB).coerce();
95+
}
96+
97+
@Override
98+
public boolean equals(Object other) {
99+
return other instanceof IdentityT<?, ?> && Objects.equals(mia, ((IdentityT<?, ?>) other).mia);
100+
}
101+
102+
@Override
103+
public int hashCode() {
104+
return Objects.hash(mia);
105+
}
106+
107+
@Override
108+
public String toString() {
109+
return "IdentityT{mia=" + mia + '}';
110+
}
111+
112+
/**
113+
* Static factory method for lifting a <code>{@link Monad}&lt;{@link Identity}&lt;A&gt;, M&gt;</code> into a
114+
* {@link IdentityT}.
115+
*
116+
* @param mia the {@link Monad}&lt;{@link Identity}&lt;A&gt;, M&gt;
117+
* @param <M> the outer {@link Monad} unification parameter
118+
* @param <A> the carrier type
119+
* @return the new {@link IdentityT}.
120+
*/
121+
public static <M extends Monad<?, M>, A> IdentityT<M, A> identityT(Monad<Identity<A>, M> mia) {
122+
return new IdentityT<>(mia);
123+
}
124+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ public String toString() {
116116
}
117117

118118
/**
119-
* Static factory method for lifting a <code>{@link Monad}&lt;{@link Maybe}&lt;A&gt;&gt;</code> into a
119+
* Static factory method for lifting a <code>{@link Monad}&lt;{@link Maybe}&lt;A&gt;, M&gt;</code> into a
120120
* {@link MaybeT}.
121121
*
122-
* @param mma the {@link Monad}&lt;{@link Maybe}&lt;A&gt;&gt;
122+
* @param mma the {@link Monad}&lt;{@link Maybe}&lt;A&gt;, M&gt;
123123
* @param <M> the outer {@link Monad} unification parameter
124124
* @param <A> the carrier type
125125
* @return the {@link MaybeT}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.jnape.palatable.lambda.monad.transformer.builtin;
2+
3+
import com.jnape.palatable.lambda.adt.Maybe;
4+
import com.jnape.palatable.lambda.functor.builtin.Identity;
5+
import com.jnape.palatable.traitor.annotations.TestTraits;
6+
import com.jnape.palatable.traitor.runners.Traits;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import testsupport.traits.ApplicativeLaws;
10+
import testsupport.traits.FunctorLaws;
11+
import testsupport.traits.MonadLaws;
12+
13+
import static com.jnape.palatable.lambda.adt.Maybe.just;
14+
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
15+
import static com.jnape.palatable.lambda.functor.builtin.Lazy.lazy;
16+
import static com.jnape.palatable.lambda.monad.transformer.builtin.IdentityT.identityT;
17+
import static org.junit.Assert.assertEquals;
18+
19+
@RunWith(Traits.class)
20+
public class IdentityTTest {
21+
22+
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, MonadLaws.class})
23+
public IdentityT<Maybe<?>, Integer> testSubject() {
24+
return identityT(just(new Identity<>(1)));
25+
}
26+
27+
@Test
28+
public void lazyZip() {
29+
assertEquals(identityT(just(new Identity<>(2))),
30+
identityT(just(new Identity<>(1)))
31+
.lazyZip(lazy(identityT(just(new Identity<>(x -> x + 1))))).value());
32+
assertEquals(identityT(nothing()),
33+
identityT(nothing()).lazyZip(lazy(() -> {
34+
throw new AssertionError();
35+
})).value());
36+
}
37+
}

0 commit comments

Comments
 (0)