Last active
March 25, 2017 17:06
-
-
Save rjsvaljean/349371adb87ac7f5c93b18633e438f14 to your computer and use it in GitHub Desktop.
Recursion Schemes using Matryoshka
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
| name := "mat-test" | |
| resolvers += Resolver.bintrayRepo("com.slamdata", "matryoshka-core") | |
| scalaVersion := "2.11.8" | |
| libraryDependencies ++= Seq( | |
| "com.slamdata" %% "matryoshka-core" % "0.18.2" | |
| ) |
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
| package rxvl | |
| import matryoshka._ | |
| import matryoshka.data._ | |
| import scalaz.{Functor, \/-, -\/} | |
| object AnyListFix { | |
| type T = matryoshka.data.Fix[AnyListF] | |
| def nil: Fix[AnyListF] = Fix[AnyListF](Nil) | |
| def cons(h: Any, t: T): T = Fix[AnyListF](Cons(h, t)) | |
| def apply(xs: Any*): T = Corecursive[T, AnyListF].ana[List[Any]](xs.toList) { | |
| case scala.Nil => Nil | |
| case h :: t => Cons(h, t) | |
| } | |
| def toList(l: Fix[AnyListF]): List[Any] = Recursive[Fix[AnyListF], AnyListF].cata[List[Any]](l) { | |
| case Nil => scala.Nil | |
| case Cons(h, t) => h :: t | |
| } | |
| } | |
| trait AnyListF[+F] | |
| case class Cons[+F](head: Any, tail: F) extends AnyListF[F] | |
| case object Nil extends AnyListF[Nothing] | |
| object AnyListF { | |
| implicit def functor: Functor[AnyListF] = new Functor[AnyListF] { | |
| def map[A, B](fa: AnyListF[A])(f: (A) => B): AnyListF[B] = fa match { | |
| case Nil => Nil | |
| case Cons(h, t) => Cons(h, f(t)) | |
| } | |
| } | |
| } | |
| object Main { | |
| def main(args: Array[String]): Unit = { | |
| List( | |
| Recursive[Fix[AnyListF], AnyListF].cata[Int](AnyListFix(1,2,3,4)) { | |
| case Cons(h, t) => h.asInstanceOf[Int] + t.asInstanceOf[Int] | |
| case Nil => 0 | |
| }, | |
| Recursive[Fix[AnyListF], AnyListF].para[Int](AnyListFix(1,2,3,4)) { | |
| case Cons(h, (subList, t)) => | |
| println(s"$subList: $t") | |
| h.asInstanceOf[Int] + t.asInstanceOf[Int] | |
| case Nil => 0 | |
| }, | |
| AnyListFix.toList(Corecursive[Fix[AnyListF], AnyListF].ana[Int](10) { | |
| case 0 => Nil | |
| case n => Cons(n, n - 1) | |
| }), | |
| AnyListFix.toList(Corecursive[Fix[AnyListF], AnyListF].apo[Int](4) { | |
| case 0 => Nil | |
| case n if n == 2 => Cons(2, -\/(AnyListFix.nil)) | |
| case n => Cons(1, \/-(n - 1)) | |
| }), | |
| AnyListFix.toList(Corecursive[Fix[AnyListF], AnyListF].apo[Fix[AnyListF]](AnyListFix(1,3,4)) { | |
| case Fix(Nil) => Nil | |
| case Fix(Cons(h, t)) if h.asInstanceOf[Int] > 2 => Cons(2, -\/(AnyListFix.cons(h, t))) | |
| case Fix(Cons(h, t)) => Cons(h, \/-(t)) | |
| }) | |
| ).foreach(println) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment