Skip to content

Instantly share code, notes, and snippets.

@lihaoyi
Created November 16, 2017 04:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lihaoyi/e9ad1c2777d336d4b7bb3aec22525d43 to your computer and use it in GitHub Desktop.
Save lihaoyi/e9ad1c2777d336d4b7bb3aec22525d43 to your computer and use it in GitHub Desktop.
@ case class Cross[T](items: T*){
def flatMap[V](f: T => Cross[V]): Cross[(T, V)] = {
val flattened = for{
i <- items
k <- f(i).items
} yield (i, k)
Cross(flattened:_*)
}
def map[V](f: T => V): Cross[(T, V)] = {
Cross(items.map(i => i -> f(i)):_*)
}
def withFilter(f: T => Boolean) = {
Cross(items.filter(f):_*)
}
}
defined class Cross
@ for(a <- Cross(1, 2, 3)) yield a.toString
res15: Cross[(Int, String)] = Cross(ArrayBuffer((1, "1"), (2, "2"), (3, "3")))
@ for{
a <- Cross(1, 2)
b <- Cross("A", "B")
if !(a == 2 && b == "B")
} yield b * a
res16: Cross[(Int, (String, String))] = Cross(
ArrayBuffer((1, ("A", "A")), (1, ("B", "B")), (2, ("A", "AA")))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment