Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Can filter be written in terms of flatMap? No, it cannot.
case class XList[A](x: List[A]) {
def filter[A](p: A => Boolean): XList[A] => XList[A] =
error("I will also give you money using flatMap/point")
def flatMap[B](f: A => XList[B]): XList[B] =
XList(x.foldRight[List[B]](Nil)((a, b) => f(a).x ::: b))
}
object XList {
def point[A]: A => XList[A] =
a => XList(List(a))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment