Skip to content

Instantly share code, notes, and snippets.

@rjsvaljean
Last active March 25, 2017 17:06
Show Gist options
  • Select an option

  • Save rjsvaljean/349371adb87ac7f5c93b18633e438f14 to your computer and use it in GitHub Desktop.

Select an option

Save rjsvaljean/349371adb87ac7f5c93b18633e438f14 to your computer and use it in GitHub Desktop.
Recursion Schemes using Matryoshka
name := "mat-test"
resolvers += Resolver.bintrayRepo("com.slamdata", "matryoshka-core")
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
"com.slamdata" %% "matryoshka-core" % "0.18.2"
)
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