@@ -35,9 +35,11 @@ public final class StateT<S, M extends MonadRec<?, M>, A> implements
35
35
MonadReader <S , A , StateT <S , M , ?>>,
36
36
MonadWriter <S , A , StateT <S , M , ?>> {
37
37
38
+ private final Pure <M > pureM ;
38
39
private final Fn1 <? super S , ? extends MonadRec <Tuple2 <A , S >, M >> stateFn ;
39
40
40
- private StateT (Fn1 <? super S , ? extends MonadRec <Tuple2 <A , S >, M >> stateFn ) {
41
+ private StateT (Pure <M > pureM , Fn1 <? super S , ? extends MonadRec <Tuple2 <A , S >, M >> stateFn ) {
42
+ this .pureM = pureM ;
41
43
this .stateFn = stateFn ;
42
44
}
43
45
@@ -78,14 +80,15 @@ public <MS extends Monad<S, M>> MS execT(S s) {
78
80
/**
79
81
* Map both the result and the final state to a new result and final state inside the {@link Monad}.
80
82
*
81
- * @param fn the mapping function
82
- * @param <N> the new {@link Monad monadic embedding} for this {@link StateT}
83
- * @param <B> the new state type
83
+ * @param fn the mapping function
84
+ * @param pureN the new embedded {@link MonadRec monad's} {@link Pure} instance
85
+ * @param <N> the new {@link Monad monadic embedding} for this {@link StateT}
86
+ * @param <B> the new state type
84
87
* @return the mapped {@link StateT}
85
88
*/
86
89
public <N extends MonadRec <?, N >, B > StateT <S , N , B > mapStateT (
87
- Fn1 <? super MonadRec <Tuple2 <A , S >, M >, ? extends MonadRec <Tuple2 <B , S >, N >> fn ) {
88
- return stateT (s -> fn .apply (runStateT (s )));
90
+ Fn1 <? super MonadRec <Tuple2 <A , S >, M >, ? extends MonadRec <Tuple2 <B , S >, N >> fn , Pure < N > pureN ) {
91
+ return stateT (s -> fn .apply (runStateT (s )), pureN );
89
92
}
90
93
91
94
/**
@@ -96,15 +99,15 @@ public <N extends MonadRec<?, N>, B> StateT<S, N, B> mapStateT(
96
99
* @return the mapped {@link StateT}
97
100
*/
98
101
public StateT <S , M , A > withStateT (Fn1 <? super S , ? extends MonadRec <S , M >> fn ) {
99
- return modify (fn ).flatMap (constantly (this ));
102
+ return modify (fn , pureM ).flatMap (constantly (this ));
100
103
}
101
104
102
105
/**
103
106
* {@inheritDoc}
104
107
*/
105
108
@ Override
106
109
public <B > StateT <S , M , Tuple2 <A , B >> listens (Fn1 <? super S , ? extends B > fn ) {
107
- return mapStateT (mas -> mas .fmap (t -> t .into ((a , s ) -> tuple (tuple (a , fn .apply (s )), s ))));
110
+ return mapStateT (mas -> mas .fmap (t -> t .into ((a , s ) -> tuple (tuple (a , fn .apply (s )), s ))), pureM );
108
111
}
109
112
110
113
/**
@@ -120,23 +123,24 @@ public StateT<S, M, A> censor(Fn1<? super S, ? extends S> fn) {
120
123
*/
121
124
@ Override
122
125
public StateT <S , M , A > local (Fn1 <? super S , ? extends S > fn ) {
123
- return stateT (s -> runStateT (fn .apply (s )));
126
+ return stateT (s -> runStateT (fn .apply (s )), pureM );
124
127
}
125
128
126
129
/**
127
130
* {@inheritDoc}
128
131
*/
129
132
@ Override
130
133
public <B > StateT <S , M , B > flatMap (Fn1 <? super A , ? extends Monad <B , StateT <S , M , ?>>> f ) {
131
- return stateT (s -> runStateT (s ).flatMap (into ((a , s_ ) -> f .apply (a ).<StateT <S , M , B >>coerce ().runStateT (s_ ))));
134
+ return stateT (s -> runStateT (s ).flatMap (into ((a , s_ ) -> f .apply (a ).<StateT <S , M , B >>coerce ().runStateT (s_ ))),
135
+ pureM );
132
136
}
133
137
134
138
/**
135
139
* {@inheritDoc}
136
140
*/
137
141
@ Override
138
142
public <B > StateT <S , M , B > pure (B b ) {
139
- return stateT (s -> runStateT ( s ). pure (tuple (b , s )));
143
+ return stateT (s -> pureM . apply (tuple (b , s )), pureM );
140
144
}
141
145
142
146
/**
@@ -185,7 +189,7 @@ public <B> StateT<S, M, A> discardR(Applicative<B, StateT<S, M, ?>> appB) {
185
189
*/
186
190
@ Override
187
191
public <B , N extends MonadRec <?, N >> StateT <S , N , B > lift (MonadRec <B , N > mb ) {
188
- return stateT (s -> mb .fmap (b -> tuple (b , s )));
192
+ return stateT (s -> mb .fmap (b -> tuple (b , s )), Pure . of ( mb ) );
189
193
}
190
194
191
195
/**
@@ -194,11 +198,12 @@ public <B, N extends MonadRec<?, N>> StateT<S, N, B> lift(MonadRec<B, N> mb) {
194
198
@ Override
195
199
public <B > StateT <S , M , B > trampolineM (
196
200
Fn1 <? super A , ? extends MonadRec <RecursiveResult <A , B >, StateT <S , M , ?>>> fn ) {
197
- return StateT .<S , M , B >stateT ((Fn1 .<S , MonadRec <Tuple2 <A , S >, M >>fn1 (this ::runStateT ))
198
- .fmap (m -> m .trampolineM (into ((a , s ) -> fn .apply (a )
199
- .<StateT <S , M , RecursiveResult <A , B >>>coerce ().runStateT (s )
200
- .fmap (into ((aOrB , s_ ) -> aOrB .biMap (a_ -> tuple (a_ , s_ ),
201
- b -> tuple (b , s_ ))))))));
201
+ return stateT ((Fn1 .<S , MonadRec <Tuple2 <A , S >, M >>fn1 (this ::runStateT ))
202
+ .fmap (m -> m .trampolineM (into ((a , s ) -> fn .apply (a )
203
+ .<StateT <S , M , RecursiveResult <A , B >>>coerce ().runStateT (s )
204
+ .fmap (into ((aOrB , s_ ) -> aOrB .biMap (a_ -> tuple (a_ , s_ ),
205
+ b -> tuple (b , s_ ))))))),
206
+ pureM );
202
207
}
203
208
204
209
/**
@@ -212,34 +217,37 @@ public <B> StateT<S, M, B> trampolineM(
212
217
*/
213
218
@ SuppressWarnings ("RedundantTypeArguments" )
214
219
public static <A , M extends MonadRec <?, M >> StateT <A , M , A > get (Pure <M > pureM ) {
215
- return gets (pureM ::<A , MonadRec <A , M >>apply );
220
+ return gets (pureM ::<A , MonadRec <A , M >>apply , pureM );
216
221
}
217
222
218
223
/**
219
224
* Given a function that produces a value inside a {@link Monad monadic effect} from a state, produce a
220
225
* {@link StateT} that simply passes its state to the function and applies it.
221
226
*
222
- * @param fn the function
223
- * @param <S> the state type
224
- * @param <M> the{@link Monad} embedding
225
- * @param <A> the value type
227
+ * @param fn the function
228
+ * @param pureM the embedded {@link MonadRec monad's} {@link Pure} instance
229
+ * @param <S> the state type
230
+ * @param <M> the{@link Monad} embedding
231
+ * @param <A> the value type
226
232
* @return the {@link StateT}
227
233
*/
228
- public static <S , M extends MonadRec <?, M >, A > StateT <S , M , A > gets (Fn1 <? super S , ? extends MonadRec <A , M >> fn ) {
229
- return stateT (s -> fn .apply (s ).fmap (a -> tuple (a , s )));
234
+ public static <S , M extends MonadRec <?, M >, A > StateT <S , M , A > gets (Fn1 <? super S , ? extends MonadRec <A , M >> fn ,
235
+ Pure <M > pureM ) {
236
+ return stateT (s -> fn .apply (s ).fmap (a -> tuple (a , s )), pureM );
230
237
}
231
238
232
239
/**
233
240
* Lift a function that makes a stateful modification inside an {@link Monad} into {@link StateT}.
234
241
*
235
242
* @param updateFn the update function
243
+ * @param pureM the embedded {@link MonadRec monad's} {@link Pure} instance
236
244
* @param <S> the state type
237
245
* @param <M> the {@link Monad} embedding
238
246
* @return the {@link StateT}
239
247
*/
240
248
public static <S , M extends MonadRec <?, M >> StateT <S , M , Unit > modify (
241
- Fn1 <? super S , ? extends MonadRec <S , M >> updateFn ) {
242
- return stateT (s -> updateFn .apply (s ).fmap (tupler (UNIT )));
249
+ Fn1 <? super S , ? extends MonadRec <S , M >> updateFn , Pure < M > pureM ) {
250
+ return stateT (s -> updateFn .apply (s ).fmap (tupler (UNIT )), pureM );
243
251
}
244
252
245
253
/**
@@ -251,7 +259,7 @@ public static <S, M extends MonadRec<?, M>> StateT<S, M, Unit> modify(
251
259
* @return the {@link StateT}
252
260
*/
253
261
public static <S , M extends MonadRec <?, M >> StateT <S , M , Unit > put (MonadRec <S , M > ms ) {
254
- return modify (constantly (ms ));
262
+ return modify (constantly (ms ), Pure . of ( ms ) );
255
263
}
256
264
257
265
/**
@@ -264,21 +272,22 @@ public static <S, M extends MonadRec<?, M>> StateT<S, M, Unit> put(MonadRec<S, M
264
272
* @return the {@link StateT}
265
273
*/
266
274
public static <S , M extends MonadRec <?, M >, A > StateT <S , M , A > stateT (MonadRec <A , M > ma ) {
267
- return gets (constantly (ma ));
275
+ return gets (constantly (ma ), Pure . of ( ma ) );
268
276
}
269
277
270
278
/**
271
279
* Lift a state-sensitive {@link Monad monadically embedded} computation into {@link StateT}.
272
280
*
273
281
* @param stateFn the stateful operation
282
+ * @param pureM the embedded {@link MonadRec monad's} {@link Pure} instance
274
283
* @param <S> the state type
275
284
* @param <M> the {@link Monad} embedding
276
285
* @param <A> the result type
277
286
* @return the {@link StateT}
278
287
*/
279
288
public static <S , M extends MonadRec <?, M >, A > StateT <S , M , A > stateT (
280
- Fn1 <? super S , ? extends MonadRec <Tuple2 <A , S >, M >> stateFn ) {
281
- return new StateT <>(stateFn );
289
+ Fn1 <? super S , ? extends MonadRec <Tuple2 <A , S >, M >> stateFn , Pure < M > pureM ) {
290
+ return new StateT <>(pureM , stateFn );
282
291
}
283
292
284
293
/**
@@ -292,7 +301,7 @@ public static <S, M extends MonadRec<?, M>, A> StateT<S, M, A> stateT(
292
301
public static <S , M extends MonadRec <?, M >> Pure <StateT <S , M , ?>> pureStateT (Pure <M > pureM ) {
293
302
return new Pure <StateT <S , M , ?>>() {
294
303
@ Override
295
- public <A > StateT <S , M , A > checkedApply (A a ) throws Throwable {
304
+ public <A > StateT <S , M , A > checkedApply (A a ) {
296
305
return stateT (pureM .<A , MonadRec <A , M >>apply (a ));
297
306
}
298
307
};
0 commit comments