@@ -30,10 +30,12 @@ public final class ReaderT<R, M extends MonadRec<?, M>, A> implements
30
30
Cartesian <R , A , ReaderT <?, M , ?>>,
31
31
MonadT <M , A , ReaderT <R , M , ?>, ReaderT <R , ?, ?>> {
32
32
33
+ private final Pure <M > pureM ;
33
34
private final Fn1 <? super R , ? extends MonadRec <A , M >> f ;
34
35
35
- private ReaderT (Fn1 <? super R , ? extends MonadRec <A , M >> f ) {
36
- this .f = f ;
36
+ private ReaderT (Pure <M > pureM , Fn1 <? super R , ? extends MonadRec <A , M >> f ) {
37
+ this .pureM = pureM ;
38
+ this .f = f ;
37
39
}
38
40
39
41
/**
@@ -50,15 +52,16 @@ public <MA extends MonadRec<A, M>> MA runReaderT(R r) {
50
52
/**
51
53
* Map the current {@link Monad monadic} embedding to a new one in a potentially different {@link Monad}.
52
54
*
53
- * @param fn the function
54
- * @param <MA> the currently embedded {@link Monad}
55
- * @param <N> the new {@link Monad} witness
56
- * @param <B> the new carrier type
55
+ * @param fn the function
56
+ * @param pureN the new {@link MonadRec monad's} {@link Pure} instance
57
+ * @param <MA> the currently embedded {@link Monad}
58
+ * @param <N> the new {@link Monad} witness
59
+ * @param <B> the new carrier type
57
60
* @return the mapped {@link ReaderT}
58
61
*/
59
62
public <MA extends MonadRec <A , M >, N extends MonadRec <?, N >, B > ReaderT <R , N , B > mapReaderT (
60
- Fn1 <? super MA , ? extends MonadRec <B , N >> fn ) {
61
- return readerT (r -> fn .apply (runReaderT (r ).coerce ()));
63
+ Fn1 <? super MA , ? extends MonadRec <B , N >> fn , Pure < N > pureN ) {
64
+ return readerT (r -> fn .apply (runReaderT (r ).coerce ()), pureN );
62
65
}
63
66
64
67
/**
@@ -70,7 +73,7 @@ public <MA extends MonadRec<A, M>, N extends MonadRec<?, N>, B> ReaderT<R, N, B>
70
73
* @return the composed {@link ReaderT}
71
74
*/
72
75
public <B > ReaderT <R , M , B > and (ReaderT <A , M , B > amb ) {
73
- return readerT (r -> runReaderT (r ).flatMap (amb ::runReaderT ));
76
+ return readerT (r -> runReaderT (r ).flatMap (amb ::runReaderT ), pureM );
74
77
}
75
78
76
79
/**
@@ -94,7 +97,7 @@ public <B, N extends MonadRec<?, N>> ReaderT<R, N, B> lift(MonadRec<B, N> mb) {
94
97
*/
95
98
@ Override
96
99
public <B > ReaderT <R , M , B > flatMap (Fn1 <? super A , ? extends Monad <B , ReaderT <R , M , ?>>> f ) {
97
- return readerT (r -> runReaderT (r ).flatMap (a -> f .apply (a ).<ReaderT <R , M , B >>coerce ().runReaderT (r )));
100
+ return readerT (r -> runReaderT (r ).flatMap (a -> f .apply (a ).<ReaderT <R , M , B >>coerce ().runReaderT (r )), pureM );
98
101
}
99
102
100
103
/**
@@ -104,15 +107,16 @@ public <B> ReaderT<R, M, B> flatMap(Fn1<? super A, ? extends Monad<B, ReaderT<R,
104
107
public <B > ReaderT <R , M , B > trampolineM (
105
108
Fn1 <? super A , ? extends MonadRec <RecursiveResult <A , B >, ReaderT <R , M , ?>>> fn ) {
106
109
return readerT (r -> runReaderT (r ).trampolineM (a -> fn .apply (a ).<ReaderT <R , M , RecursiveResult <A , B >>>coerce ()
107
- .runReaderT (r )));
110
+ .runReaderT (r )), pureM );
108
111
}
109
112
110
113
/**
111
114
* {@inheritDoc}
112
115
*/
113
116
@ Override
114
117
public <B > ReaderT <R , M , B > pure (B b ) {
115
- return readerT (r -> runReaderT (r ).pure (b ));
118
+ MonadRec <B , M > mb = pureM .apply (b );
119
+ return readerT (r -> mb , pureM );
116
120
}
117
121
118
122
/**
@@ -128,7 +132,7 @@ public <B> ReaderT<R, M, B> fmap(Fn1<? super A, ? extends B> fn) {
128
132
*/
129
133
@ Override
130
134
public <B > ReaderT <R , M , B > zip (Applicative <Fn1 <? super A , ? extends B >, ReaderT <R , M , ?>> appFn ) {
131
- return readerT (r -> f .apply (r ).zip (appFn .<ReaderT <R , M , Fn1 <? super A , ? extends B >>>coerce ().runReaderT (r )));
135
+ return readerT (r -> f .apply (r ).zip (appFn .<ReaderT <R , M , Fn1 <? super A , ? extends B >>>coerce ().runReaderT (r )), pureM );
132
136
}
133
137
134
138
/**
@@ -161,7 +165,7 @@ public <B> ReaderT<R, M, A> discardR(Applicative<B, ReaderT<R, M, ?>> appB) {
161
165
*/
162
166
@ Override
163
167
public <Q , B > ReaderT <Q , M , B > diMap (Fn1 <? super Q , ? extends R > lFn , Fn1 <? super A , ? extends B > rFn ) {
164
- return readerT (q -> runReaderT (lFn .apply (q )).fmap (rFn ));
168
+ return readerT (q -> runReaderT (lFn .apply (q )).fmap (rFn ), pureM );
165
169
}
166
170
167
171
/**
@@ -193,7 +197,7 @@ public <Q> ReaderT<Q, M, A> contraMap(Fn1<? super Q, ? extends R> fn) {
193
197
*/
194
198
@ Override
195
199
public <C > ReaderT <Tuple2 <C , R >, M , Tuple2 <C , A >> cartesian () {
196
- return readerT (into ((c , r ) -> runReaderT (r ).fmap (tupler (c ))));
200
+ return readerT (into ((c , r ) -> runReaderT (r ).fmap (tupler (c ))), pureM );
197
201
}
198
202
199
203
/**
@@ -207,15 +211,16 @@ public ReaderT<R, M, Tuple2<R, A>> carry() {
207
211
/**
208
212
* Lift a {@link Fn1 function} (<code>R -> {@link Monad}<A, M></code>) into a {@link ReaderT} instance.
209
213
*
210
- * @param fn the function
211
- * @param <R> the input type
212
- * @param <M> the returned {@link Monad}
213
- * @param <A> the embedded output type
214
+ * @param fn the function
215
+ * @param pureM the {@link MonadRec monad's} {@link Pure} instance
216
+ * @param <R> the input type
217
+ * @param <M> the returned {@link Monad}
218
+ * @param <A> the embedded output type
214
219
* @return the {@link ReaderT}
215
220
*/
216
221
public static <R , M extends MonadRec <?, M >, A > ReaderT <R , M , A > readerT (
217
- Fn1 <? super R , ? extends MonadRec <A , M >> fn ) {
218
- return new ReaderT <>(fn );
222
+ Fn1 <? super R , ? extends MonadRec <A , M >> fn , Pure < M > pureM ) {
223
+ return new ReaderT <>(pureM , fn );
219
224
}
220
225
221
226
/**
@@ -230,7 +235,7 @@ public static <R, M extends MonadRec<?, M>, A> ReaderT<R, M, A> readerT(
230
235
return new Pure <ReaderT <R , M , ?>>() {
231
236
@ Override
232
237
public <A > ReaderT <R , M , A > checkedApply (A a ) {
233
- return readerT (__ -> pureM .apply (a ));
238
+ return readerT (__ -> pureM .apply (a ), pureM );
234
239
}
235
240
};
236
241
}
@@ -245,7 +250,7 @@ public <A> ReaderT<R, M, A> checkedApply(A a) {
245
250
return new Lift <ReaderT <R , ?, ?>>() {
246
251
@ Override
247
252
public <A , M extends MonadRec <?, M >> ReaderT <R , M , A > checkedApply (MonadRec <A , M > ga ) {
248
- return readerT (constantly (ga ));
253
+ return readerT (constantly (ga ), Pure . of ( ga ) );
249
254
}
250
255
};
251
256
}
0 commit comments