Last active
December 16, 2015 03:59
-
-
Save aztek/5374313 to your computer and use it in GitHub Desktop.
Trivial FRP for Scala with scala-idioms
This file contains hidden or 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
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