Skip to content

Commit 1f5b9f0

Browse files
committed
WriterT retains a Pure m instance
1 parent 8e95a1d commit 1f5b9f0

File tree

1 file changed

+11
-9
lines changed
  • src/main/java/com/jnape/palatable/lambda/monad/transformer/builtin

1 file changed

+11
-9
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ public final class WriterT<W, M extends MonadRec<?, M>, A> implements
3434
MonadWriter<W, A, WriterT<W, M, ?>>,
3535
MonadT<M, A, WriterT<W, M, ?>, WriterT<W, ?, ?>> {
3636

37+
private final Pure<M> pureM;
3738
private final Fn1<? super Monoid<W>, ? extends MonadRec<Tuple2<A, W>, M>> writerFn;
3839

39-
private WriterT(Fn1<? super Monoid<W>, ? extends MonadRec<Tuple2<A, W>, M>> writerFn) {
40+
private WriterT(Pure<M> pureM, Fn1<? super Monoid<W>, ? extends MonadRec<Tuple2<A, W>, M>> writerFn) {
41+
this.pureM = pureM;
4042
this.writerFn = writerFn;
4143
}
4244

@@ -82,15 +84,15 @@ public <MW extends MonadRec<W, M>> MW execWriterT(Monoid<W> monoid) {
8284
*/
8385
@Override
8486
public <B> WriterT<W, M, Tuple2<A, B>> listens(Fn1<? super W, ? extends B> fn) {
85-
return new WriterT<>(writerFn.fmap(m -> m.fmap(into((a, w) -> both(both(constantly(a), fn), id(), w)))));
87+
return new WriterT<>(pureM, writerFn.fmap(m -> m.fmap(into((a, w) -> both(both(constantly(a), fn), id(), w)))));
8688
}
8789

8890
/**
8991
* {@inheritDoc}
9092
*/
9193
@Override
9294
public WriterT<W, M, A> censor(Fn1<? super W, ? extends W> fn) {
93-
return new WriterT<>(writerFn.fmap(mt -> mt.fmap(t -> t.fmap(fn))));
95+
return new WriterT<>(pureM, writerFn.fmap(mt -> mt.fmap(t -> t.fmap(fn))));
9496
}
9597

9698
/**
@@ -107,7 +109,7 @@ public <B, N extends MonadRec<?, N>> WriterT<W, N, B> lift(MonadRec<B, N> mb) {
107109
@Override
108110
public <B> WriterT<W, M, B> trampolineM(
109111
Fn1<? super A, ? extends MonadRec<RecursiveResult<A, B>, WriterT<W, M, ?>>> fn) {
110-
return new WriterT<>(monoid -> runWriterT(monoid).trampolineM(into((a, w) -> fn.apply(a)
112+
return new WriterT<>(pureM, monoid -> runWriterT(monoid).trampolineM(into((a, w) -> fn.apply(a)
111113
.<WriterT<W, M, RecursiveResult<A, B>>>coerce()
112114
.runWriterT(monoid).fmap(t -> t.fmap(monoid.apply(w)))
113115
.fmap(into((aOrB, w_) -> aOrB.biMap(a_ -> tuple(a_, w_), b -> tuple(b, w_)))))));
@@ -126,15 +128,15 @@ public <B> WriterT<W, M, B> fmap(Fn1<? super A, ? extends B> fn) {
126128
*/
127129
@Override
128130
public <B> WriterT<W, M, B> pure(B b) {
129-
return new WriterT<>(m -> runWriterT(m).pure(tuple(b, m.identity())));
131+
return new WriterT<>(pureM, m -> pureM.apply(tuple(b, m.identity())));
130132
}
131133

132134
/**
133135
* {@inheritDoc}
134136
*/
135137
@Override
136138
public <B> WriterT<W, M, B> flatMap(Fn1<? super A, ? extends Monad<B, WriterT<W, M, ?>>> f) {
137-
return new WriterT<>(monoid -> writerFn.apply(monoid)
139+
return new WriterT<>(pureM, monoid -> writerFn.apply(monoid)
138140
.flatMap(into((a, w) -> f.apply(a).<WriterT<W, M, B>>coerce().runWriterT(monoid)
139141
.fmap(t -> t.fmap(monoid.apply(w))))));
140142
}
@@ -144,7 +146,7 @@ public <B> WriterT<W, M, B> flatMap(Fn1<? super A, ? extends Monad<B, WriterT<W,
144146
*/
145147
@Override
146148
public <B> WriterT<W, M, B> zip(Applicative<Fn1<? super A, ? extends B>, WriterT<W, M, ?>> appFn) {
147-
return new WriterT<>(monoid -> runWriterT(monoid)
149+
return new WriterT<>(pureM, monoid -> runWriterT(monoid)
148150
.zip(appFn.<WriterT<W, M, Fn1<? super A, ? extends B>>>coerce().runWriterT(monoid)
149151
.fmap(into((f, y) -> into((a, x) -> tuple(f.apply(a), monoid.apply(x, y)))))));
150152
}
@@ -196,7 +198,7 @@ public static <W, M extends MonadRec<?, M>> WriterT<W, M, Unit> tell(MonadRec<W,
196198
* @return the {@link WriterT}
197199
*/
198200
public static <W, M extends MonadRec<?, M>, A> WriterT<W, M, A> listen(MonadRec<A, M> ma) {
199-
return new WriterT<>(monoid -> ma.fmap(a -> tuple(a, monoid.identity())));
201+
return new WriterT<>(Pure.of(ma), monoid -> ma.fmap(a -> tuple(a, monoid.identity())));
200202
}
201203

202204
/**
@@ -209,7 +211,7 @@ public static <W, M extends MonadRec<?, M>, A> WriterT<W, M, A> listen(MonadRec<
209211
* @return the {@link WriterT}
210212
*/
211213
public static <W, M extends MonadRec<?, M>, A> WriterT<W, M, A> writerT(MonadRec<Tuple2<A, W>, M> maw) {
212-
return new WriterT<>(constantly(maw));
214+
return new WriterT<>(Pure.of(maw), constantly(maw));
213215
}
214216

215217
/**

0 commit comments

Comments
 (0)