Skip to content

Instantly share code, notes, and snippets.

@clinuxrulz
Created April 17, 2016 03:20
Show Gist options
  • Save clinuxrulz/4125768b73e897e5e3d3b481eb66444f to your computer and use it in GitHub Desktop.
Save clinuxrulz/4125768b73e897e5e3d3b481eb66444f to your computer and use it in GitHub Desktop.
RWST MonadRec
/*
* 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