Skip to content

Instantly share code, notes, and snippets.

@aztek
Last active December 16, 2015 03:59
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aztek/5374313 to your computer and use it in GitHub Desktop.
Save aztek/5374313 to your computer and use it in GitHub Desktop.
Trivial FRP for Scala with scala-idioms
import idioms._ // http://github.com/aztek/scala-idioms
trait Cell[T] {
def ! : T
def := (value: T) { throw new UnsupportedOperationException }
}
val frp = new Idiom[Cell] {
def pure[A](a: ⇒ A) = new Cell[A] {
private var value = a
override def := (a: A) { value = a }
def ! = value
}
def map[A, B](f: A ⇒ B) = a ⇒ new Cell[B] {
def ! = f(a!)
}
def app[A, B](f: Cell[A ⇒ B]) = a ⇒ new Cell[B] {
def ! = f!(a!)
}
}
idiom (frp) {
val a = $(10)
val b = $(5)
val c = $(a + b * 2)
println(c!) // 20
b := 7
println(c!) // 24
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment