Skip to content

Instantly share code, notes, and snippets.

@retronym
Created December 10, 2009 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retronym/253314 to your computer and use it in GitHub Desktop.
Save retronym/253314 to your computer and use it in GitHub Desktop.
First attempt at a Bind[M[X] <: Traversable[X]]
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