Created
April 17, 2016 03:20
-
-
Save clinuxrulz/4125768b73e897e5e3d3b481eb66444f to your computer and use it in GitHub Desktop.
RWST MonadRec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package com.sm.fp.data.trans.rws; | |
import com.sm.fp.data.trans.RWST; | |
import com.sm.fp.kinds.T_; | |
import com.sm.fp.kinds.T__; | |
import com.sm.fp.kinds.T___; | |
import com.sm.fp.kinds.T____; | |
import com.sm.fp.kinds.T_____; | |
import com.sm.fp.typeclass1.monad.MonadRec; | |
import fj.F; | |
import fj.P; | |
import fj.P3; | |
import fj.data.Either; | |
/** | |
* | |
* @author Clinton | |
*/ | |
public interface RWSTMonadRec<R,W,S,M> extends RWSTMonad<R,W,S,M>, MonadRec<T__.Mu<T___.Mu<T____.Mu<T_____.Mu<RWST.Mu,R>,W>,S>,M>> { | |
@Override | |
public MonadRec<M> m(); | |
@Override | |
public default <A, B> RWST<R,W,S,M,B> tailRecM(F<A, T_<T__.Mu<T___.Mu<T____.Mu<T_____.Mu<RWST.Mu, R>, W>, S>, M>, Either<A, B>>> f, A a0) { | |
return (R r, S s0) -> m().tailRecM( | |
(P3<A,S,W> x) -> { | |
A a = x._1(); | |
S s = x._2(); | |
W w = x._3(); | |
return m().fmap( | |
(P3<Either<A,B>,S,W> x2) -> { | |
Either<A,B> aOrB = x2._1(); | |
S s2 = x2._2(); | |
W w2 = x2._3(); | |
return aOrB.bimap( | |
(A a2) -> P.p(a2, s2, wSemigroup().sum(w, w2)), | |
(B b) -> P.p(b, s2, wSemigroup().sum(w, w2)) | |
); | |
}, | |
RWST.narrow(f.f(a)).run(r, s) | |
); | |
}, | |
P.p(a0, s0, wMonoid().zero()) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment