Skip to content

Instantly share code, notes, and snippets.

@bmjames
Created August 17, 2012 13:45
Show Gist options
  • Save bmjames/3378799 to your computer and use it in GitHub Desktop.
Save bmjames/3378799 to your computer and use it in GitHub Desktop.
filterMN
import scalaz._, syntax.monad._
final def filterMN[A, M[_]:Monad, N[_]:Monad](as: List[A])(p: A => N[M[Boolean]]): N[M[List[A]]] =
as match {
case Nil => Monad[N].point(Monad[M].point(Nil))
case h :: t =>
for {
mb <- p(h)
mg <- filterMN(t)(p)
} yield {
Monad[M].bind(mb)(b => if (b) Monad[M].map(mg)(tt => h :: tt) else mg)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment