1919 * @param <M> the returned {@link Monad}
2020 * @param <A> the embedded output type
2121 */
22- public interface ReaderT <R , M extends Monad <?, M >, A > extends
22+ public final class ReaderT <R , M extends Monad <?, M >, A > implements
2323 MonadT <Fn1 <R , ?>, M , A , ReaderT <R , M , ?>>,
2424 Cartesian <R , A , ReaderT <?, M , ?>> {
2525
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+
2632 /**
2733 * Run the computation represented by this {@link ReaderT}.
2834 *
2935 * @param r the input
3036 * @return the {¬@link Monad monadic} embedding {@link Monad}<A, M>
3137 */
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+ }
3341
3442 /**
3543 * 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
4048 * @param <B> the new embedded result
4149 * @return the mapped {@link ReaderT}
4250 */
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 (
4452 Fn1 <? super MA , ? extends Monad <B , N >> fn ) {
4553 return readerT (r -> fn .apply (runReaderT (r ).coerce ()));
4654 }
@@ -49,47 +57,47 @@ default <MA extends Monad<A, M>, N extends Monad<?, N>, B> ReaderT<R, N, B> mapR
4957 * {@inheritDoc}
5058 */
5159 @ 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 () {
5361 return Fn1 .<R , GA >fn1 (r -> runReaderT (r ).coerce ()).coerce ();
5462 }
5563
5664 /**
5765 * {@inheritDoc}
5866 */
5967 @ 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 ) {
6169 return readerT (r -> runReaderT (r ).flatMap (a -> f .apply (a ).<ReaderT <R , M , B >>coerce ().runReaderT (r )));
6270 }
6371
6472 /**
6573 * {@inheritDoc}
6674 */
6775 @ Override
68- default <B > ReaderT <R , M , B > pure (B b ) {
76+ public <B > ReaderT <R , M , B > pure (B b ) {
6977 return readerT (r -> runReaderT (r ).pure (b ));
7078 }
7179
7280 /**
7381 * {@inheritDoc}
7482 */
7583 @ 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 ) {
7785 return MonadT .super .<B >fmap (fn ).coerce ();
7886 }
7987
8088 /**
8189 * {@inheritDoc}
8290 */
8391 @ 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 ) {
8593 return readerT (r -> runReaderT (r ).zip (appFn .<ReaderT <R , M , Fn1 <? super A , ? extends B >>>coerce ().runReaderT (r )));
8694 }
8795
8896 /**
8997 * {@inheritDoc}
9098 */
9199 @ Override
92- default <B > Lazy <? extends ReaderT <R , M , B >> lazyZip (
100+ public <B > Lazy <ReaderT <R , M , B >> lazyZip (
93101 Lazy <? extends Applicative <Fn1 <? super A , ? extends B >, ReaderT <R , M , ?>>> lazyAppFn ) {
94102 return lazyAppFn .fmap (this ::zip );
95103 }
@@ -98,63 +106,63 @@ default <B> Lazy<? extends ReaderT<R, M, B>> lazyZip(
98106 * {@inheritDoc}
99107 */
100108 @ 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 ) {
102110 return MonadT .super .discardL (appB ).coerce ();
103111 }
104112
105113 /**
106114 * {@inheritDoc}
107115 */
108116 @ 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 ) {
110118 return MonadT .super .discardR (appB ).coerce ();
111119 }
112120
113121 /**
114122 * {@inheritDoc}
115123 */
116124 @ 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 ) {
118126 return readerT (q -> runReaderT (lFn .apply (q )).fmap (rFn ));
119127 }
120128
121129 /**
122130 * {@inheritDoc}
123131 */
124132 @ 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 ) {
126134 return (ReaderT <Q , M , A >) Cartesian .super .<Q >diMapL (fn );
127135 }
128136
129137 /**
130138 * {@inheritDoc}
131139 */
132140 @ 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 ) {
134142 return (ReaderT <R , M , B >) Cartesian .super .<B >diMapR (fn );
135143 }
136144
137145 /**
138146 * {@inheritDoc}
139147 */
140148 @ 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 ) {
142150 return (ReaderT <Q , M , A >) Cartesian .super .<Q >contraMap (fn );
143151 }
144152
145153 /**
146154 * {@inheritDoc}
147155 */
148156 @ 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 () {
150158 return readerT (into ((c , r ) -> runReaderT (r ).fmap (tupler (c ))));
151159 }
152160
153161 /**
154162 * {@inheritDoc}
155163 */
156164 @ Override
157- default ReaderT <R , M , Tuple2 <R , A >> carry () {
165+ public ReaderT <R , M , Tuple2 <R , A >> carry () {
158166 return (ReaderT <R , M , Tuple2 <R , A >>) Cartesian .super .carry ();
159167 }
160168
@@ -167,7 +175,7 @@ default ReaderT<R, M, Tuple2<R, A>> carry() {
167175 * @param <A> the embedded output type
168176 * @return the {@link ReaderT}
169177 */
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 ) ;
172180 }
173181}
0 commit comments