Skip to content

Instantly share code, notes, and snippets.

@jrudolph
Created July 21, 2011 14:18
Show Gist options
  • Save jrudolph/1097283 to your computer and use it in GitHub Desktop.
Save jrudolph/1097283 to your computer and use it in GitHub Desktop.
Show the non-Existential Of God
abstract class Action
case object Eat extends Action
case object Speak extends Action
case object Move extends Action
abstract class Creature (val action: Action)
case class Fish (a: Action, val size: String) extends Creature (a)
case class Dog (a: Action, val sound: String) extends Creature (a)
case class Man (a: Action, val name: String, val favFood: String) extends Creature (a)
trait Animate[A <: Creature] {
private def nada = println ("I don't do that")
def eat (animal: A): Unit = nada
def speak (animal: A): Unit = nada
def move (animal: A): Unit = nada
def apply (animal: A): Unit = {
animal.action match {
case Eat => eat (animal)
case Speak => speak (animal)
case Move => move (animal)
}
}
}
object SoulOfFish extends Animate[Fish] {
override def move (f: Fish) =
println ("The %s fish swims".format (f.size))
}
object SoulOfMan extends Animate[Man] {
override def eat (m: Man) =
println ("%s eats %s".format (m.name, m.favFood))
}
object SoulOfDog extends Animate[Dog] {
override def speak (d: Dog) =
println ("Dog goes %s".format (d.sound))
}
trait MoldFromClay {
type C <: Creature
def creature: C
def soul: Animate[C]
def animate () =
soul (creature)
}
object MoldFromClay {
type Into[X <: Creature] = MoldFromClay { type C = X }
}
case class MoldFromRealClay[X <: Creature](val creature: X, val soul: Animate[X]) extends MoldFromClay {
type C = X
}
object God {
def fish () : MoldFromClay.Into[Fish] =
MoldFromRealClay (Fish (Move, "Big"), SoulOfFish)
def dog () : MoldFromClay.Into[Dog] =
MoldFromRealClay (Dog (Speak, "Woof"), SoulOfDog)
def man () : MoldFromClay.Into[Man] =
MoldFromRealClay (Man (Eat, "Adam", "Pizza"), SoulOfMan)
def breathLife (day: Int): MoldFromClay =
day match {
case 5 => fish
case 6 => dog
case 7 => man
}
}
object InTheBeginning {
def genesis () = {
val allCreaturesGreatAndSmall =
Seq ( God.breathLife (5),
God.breathLife (6),
God.breathLife (7) )
allCreaturesGreatAndSmall foreach ( c => c.animate )
}
}
InTheBeginning.genesis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment