12
12
import com .jnape .palatable .lambda .monad .MonadWriter ;
13
13
import com .jnape .palatable .lambda .monad .transformer .builtin .StateT ;
14
14
15
- import static com .jnape .palatable .lambda .adt .Unit .UNIT ;
16
- import static com .jnape .palatable .lambda .adt .hlist .HList .tuple ;
17
- import static com .jnape .palatable .lambda .functions .Fn1 .fn1 ;
18
- import static com .jnape .palatable .lambda .functions .builtin .fn1 .Constantly .constantly ;
19
- import static com .jnape .palatable .lambda .functions .builtin .fn1 .Id .id ;
20
- import static com .jnape .palatable .lambda .functions .builtin .fn2 .Both .both ;
21
- import static com .jnape .palatable .lambda .functions .builtin .fn2 .Into .into ;
22
- import static com .jnape .palatable .lambda .functions .recursion .Trampoline .trampoline ;
23
15
import static com .jnape .palatable .lambda .functor .builtin .Identity .pureIdentity ;
16
+ import static com .jnape .palatable .lambda .monad .transformer .builtin .StateT .pureStateT ;
24
17
import static com .jnape .palatable .lambda .monad .transformer .builtin .StateT .stateT ;
25
18
26
19
/**
@@ -37,10 +30,19 @@ public final class State<S, A> implements
37
30
MonadReader <S , A , State <S , ?>>,
38
31
MonadWriter <S , A , State <S , ?>> {
39
32
40
- private final StateT <S , Identity <?>, A > stateFn ;
33
+ private final StateT <S , Identity <?>, A > delegate ;
41
34
42
- private State (StateT <S , Identity <?>, A > stateFn ) {
43
- this .stateFn = stateFn ;
35
+ private State (StateT <S , Identity <?>, A > delegate ) {
36
+ this .delegate = delegate ;
37
+ }
38
+
39
+ /**
40
+ * Convert this {@link State} to a {@link StateT} with an {@link Identity} embedding.
41
+ *
42
+ * @return the {@link StateT}
43
+ */
44
+ public StateT <S , Identity <?>, A > toStateT () {
45
+ return delegate ;
44
46
}
45
47
46
48
/**
@@ -50,7 +52,7 @@ private State(StateT<S, Identity<?>, A> stateFn) {
50
52
* @return a {@link Tuple2} of the result and the final state.
51
53
*/
52
54
public Tuple2 <A , S > run (S s ) {
53
- return stateFn .<Identity <Tuple2 <A , S >>>runStateT (s ).runIdentity ();
55
+ return delegate .<Identity <Tuple2 <A , S >>>runStateT (s ).runIdentity ();
54
56
}
55
57
56
58
/**
@@ -81,7 +83,7 @@ public S exec(S s) {
81
83
* @return the mapped {@link State}
82
84
*/
83
85
public <B > State <S , B > mapState (Fn1 <? super Tuple2 <A , S >, ? extends Tuple2 <B , S >> fn ) {
84
- return state (s -> fn . apply ( run ( s )));
86
+ return state (delegate . mapStateT ( f -> f . fmap ( fn ), pureIdentity ( )));
85
87
}
86
88
87
89
/**
@@ -91,23 +93,23 @@ public <B> State<S, B> mapState(Fn1<? super Tuple2<A, S>, ? extends Tuple2<B, S>
91
93
* @return the mapped {@link State}
92
94
*/
93
95
public State <S , A > withState (Fn1 <? super S , ? extends S > fn ) {
94
- return state (s -> run (fn .apply ( s )));
96
+ return state (delegate . withStateT (fn .fmap ( Identity :: new )));
95
97
}
96
98
97
99
/**
98
100
* {@inheritDoc}
99
101
*/
100
102
@ Override
101
103
public State <S , A > local (Fn1 <? super S , ? extends S > fn ) {
102
- return state (s -> run ( fn . apply ( s ) ));
104
+ return state (delegate . local ( fn ));
103
105
}
104
106
105
107
/**
106
108
* {@inheritDoc}
107
109
*/
108
110
@ Override
109
111
public <B > State <S , Tuple2 <A , B >> listens (Fn1 <? super S , ? extends B > fn ) {
110
- return state (s -> run ( s ). biMapL ( both ( id (), constantly ( fn . apply ( s ))) ));
112
+ return state (delegate . listens ( fn ));
111
113
}
112
114
113
115
/**
@@ -123,15 +125,15 @@ public State<S, A> censor(Fn1<? super S, ? extends S> fn) {
123
125
*/
124
126
@ Override
125
127
public <B > State <S , B > flatMap (Fn1 <? super A , ? extends Monad <B , State <S , ?>>> f ) {
126
- return state (s -> run ( s ). into (( a , s2 ) -> f . apply ( a ). <State <S , B >>coerce ().run ( s2 )));
128
+ return state (delegate . flatMap ( f . fmap ( state -> state . <State <S , B >>coerce ().delegate )));
127
129
}
128
130
129
131
/**
130
132
* {@inheritDoc}
131
133
*/
132
134
@ Override
133
135
public <B > State <S , B > pure (B b ) {
134
- return state (s -> tuple ( b , s ));
136
+ return state (delegate . pure ( b ));
135
137
}
136
138
137
139
/**
@@ -180,9 +182,7 @@ public <B> State<S, B> discardL(Applicative<B, State<S, ?>> appB) {
180
182
*/
181
183
@ Override
182
184
public <B > State <S , B > trampolineM (Fn1 <? super A , ? extends MonadRec <RecursiveResult <A , B >, State <S , ?>>> fn ) {
183
- return state (fn1 (this ::run ).fmap (trampoline (into ((a , s ) -> fn .apply (a )
184
- .<State <S , RecursiveResult <A , B >>>coerce ().run (s )
185
- .into ((aOrB , s_ ) -> aOrB .biMap (a_ -> tuple (a_ , s_ ), b -> tuple (b , s_ )))))));
185
+ return state (delegate .trampolineM (a -> fn .apply (a ).<State <S , RecursiveResult <A , B >>>coerce ().delegate ));
186
186
}
187
187
188
188
/**
@@ -191,9 +191,8 @@ public <B> State<S, B> trampolineM(Fn1<? super A, ? extends MonadRec<RecursiveRe
191
191
* @param <A> the state and result type
192
192
* @return the new {@link State} instance
193
193
*/
194
- @ SuppressWarnings ("RedundantTypeArguments" )
195
194
public static <A > State <A , A > get () {
196
- return state (Tuple2 ::< A > fill );
195
+ return state (StateT . get ( pureIdentity ()) );
197
196
}
198
197
199
198
/**
@@ -205,7 +204,7 @@ public static <A> State<A, A> get() {
205
204
* @return the new {@link State} instance
206
205
*/
207
206
public static <S > State <S , Unit > put (S s ) {
208
- return modify ( constantly ( s ));
207
+ return state ( StateT . put ( new Identity <>( s ) ));
209
208
}
210
209
211
210
/**
@@ -217,7 +216,7 @@ public static <S> State<S, Unit> put(S s) {
217
216
* @return the new {@link State} instance
218
217
*/
219
218
public static <S , A > State <S , A > gets (Fn1 <? super S , ? extends A > fn ) {
220
- return state (both ( fn , id ()));
219
+ return state (StateT . gets ( a -> new Identity <>( fn . apply ( a )), pureIdentity ()));
221
220
}
222
221
223
222
/**
@@ -228,7 +227,7 @@ public static <S, A> State<S, A> gets(Fn1<? super S, ? extends A> fn) {
228
227
* @return the new {@link State} instance
229
228
*/
230
229
public static <S > State <S , Unit > modify (Fn1 <? super S , ? extends S > fn ) {
231
- return state (both ( constantly ( UNIT ), fn ));
230
+ return state (StateT . modify ( s -> new Identity <>( fn . apply ( s )), pureIdentity () ));
232
231
}
233
232
234
233
/**
@@ -240,7 +239,7 @@ public static <S> State<S, Unit> modify(Fn1<? super S, ? extends S> fn) {
240
239
* @return the new {@link State} instance
241
240
*/
242
241
public static <S , A > State <S , A > state (A a ) {
243
- return gets ( constantly ( a ));
242
+ return state ( stateT ( new Identity <>( a ) ));
244
243
}
245
244
246
245
/**
@@ -253,7 +252,7 @@ public static <S, A> State<S, A> state(A a) {
253
252
* @return the new {@link State} instance
254
253
*/
255
254
public static <S , A > State <S , A > state (Fn1 <? super S , ? extends Tuple2 <A , S >> stateFn ) {
256
- return new State <> (stateT (s -> new Identity <>(stateFn .apply (s )), pureIdentity ()));
255
+ return state (stateT (s -> new Identity <>(stateFn .apply (s )), pureIdentity ()));
257
256
}
258
257
259
258
/**
@@ -263,11 +262,24 @@ public static <S, A> State<S, A> state(Fn1<? super S, ? extends Tuple2<A, S>> st
263
262
* @return the {@link Pure} instance
264
263
*/
265
264
public static <S > Pure <State <S , ?>> pureState () {
265
+ Pure <StateT <S , Identity <?>, ?>> pureStateT = pureStateT (pureIdentity ());
266
266
return new Pure <State <S , ?>>() {
267
267
@ Override
268
268
public <A > State <S , A > checkedApply (A a ) {
269
- return state (s -> tuple ( a , s ));
269
+ return state (pureStateT .< A , StateT < S , Identity <?>, A >> apply ( a ));
270
270
}
271
271
};
272
272
}
273
+
274
+ /**
275
+ * Create a {@link State} from a delegate {@link StateT} with an {@link Identity} embedding.
276
+ *
277
+ * @param stateT the delegate {@link StateT}
278
+ * @param <S> the state type
279
+ * @param <A> the result type
280
+ * @return the new {@link State}
281
+ */
282
+ public static <S , A > State <S , A > state (StateT <S , Identity <?>, A > stateT ) {
283
+ return new State <>(stateT );
284
+ }
273
285
}
0 commit comments