Applicative vs. monadic combinators and the free monad in Scalaz

后端 未结 3 1323
醉酒成梦
醉酒成梦 2020-12-31 11:55

A couple of weeks ago Dragisa Krsmanovic asked a question here about how to use the free monad in Scalaz 7 to avoid stack overflows in this situation (I\'ve adapted his code

3条回答
  •  佛祖请我去吃肉
    2020-12-31 12:28

    Just to add to the discussion...

    In StateT, you have:

      def flatMap[S3, B](f: A => IndexedStateT[F, S2, S3, B])(implicit F: Bind[F]): IndexedStateT[F, S1, S3, B] = 
      IndexedStateT(s => F.bind(apply(s)) {
        case (s1, a) => f(a)(s1)
      })
    

    The apply(s) fixes the current state reference in the next state.

    bind definition interpretes eagerly its parameters catching the reference because it requires it:

      def bind[A, B](fa: F[A])(f: A => F[B]): F[B]
    

    At the difference of ap which might not need to interprete one of its parameters:

      def ap[A, B](fa: => F[A])(f: => F[A => B]): F[B]
    

    With this code, the Trampoline can't help for StateT flatMap (and also map)...

提交回复
热议问题