Created
July 19, 2015 11:23
-
-
Save JohannesRudolph/2379909c9b0fc89bd522 to your computer and use it in GitHub Desktop.
ReScala basiscs
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 rescala._ | |
| import rescala.events.{ Event, ImperativeEvent } | |
| import makro.SignalMacro.{SignalM => Signal} | |
| object ReScalaDemo extends App { | |
| /* | |
| Observer criticism | |
| - inverts natural dependency order (listeners call soure to register) | |
| - lots of boilerplate code | |
| - no composition of events and handlers | |
| - scattering and tangling of trigger code, inconsistent firing | |
| */ | |
| /*** Events ***/ | |
| val e1 : ImperativeEvent[Int] = new ImperativeEvent[Int]() | |
| e1 += (x => println("x:" + x)) // register handler | |
| e1(1) // firing the event, passing 1 as value | |
| // handler execution order is not guaranteed deterministic | |
| val e2 : Event[Int] = new ImperativeEvent[Int]() | |
| val e3 : Event[Int] = e1 || e2 // declarative event, fired when e1 or e2 fire | |
| // create new event with evt. parameter mapping | |
| val e4 : Event[String] = e1 map ( (x:Int) => x.toString) | |
| /*** Vars ***/ | |
| // vars: primitive reactive values, manual update | |
| val v1 : Var[Int] = Var(1) | |
| val v2 : Var[Int] = Var(2) | |
| v2()= 3// assign 3 to v2 | |
| /*** Signals ***/ | |
| // Signals: reactive expressions, updated automatically | |
| val s1 = Signal { | |
| // signals can depend on vars | |
| v1() + v2() | |
| // should be side-effect free!!! | |
| } | |
| val s2 = Signal { | |
| // signals can depend on other signals | |
| s1() * s1() | |
| } | |
| s1.get // retrieve current value of signal | |
| val s1e : Event[Int] = s1.changed // signal -> event | |
| val e1s : Signal[Int] = e1.latest(0) // event -> default value -> signal | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment