Skip to content

Instantly share code, notes, and snippets.

@jedws
Created August 13, 2013 04:41
Show Gist options
  • Select an option

  • Save jedws/6217956 to your computer and use it in GitHub Desktop.

Select an option

Save jedws/6217956 to your computer and use it in GitHub Desktop.
import scalaz.Monad
import scalaz.syntax.monad._
/**
* Non stack-using Foldable substitute
*/
trait Folder[F[_], A] {
def foldLefterM[G[_], B](z: B)(f: (B, A) => G[B])(implicit M: Monad[G]): G[B]
}
object Folder {
implicit def FolderList[A](ls: List[A]) =
new Folder[List, A] {
def foldLefterM[G[_], B](z: B)(f: (B, A) => G[B])(implicit M: Monad[G]): G[B] =
ls.foldLeft(z.point[G]) {
case (mg, a) => mg >>= { b => f(b, a) }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment