Created
May 15, 2013 16:42
-
-
Save YoEight/5585354 to your computer and use it in GitHub Desktop.
cartesian product using anamorphism
This file contains hidden or 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
object ex { | |
def ana[A, B](start: B)(f: B => Option[(A, B)]): List[A] = f(start) match { | |
case Some((a, b)) => a :: ana(b)(f) | |
case _ => Nil | |
} | |
def cartesian[A](xss: List[List[A]]): List[List[(A, A)]] = ana(xss) { | |
case xs :: vs :: rest => | |
val action = for { | |
x <- xs | |
v <- vs | |
} yield (x, v) | |
Some((action, rest)) | |
case _ => None | |
} | |
} |
cchantep
commented
May 15, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment