Skip to content

Instantly share code, notes, and snippets.

@vmarquez
Created June 17, 2015 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vmarquez/1e0b0366719f949eb59c to your computer and use it in GitHub Desktop.
Save vmarquez/1e0b0366719f949eb59c to your computer and use it in GitHub Desktop.
object StateApply {
import scalaz._
implicit def applyState[F[_], A](implicit F: Applicative[F], S: Semigroup[A]): Applicative[({ type l[a] = StateT[F, A, a]})#l] = new Applicative[({ type l[a] = StateT[F, A, a]})#l] {
def point[B](b: => B): StateT[F, A, B] = StateT[F, A, B]((a:A) => F.point((a,b)))
def ap[B, C](fa: => StateT[F, A, B])(f: => StateT[F, A, B => C]): StateT[F, A, C] = {
StateT[F, A, C]((s: A) => {
val sa = fa.run(s)
val sb = f.run(s)
val app = F.apply2[(A,B),(A,B => C), (A,C)](sa, sb) { case ((aa,bb),(cc,dd)) => {
val na = S.append(aa, cc)
(na, dd(bb))
}
}
app
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment