Created
May 31, 2014 03:02
-
-
Save mxswd/309593a79ea95ad27247 to your computer and use it in GitHub Desktop.
This file contains 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
sealed trait Shape[P] | |
case class Void[P]() extends Shape[P] | |
case class Unit[P]() extends Shape[P] | |
case class Value[P, A](a: A) extends Shape[P] | |
case class Meta[P, T <: Tag](shape: Shape[P]) extends Shape[P] | |
sealed trait Sum[P] extends Shape[P] { | |
val shape: Shape[P] | |
} | |
case class Left[P](shape: Shape[P]) extends Sum[P] | |
case class Right[P](shape: Shape[P]) extends Sum[P] | |
case class Product[P](left: Shape[P], right: Shape[P]) extends Shape[P] | |
sealed trait Tag | |
trait Datatype extends Tag | |
trait Constructor extends Tag | |
trait RecordSelector extends Tag | |
// inu | |
sealed trait Dog | |
case class ShibaInu(name: String) extends Dog | |
case object NotInu extends Dog | |
object Main { | |
def genericDogFrom(dog: Dog) = dog match { | |
case ShibaInu(name) => Meta[Dog, Datatype](Left[Dog](Meta[Dog, Constructor](Meta[Dog, RecordSelector](Value[Dog, String](name))))) | |
case NotInu => Meta[Dog, Datatype](Right[Dog](Meta[Dog, Constructor](Unit[Dog]()))) | |
} | |
// json | |
def toJSON[A](rep: Shape[A]): String = rep match { | |
case Void() => "{}" | |
case Unit() => "{}" | |
case Value(x) => "\"" ++ x.toString ++ "\"" | |
case Meta(v) => toJSON(v) | |
case Left(s) => s"[false, ${toJSON(s)}]" | |
case Right(s) => s"[true, ${toJSON(s)}]" | |
case Product(l, r) => s"[${toJSON(l)}, ${toJSON(r)}]" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment