Created
November 5, 2011 12:30
-
-
Save einblicker/1341454 to your computer and use it in GitHub Desktop.
reactive programming
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 util.continuations._ | |
class DataflowVar[A] { | |
private var value_ : A = _ | |
private var conts: List[A => Unit] = Nil | |
def apply(): A @suspendable = | |
shift { (k: A => Unit) => | |
conts ::= k | |
k(value) | |
} | |
def value: A = value_ | |
def value_=(x: => A @suspendable): Unit = | |
reset { | |
value_ = x | |
conts.foreach(_(value)) | |
} | |
} | |
val a = new DataflowVar[Double] | |
val b = new DataflowVar[Double] | |
val c = new DataflowVar[Double] | |
val d = new DataflowVar[String] | |
import math._ | |
//データフロー変数への代入はx.value = hogeの形で行う | |
a.value = 1.0 | |
b.value = 2.0 | |
c.value = sqrt(pow(a(), 2.0) + pow(b(), 2.0)) //代入の右辺値の中で他のデータフロー変数が使える | |
d.value = "(a^2 + b^2)^1/2 = " + c().toString() | |
println(d.value) | |
//試しにこうやってaやbの値を変えてみると… | |
a.value = 12.0 | |
b.value = 34.0 | |
println(d.value) //それに連動してcやdの値も変わってくれる |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment