Created
August 17, 2012 13:45
-
-
Save bmjames/3378799 to your computer and use it in GitHub Desktop.
filterMN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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