EzDevInfo.com

scala.rx

An experimental library for Functional Reactive Programming in Scala

What is the difference between Reactive programming and plain old closures?

Example from scala.rx:

import rx._
val a = Var(1); val b = Var(2)
val c = Rx{ a() + b() }
println(c()) // 3
a() = 4
println(c()) // 6

How is the above version better than:

var a = 1; var b = 2
def c = a + b
println(c) // 3
a = 4
println(c) // 6

The only thing I can think of is that the first example is efficient in the sense that unless a or b changes, c is not recalculated but in my version, c is recomputed every time I invoke c() but that is just a special case of memoization with size=1 e.g. I can do this to prevent re-computations using a memoization macro:

var a = 1; var b = 2
@memoize(maxSize = 1) def c(x: Int = a, y: Int = z) = x + y

Is there anything that I am missing to grok about reactive programming that provides insight into why it might be a better paradigm (than memoized closures) in certain cases?


Source: (StackOverflow)

Using Timer from scala.rx

Can someone give me a simple example of using the Timer from Li Haoyi's scala.rx that doesn't depend on an Akka or any other libraries besides scalajs, dom, and rx?

The example of a Timer from Haoyi's GitHub is:

import scala.concurrent.duration._
implicit val scheduler = new AkkaScheduler(akka.actor.ActorSystem())

val t = Timer(100 millis)
var count = 0  
val o = Obs(t){
  count = count + 1
}

println(count) // 3
println(count) // 8
println(count) // 13

However, this uses Akka.

Looking in the scala.rx api, the way to create a rx.ops.Timer is:

new Timer(interval: FiniteDuration, delay: FiniteDuration)(implicit scheduler: Scheduler, p: Propagator[P], ec: ExecutionContext)

where Scheduler is a trait defined as:

abstract def scheduleOnce[T](interval: FiniteDuration)(thunk: ⇒ T)(implicit executor: ExecutionContext): Unit

The Scheduler is an Akka ActorSystem on the JVM and the setTimeout function in JavaScript."

Though all the information in the api is useful, I still can't get the right syntax for a simple timer.


Source: (StackOverflow)

Advertisements

Where is monadic bind/flatMap in Scala.rx?

I just watched this talk : http://vimeo.com/98477272

It made me wonder how flatMap/bind can be implemented in Scala.rx ?

More specifically, it seems that Rx { } is a map operation on Vars but what if I have a type of Var[Var[Int]] ? How can I make it into a Var[Int] ?

In other words Var corresponds to RX.Net's Observable and Rx { } corresponds to RX.Net's map on Observables. But how do I get RX's flapMap/bind ?


Source: (StackOverflow)

`eventually` not found in scala.rx

Here is an example from the scala.rx doc:

package tutorial.webapp

import rx.core.{Rx, Var}
import rx._
import rx.ops._

import scala.concurrent.Promise
import scala.concurrent.duration._
import scala.scalajs.js.JSApp
import scala.scalajs.js.annotation.JSExport
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by IDEA on 29/10/15.
 */
object RxAddtionalOps extends JSApp {
  @JSExport
  override def main(): Unit = {
    mapDemo
    filterDemo
    asyncDemo
    async2
    timer1
  }

  def delay1: Unit = {
    val a = Var(10)
    val b = a.delay(250 millis)
    a() = 5
    println(b())
    eventually{
      println(b)
    }
  }
}

I got this error on sbt when compiling:

[error] /Users/kaiyin/personal_config_bin_files/workspace/scalajsHandson/src/main/scala/tutorial/webapp/RxAddtionalOps.scala:43: not found: value eventually
[error]     eventually{
[error]     ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 1 s, completed 30 oct. 2015 11:15:07

Where does the eventually function come from? Am I missing any imports?


Source: (StackOverflow)

Set a limit to scala.rx timer

Here is an example using Timer from scala.rx:

package tutorial.webapp

import akka.actor.ActorSystem
import rx.core.{Rx, Var}
import rx._
import rx.ops._

import scala.concurrent.Promise
import scala.concurrent.duration._
import scala.scalajs.js.JSApp
import scala.scalajs.js.annotation.JSExport
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by IDEA on 29/10/15.
 */
object RxAddtionalOps extends JSApp {
  @JSExport
  override def main(): Unit = {
    timer1
  }

  def timer1: Unit = {
    implicit val scheduler = new DomScheduler        
    val t = Timer(100 millis)
    var count = 0
    val o = Obs(t){
      count = count + 1
      println(count)
    }
  }
}

When you run runMain tutorial.webapp.RxAddtionalOps from sbt, the console will be indefinitely blocked. Can I set a limit to the timer? For example, to make it stop emitting events in 2 minutes.


Source: (StackOverflow)