Created
December 10, 2009 12:58
-
-
Save retronym/253314 to your computer and use it in GitHub Desktop.
First attempt at a Bind[M[X] <: Traversable[X]]
This file contains 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 collection._ | |
import collection.generic._ | |
trait Bind[Z[_]] { | |
def bind[A, B](a: Z[A], f: A => Z[B]): Z[B] | |
} | |
def TraversableBind[M[X] <: Traversable[X]] = new Bind[M] { | |
def bind[A, B](r: M[A], f: A => M[B]): M[B] = { | |
implicit object cbf extends CanBuildFrom[Traversable[A], B, M[B]] { | |
def apply(from: Traversable[A]) = r.genericBuilder[B].asInstanceOf[collection.mutable.Builder[B, M[B]]] | |
def apply() = error("undefined") | |
} | |
r.flatMap[B, M[B]](f) | |
} | |
} | |
val bound = TraversableBind[List].bind(List(1, 2, 3), (x: Int) => List(x, x)) | |
println(bound) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment