Skip to content

Instantly share code, notes, and snippets.

@wheaties
Created January 31, 2015 20:27
Show Gist options
  • Save wheaties/b7bff5f42209b5807725 to your computer and use it in GitHub Desktop.
Save wheaties/b7bff5f42209b5807725 to your computer and use it in GitHub Desktop.
Compilation error
/*
So here's the error which seems to be straight forward. That said, I've tried a bunch of things and this is the "bets" I've been
able to do:
[error] CKnot.scala:18: Cannot construct a collection of type That with elements of type B based on a collection of type C[A].
[error] def apply[A, C[A] <: GenTraversableLike[A, C[A]]](ma: C[A], f: A => C[B]) = ma flatMap f
[error]
[error] CKnot.scala:26: Cannot construct a collection of type That with elements of type B based on a collection of type C[A].
[error] a <- ma
[error]
...and it continues
*/
import scala.collection.{GenTraversable, GenTraversableLike}
trait CKnot[C[+_], B]{
type R
def apply[A](ma: C[A], f: A => B): C[R]
}
trait CKnotCompanion extends LowPriorityCKnot{
def apply[B](implicit knot: CKnot[C, B]): Aux[C, B, knot.R] = knot
implicit def Cbase[B]: Aux[C, C[B], B] =
new CKnot[C, C[B]]{
type R = B
def apply[A](ma: C[A], f: A => C[B]) = ma flatMap f
}
implicit def opt[B]: Aux[C, Option[B], B] =
new CKnot[C, Option[B]]{
type R = B
def apply[A](ma: C[A], f: A => Option[B]) = for{
a <- ma
v <- f(a)
} yield v
}
}
trait LowPriorityCKnot extends LowestPriorityCKnot{
implicit def trav[C0[_] <: GenTraversable[_],B]: Aux[C, C0[B], C[B]] =
new CKnot[C, C0[B]]{
type R = C0[B]
def apply[A](ma: C[A], f: A => C0[B]) = (ma map f) filter (_.nonEmpty)
}
}
trait LowestPriorityCKnot{
type C[X] <: GenTraversableLike[X, C[X]]
type Aux[G[_], B, R0] = CKnot[G, B]{ type R = R0 }
implicit def any[B]: Aux[C, B, B] =
new CKnot[C, B]{
type R = B
def apply[A](ma: C[A], f: A => B) = ma map f
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment