Skip to content

Instantly share code, notes, and snippets.

@einblicker
Created February 7, 2012 13:46
Show Gist options
  • Save einblicker/1759752 to your computer and use it in GitHub Desktop.
Save einblicker/1759752 to your computer and use it in GitHub Desktop.
object Formatter {
sealed abstract class Dir[A] {
def formatAux: String => A
def format: A = formatAux.apply("")
}
case object E extends Dir[String] {
def formatAux = identity
}
case class L[A](s: String, d: Dir[A]) extends Dir[A] {
def formatAux =
(out: String) => d.formatAux.apply(out + s)
}
case class I[A](d: Dir[A]) extends Dir[Int => A] {
def formatAux =
(out: String) =>
(i: Int) =>
d.formatAux.apply(out + i.toString)
}
case class S[A](d: Dir[A]) extends Dir[String => A] {
def formatAux =
(out: String) =>
(s: String) =>
d.formatAux.apply(out + s)
}
def test = {
println(L("hello, ", S( L("!", (E)))).format("world"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment