19
19
* @param <M> the returned {@link Monad}
20
20
* @param <A> the embedded output type
21
21
*/
22
- public interface ReaderT <R , M extends Monad <?, M >, A > extends
22
+ public final class ReaderT <R , M extends Monad <?, M >, A > implements
23
23
MonadT <Fn1 <R , ?>, M , A , ReaderT <R , M , ?>>,
24
24
Cartesian <R , A , ReaderT <?, M , ?>> {
25
25
26
+ private final Fn1 <? super R , ? extends Monad <A , M >> fn ;
27
+
28
+ private ReaderT (Fn1 <? super R , ? extends Monad <A , M >> fn ) {
29
+ this .fn = fn ;
30
+ }
31
+
26
32
/**
27
33
* Run the computation represented by this {@link ReaderT}.
28
34
*
29
35
* @param r the input
30
36
* @return the {¬@link Monad monadic} embedding {@link Monad}<A, M>
31
37
*/
32
- Monad <A , M > runReaderT (R r );
38
+ public <MA extends Monad <A , M >> MA runReaderT (R r ) {
39
+ return fn .apply (r ).coerce ();
40
+ }
33
41
34
42
/**
35
43
* Map the current {@link Monad monadic} embedding to a new one in a potentially different {@link Monad}.
@@ -40,7 +48,7 @@ public interface ReaderT<R, M extends Monad<?, M>, A> extends
40
48
* @param <B> the new embedded result
41
49
* @return the mapped {@link ReaderT}
42
50
*/
43
- default <MA extends Monad <A , M >, N extends Monad <?, N >, B > ReaderT <R , N , B > mapReaderT (
51
+ public <MA extends Monad <A , M >, N extends Monad <?, N >, B > ReaderT <R , N , B > mapReaderT (
44
52
Fn1 <? super MA , ? extends Monad <B , N >> fn ) {
45
53
return readerT (r -> fn .apply (runReaderT (r ).coerce ()));
46
54
}
@@ -49,47 +57,47 @@ default <MA extends Monad<A, M>, N extends Monad<?, N>, B> ReaderT<R, N, B> mapR
49
57
* {@inheritDoc}
50
58
*/
51
59
@ Override
52
- default <GA extends Monad <A , M >, FGA extends Monad <GA , Fn1 <R , ?>>> FGA run () {
60
+ public <GA extends Monad <A , M >, FGA extends Monad <GA , Fn1 <R , ?>>> FGA run () {
53
61
return Fn1 .<R , GA >fn1 (r -> runReaderT (r ).coerce ()).coerce ();
54
62
}
55
63
56
64
/**
57
65
* {@inheritDoc}
58
66
*/
59
67
@ Override
60
- default <B > ReaderT <R , M , B > flatMap (Fn1 <? super A , ? extends Monad <B , ReaderT <R , M , ?>>> f ) {
68
+ public <B > ReaderT <R , M , B > flatMap (Fn1 <? super A , ? extends Monad <B , ReaderT <R , M , ?>>> f ) {
61
69
return readerT (r -> runReaderT (r ).flatMap (a -> f .apply (a ).<ReaderT <R , M , B >>coerce ().runReaderT (r )));
62
70
}
63
71
64
72
/**
65
73
* {@inheritDoc}
66
74
*/
67
75
@ Override
68
- default <B > ReaderT <R , M , B > pure (B b ) {
76
+ public <B > ReaderT <R , M , B > pure (B b ) {
69
77
return readerT (r -> runReaderT (r ).pure (b ));
70
78
}
71
79
72
80
/**
73
81
* {@inheritDoc}
74
82
*/
75
83
@ Override
76
- default <B > ReaderT <R , M , B > fmap (Fn1 <? super A , ? extends B > fn ) {
84
+ public <B > ReaderT <R , M , B > fmap (Fn1 <? super A , ? extends B > fn ) {
77
85
return MonadT .super .<B >fmap (fn ).coerce ();
78
86
}
79
87
80
88
/**
81
89
* {@inheritDoc}
82
90
*/
83
91
@ Override
84
- default <B > ReaderT <R , M , B > zip (Applicative <Fn1 <? super A , ? extends B >, ReaderT <R , M , ?>> appFn ) {
92
+ public <B > ReaderT <R , M , B > zip (Applicative <Fn1 <? super A , ? extends B >, ReaderT <R , M , ?>> appFn ) {
85
93
return readerT (r -> runReaderT (r ).zip (appFn .<ReaderT <R , M , Fn1 <? super A , ? extends B >>>coerce ().runReaderT (r )));
86
94
}
87
95
88
96
/**
89
97
* {@inheritDoc}
90
98
*/
91
99
@ Override
92
- default <B > Lazy <? extends ReaderT <R , M , B >> lazyZip (
100
+ public <B > Lazy <ReaderT <R , M , B >> lazyZip (
93
101
Lazy <? extends Applicative <Fn1 <? super A , ? extends B >, ReaderT <R , M , ?>>> lazyAppFn ) {
94
102
return lazyAppFn .fmap (this ::zip );
95
103
}
@@ -98,63 +106,63 @@ default <B> Lazy<? extends ReaderT<R, M, B>> lazyZip(
98
106
* {@inheritDoc}
99
107
*/
100
108
@ Override
101
- default <B > ReaderT <R , M , B > discardL (Applicative <B , ReaderT <R , M , ?>> appB ) {
109
+ public <B > ReaderT <R , M , B > discardL (Applicative <B , ReaderT <R , M , ?>> appB ) {
102
110
return MonadT .super .discardL (appB ).coerce ();
103
111
}
104
112
105
113
/**
106
114
* {@inheritDoc}
107
115
*/
108
116
@ Override
109
- default <B > ReaderT <R , M , A > discardR (Applicative <B , ReaderT <R , M , ?>> appB ) {
117
+ public <B > ReaderT <R , M , A > discardR (Applicative <B , ReaderT <R , M , ?>> appB ) {
110
118
return MonadT .super .discardR (appB ).coerce ();
111
119
}
112
120
113
121
/**
114
122
* {@inheritDoc}
115
123
*/
116
124
@ Override
117
- default <Q , B > ReaderT <Q , M , B > diMap (Fn1 <? super Q , ? extends R > lFn , Fn1 <? super A , ? extends B > rFn ) {
125
+ public <Q , B > ReaderT <Q , M , B > diMap (Fn1 <? super Q , ? extends R > lFn , Fn1 <? super A , ? extends B > rFn ) {
118
126
return readerT (q -> runReaderT (lFn .apply (q )).fmap (rFn ));
119
127
}
120
128
121
129
/**
122
130
* {@inheritDoc}
123
131
*/
124
132
@ Override
125
- default <Q > ReaderT <Q , M , A > diMapL (Fn1 <? super Q , ? extends R > fn ) {
133
+ public <Q > ReaderT <Q , M , A > diMapL (Fn1 <? super Q , ? extends R > fn ) {
126
134
return (ReaderT <Q , M , A >) Cartesian .super .<Q >diMapL (fn );
127
135
}
128
136
129
137
/**
130
138
* {@inheritDoc}
131
139
*/
132
140
@ Override
133
- default <B > ReaderT <R , M , B > diMapR (Fn1 <? super A , ? extends B > fn ) {
141
+ public <B > ReaderT <R , M , B > diMapR (Fn1 <? super A , ? extends B > fn ) {
134
142
return (ReaderT <R , M , B >) Cartesian .super .<B >diMapR (fn );
135
143
}
136
144
137
145
/**
138
146
* {@inheritDoc}
139
147
*/
140
148
@ Override
141
- default <Q > ReaderT <Q , M , A > contraMap (Fn1 <? super Q , ? extends R > fn ) {
149
+ public <Q > ReaderT <Q , M , A > contraMap (Fn1 <? super Q , ? extends R > fn ) {
142
150
return (ReaderT <Q , M , A >) Cartesian .super .<Q >contraMap (fn );
143
151
}
144
152
145
153
/**
146
154
* {@inheritDoc}
147
155
*/
148
156
@ Override
149
- default <C > ReaderT <Tuple2 <C , R >, M , Tuple2 <C , A >> cartesian () {
157
+ public <C > ReaderT <Tuple2 <C , R >, M , Tuple2 <C , A >> cartesian () {
150
158
return readerT (into ((c , r ) -> runReaderT (r ).fmap (tupler (c ))));
151
159
}
152
160
153
161
/**
154
162
* {@inheritDoc}
155
163
*/
156
164
@ Override
157
- default ReaderT <R , M , Tuple2 <R , A >> carry () {
165
+ public ReaderT <R , M , Tuple2 <R , A >> carry () {
158
166
return (ReaderT <R , M , Tuple2 <R , A >>) Cartesian .super .carry ();
159
167
}
160
168
@@ -167,7 +175,7 @@ default ReaderT<R, M, Tuple2<R, A>> carry() {
167
175
* @param <A> the embedded output type
168
176
* @return the {@link ReaderT}
169
177
*/
170
- static <R , M extends Monad <?, M >, A > ReaderT <R , M , A > readerT (Fn1 <? super R , ? extends Monad <A , M >> fn ) {
171
- return fn :: apply ;
178
+ public static <R , M extends Monad <?, M >, A > ReaderT <R , M , A > readerT (Fn1 <? super R , ? extends Monad <A , M >> fn ) {
179
+ return new ReaderT <>( fn ) ;
172
180
}
173
181
}
0 commit comments