lihaoyi / scala.rx Goto Github PK
View Code? Open in Web Editor NEWAn experimental library for Functional Reactive Programming in Scala
An experimental library for Functional Reactive Programming in Scala
Would be nice if this:
val a = Var(1); val b = Var(2)
val c = Rx{ a() + b() }
could be written as:
val c = a + b
See how it is implemented in MetaRx: https://github.com/MetaStack-pl/MetaRx/blob/master/shared/src/main/scala/pl/metastack/metarx.scala#L49
Could you please outline in the readme pros and cons of your implementation versus Netflix's one: https://github.com/Netflix/RxJava/tree/master/language-adaptors/rxjava-scala
It seems strange that you've missed the elephant in the room and didn't mention RxJava in the readme at all.
When first using for
-syntax on Rx
, it seemed to work the same as with combining them in Rx{}
. But they behave differently when using a diamond-shape flow-graph:
b
/ \
source a o trigger
\ /
c
Here is an example code:
println("initializing...")
val rxa = Var(2)
val rxb = rxa.map(_ + 1)
val rxc = rxa.map(_ + 1)
for (
b <- rxb;
c <- rxc
) {
println(s" for loop triggered: $b, $c")
}
Rx {
val b = rxb()
val c = rxc()
println(s" rx triggered: $b, $c")
}
println("initialization done")
println("set rxa to 12...")
rxa() = 12
println("set rxa to 22...")
rxa() = 22
println("set rxa to 32...")
rxa() = 32
And here the printed result:
initializing...
for loop triggered: 3, 3
rx triggered: 3, 3
initialization done
set rxa to 12...
rx triggered: 13, 13
for loop triggered: 13, 13
for loop triggered: 3, 13
set rxa to 22...
rx triggered: 23, 23
for loop triggered: 23, 23
for loop triggered: 13, 23
for loop triggered: 3, 23
set rxa to 32...
rx triggered: 33, 33
for loop triggered: 33, 33
for loop triggered: 13, 33
for loop triggered: 3, 33
for loop triggered: 23, 33
The for-loop prints for every individual change of a,b and itself, while the Rx-version only prints when the calculation wave has finished. Even more, the for-loop leaks Observers for b. This looks like a bug to me. Shouldn't the for-loop ideally behave exactly like the Rx-version?
Sometimes one part of the code wants read/write access, but it only wants other part of the code to get read/access with the ability to observe. It would be useful if one could transform a Var
to an Rx
in such a way that it cannot be downcast back to the Var
.
Obs set on Vars are being garabage colected while the observed Var isn't. Is it normal ? I would think that an observable set on a Var should have the same lifecycle as the observed Var.
For example, this code has different behaviour wether we call System.gc or not:
object Test extends App {
val l: Seq[Var[Int]] =
(0 until 10).map { v => Var(v) }
l.foreach {
v => Obs(v) { println("observed " + v()) }
}
System.gc() // changes the code behaviour
l.head() = 9
}
Is the user suposed to keep strong ref to the Obs he defines ?
Please, move the library to scalaJS 0.6-RC1.
P.S. If you do not have time and are ok with substitution of utest.jsrunner.JsCrossBuild to CrossProject I can make a PR on that.
I wonder is there a way to tweak how propagation works?
In my case I want to avoid propagation if the same values are pushed to creatives (for instance fooVar equals 1 and want to avoid propogation if I do fooVar()=1) globably (without a need to say something like val bar = fooVar.filter(v=>v!=fooVar.now) for each reactive variable
Maven Central shows only version 0.10. It's either the wrong version number or the Jars for version 0.20 need toe be uploaded.
Just an idea.
In Excel there is a concept of array formulas. This would be an interesting concept to have in Scala Rx. These can be treated as vectors / matrices / arrays / grouping where if you do a operation on the grouping this will be applied to all its members. Also allow vars could be put into multiple grouping and them to have the notion of dimensionality with the view of implementing linear algebraic operations on them. Excel has some basic matrix operations which might give an idea.
I'm executing the following ammonite script:
import $ivy.`com.lihaoyi::scalarx:0.3.2`, rx._
object Example {
val v1 = Var(0)
val v2 = v1.map(identity)
val v3 = v1.map(identity).map(identity).map(identity)
def q(implicit trackDependency: Ctx.Data) = {
if (v1() == 0) v2()
else {
val b = v3() != v2()
if (b)
103
else
17
}
}
val v = Rx { q }
val list = {
var result = List.empty[Int]
val obs = v.trigger { result = result :+ v.now }
v1() = 1
v1() = 2
result
}
}
println(Example.list)
The output of this code is nondeterministically sometimes List(0, 103, 17)
and sometimes List(0, 17)
. (actually, it seems to be List(0, 17)
after the first run and List(0, 103, 17)
for already compiled runs).
Only List(0, 17)
is the correct result AFAICT, because 103 should never happen. trigger
is supposed to be called only after all Rx's have stabilized as presented in the README.
It should only be called when v1
, v2
, v3
have the same value, thus b
should be always false at the time the Obs
is reevaluated. Hence, we should be in the 17 case and v.now
should be 17.
It would be great if we could update a Var in one function in atomic style.
The current update does not deliver the current value, leading to such code:
val model = Var("world")
model.update {
val v = model()
v.toUpperCase
}
It would be easier to write:
val model = Var("world")
model.update { v =>
v.toUpperCase
}
Or
val model = Var("world")
model.update(_.toUpperCase)
Yep.
I would expect that updating a Var
to a value equal to its previous value won't trigger Obs
depending on this Var
. However:
val v = Var(10)
val o = v.foreach(println) // prints 10
v() = 11 // prints 11
v() = 11 // again prints 11!
v() = 11 // and again
When Var
is pulled through an Rx
it works correctly:
val v = Var(10)
val w = Rx { v() }
val o = w.foreach(println) // prints 10
v() = 11 // prints 11
v() = 11 // nothing, just as expected
v() = 11 // again, nothing
Please, publish for Scala 2.12
When I used scala.Rx in Ammonite.
I did only import, then Var(1), then Var(2)
yury@yury-HP-ProBook-450-G3:~$ amm
Loading...
Welcome to the Ammonite Repl 0.8.1
(Scala 2.12.1 Java 1.8.0_111)
yury-yury@ import $ivy.com.lihaoyi::scalarx:0.3.2
, rx._
import
yury-yury@ Var(1)
res1: Var[Int] = Var@51(1)
yury-yury@ Var(2)
cmd2.sc:8: exception during macro expansion:
scala.ScalaReflectionException: object Internal encapsulates multiple overloaded alternatives and cannot be treated as a method. Consider invoking <offending symbol>.asTerm.alternatives
and manually picking the required method
at scala.reflect.api.Symbols$SymbolApi.asMethod(Symbols.scala:228)
at scala.reflect.api.Symbols$SymbolApi.asMethod$(Symbols.scala:222)
at scala.reflect.internal.Symbols$SymbolContextApiImpl.asMethod(Symbols.scala:88)
at derive.Derive.$anonfun$getArgSyms$3(Derive.scala:391)
at derive.Derive.$anonfun$getArgSyms$3$adapted(Derive.scala:391)
at scala.collection.Iterator.find(Iterator.scala:981)
at scala.collection.Iterator.find$(Iterator.scala:978)
at scala.collection.AbstractIterator.find(Iterator.scala:1406)
at scala.collection.IterableLike.find(IterableLike.scala:78)
at scala.collection.IterableLike.find$(IterableLike.scala:77)
at scala.reflect.internal.Scopes$Scope.find(Scopes.scala:51)
at derive.Derive.$anonfun$getArgSyms$1(Derive.scala:391)
at scala.util.Either$RightProjection.flatMap(Either.scala:719)
at derive.Derive.getArgSyms(Derive.scala:387)
at derive.Derive.onFail$1(Derive.scala:155)
at derive.Derive.$anonfun$deriveType$5(Derive.scala:212)
at scala.collection.mutable.HashMap.getOrElseUpdate(HashMap.scala:79)
at derive.Derive.rec$1(Derive.scala:185)
at derive.Derive.deriveType(Derive.scala:230)
at derive.Derive.derive(Derive.scala:60)
at pprint.Internals$LowerPriPPrint$.liftedTree1$1(PPrint.scala:408)
at pprint.Internals$LowerPriPPrint$.FinalRepr(PPrint.scala:408)
.print(res2, res2, "res2", _root_.scala.None)
^
Compilation Failed
Please, publish for 0.5RC1
I wonder is there a way to turn of console messages from Observables in ReactiveJS? I am implementing rx-based scalajs data-binding right now and getting a lot of console messages is very irritating.
Maybe it will be better to provide logger for reactive variable system instead?
In my libraries (like scala-js-binding ) I often nead to subscribe to Rx-ses of collections of items and also react to the changes inside of items themselves.
So, I really, really, need a nice memory-leack-safe way to subscribe both to collection of rx-es and to each Var[Item] separately. I have something like:
val items: Rx[List[Var[Item]] = Rx{/*some code*/}
val views = Rx{ items() /*some other code*/
So, when new items are added to the collection , HTML view are created and binded to the item. Whenever an item is deleted, both view and its subscription to the item should also be deleted
Usage:
val va = Var(0)
val vb = Var(0)
val vc = Var(0)
val zipped: Rx[(Int, Int, Int)] = va.zip(vb, vc)
zipped would change for a change in all inputs. See similar in MetaRx: https://github.com/MetaStack-pl/MetaRx/blob/d35ee24afaaa49ee044a1d991d2c096c7acbffed/shared/src/main/scala/pl/metastack/metarx/Channel.scala#L134
Maybe this doesn't work the way I'm expecting it to. It appears to me that the before is always an empty list. Am I doing something wrong?
Code:
val l = Var(ListString)
val before = l.reduce{(b,a) => println("before: "+b); b}
val after = l.reduce{(b,a) => println("after: "+a); a}
l() = l() :+ "9"
l() = l() :+ "2"
Results:
after: List(9)
before: List()
after: List(9,2)
before: List()
The first example on the readme.md does not deliver the point what is so great about Scala.RX. We had the following dicussion on stackoverflow where the too simple example confused a user into thinking Scala.RX is about memoization.
Please consider to update example, so it shows asynchronous notifications. Of course you can use the example I have provided on SO:
import rx._
import rx.ops._
val a = Var(1); val b = Var(2)
val c: Rx[Int] = Rx{ a() + b() }
val o = c.foreach{value =>
println(s"c has a new value: ${value}")
}
a()=4
b()=12
a()=35
which prints
c has a new value: 3
c has a new value: 6
c has a new value: 16
c has a new value: 47
It would be great to see it updated to 0.5M2
I'd like to have some feedback for an extension to scala.rx:
PR: #84
The motivation goes like this:
I'm using reactive programming for data consistency to avoid manual bookkeeping of changes.
Example:
val list = Var(List(17,4,23))
val selected = Var[Option[Int]](17) // should only contain an existing element from list
In this case the data can become inconsistent if the selected element is removed from list
. It is also possible to set selected
to an element which never existed.
As an initial solution, I add a reactive consistency layer, which makes sure that selected
only contains elements from list
:
val list = Var(List(17,4,23))
val rawSelected = Var[Option[Int]](17)
val selected = selected.filter(list contains)
Now I have an Rx
where I only can read consistent data from.
Thinking further there are some issues with this approach:
rawSelected
selected
and write to rawSelected
To overcome these issues, the idea is to wrap both Var
and Rx
in a datastructure RxVar
which behaves like a Var
. The difference is that reading is mapped to the consistent Rx
, while writing is mapped to the inner Var
. Transformations like map
and filter
only operate on the Rx
-Part and result in another RxVar
where I can still write to.
The Example above would be implemented like this:
val list = Var(List(17,4,23))
val selected = RxVar(None:Option[Int]).filter(list contains)
This gives some safety guarantees:
RxVar
which I can use to read and writeIt was not possible to implement this extension without forking, since
Rx
is a sealed traitVar
which I could use to let RxVar
behave like a Var
.I'd like to hear what you think of this approach and if such a structure should become part of scala.rx.
I can not find out where is the for-loop
with condition check, from description,
in the corresponding code example
Version 0.3.x introduces the concepts of "ownership context" and "data context". I have not found any substantial (real-world) examples for these concepts, like the ones demonstrated here in a presentation for version 0.2.5.
Any help on this will be much appreciated.
Also, in the code below, why does c
require an owning context?
import rx._
val a = Var(1)
val b = Var(2)
val c = Rx { a() + b() }
published 0.3.2, so we should update documentation.
I noticed that you had said on other issues that you wanted to rewrite, and then saw that you made what seems to be a lot of progress on the 0.3.0 branch. For sake of others like me who may wonder about status and what to expect, could you comment on goals of 0.3 and the scope of changes you anticipate? Are there any problems that stalled you, other than time?
Thanks!
Let's say we have three Rx
s and we want to perform side-effects when any of them changes:
val va = Var(0)
val vb = Var(0)
val vc = Var(0)
for { a <- va ; b <- vb ; c <- vc }
effectful(a, b, c)
This obviously doesn't work since this creates a new Obs
every time va
or vb
changes.
Currently I'm resorting to creating a Rx
that bundles all Rx
s into a tuple, and then create an Obs
from it:
for { (a, b, c) <- Rx { (va(), vb(), vc()) } }
effectful(a, b, c)
While this works, it seems to me that it is a pretty inelegant solution. Is there any possibility to provide a combinator or a better abstraction for this pattern? I believe this is a very common pattern and the library should provide an easy way to solve that.
Thanks.
I try to avoid adding (implicit ctx: Ctx.Owner)
to each class with following approach:
class Test {
implicit val ctx: Ctx.Owner = Ctx.Owner.safe()
var count = 0
val a = Var(1); val b = Var(2)
def mkRx(i: Int)(implicit ctx: Ctx.Owner) = Rx { count += 1; i + b() }
val c = Rx {
val newRx = mkRx(a())
newRx()
}
println(c.now, count)
a() = 4
println(c.now, count)
b() = 3
println(c.now, count) //(7,5) -- 5??
(0 to 100).foreach { i => a() = i }
println(c.now, count)
b() = 4
println(c.now, count) //(104,211) -- 211!!!
}
This seams to work but I don't know if this is really correct or if there is a better way to do this.
Hi,
I ugraded to 0.3.1, and I am asked for providing with Ctx.Data. I did this in the right functions, but at the top of my calls (typically in the main()) I would need to pass an implementation of this Ctx.Data, and it does not seem to have one (whereas there is scala Ctx.Owner.safe()
for Owner).
Thanks
Is it possible to have ability to map multiple Vars to be redirected to a function. When all the parameters a variable the function is called and results passed on.
In the section where the Timer is explained, the text below the example references a for-loop that does not exist (line 666 I believe)
Let's say I have a custom Extractor and a Var:
object Extractor {
def unapply(x: Int): Option[Int] = Some(x + 1)
}
val a = Var(1)
Then the following code raises a comile error:
Rx {
a() match {
case Extractor(x) => x
}
}
Error:
[error] unexpected UnApply Extractor.unapply(<unapply-selector>) <unapply> ((x @ _))
See the functionality in Knockout.js: http://knockoutjs.com/documentation/rateLimit-observable.html and in MetaRx: tindzk/MetaRx@4cc7930
It should ensure that the latest value always is propagated.
Instead of making b return 5 in this sample code, why not make it return None?
val a = Var(5)
val b = a filter (_ > 5)
Current implementation of Var[T]
looks more natural to be covariant, and it is needed for my use case.
Some timing tests fail sporadically. The one that fails roughly 50% of the time is
[info] 36/67 rx.GcTests.timersGetGced Failure(utest.AssertionError: (eventually count == 3
[info] count: Int = 2,null))
I stumbled upon some other failures when running the tests repeatedly for the issue above. These occured less frequently:
[info] 34/67 rx.EventedTests.timerShouldEmitEvents Failure(utest.AssertionError: (eventually eventually(t() == i)
[info] t: rx.ops.Timer[Unit] = rx.ops.Timer@11ee4e44
[info] i: Int = 3,null))
[info] 34/67 rx.EventedTests.timerShouldEmitEvents Failure(utest.AssertionError: (eventually eventually(t() == i)
[info] t: rx.ops.Timer[Unit] = rx.ops.Timer@5000ebba
[info] i: Int = 4,null))
[info] 35/67 rx.EventedTests.debounce.simple Failure(utest.AssertionError: (assert(b() == 2)
[info] b: rx.Rx[Int] = rx.ops.Debounce@20772569,null))
[info] 38/67 rx.EventedTests.debounce.longer Failure(utest.AssertionError: (assert(count == 4)
[info] count: Int = 7,null))
[info] 29/67 rx.GcTests.obsGetGCed Failure(utest.AssertionError: (eventually eventually{
[info] count: Int = 1
[info] a: rx.core.Var[Int] = rx.core.Var@64fc8c3d
[info] a: rx.core.Var[Int] = rx.core.Var@64fc8c3d
[info] oldCount: Int = 1
[info] count: Int = 2,null))
The failing test case:
val a = Var[Option[Int]](None)
val b = Var[Option[Int]](None)
val c = Rx { for (x <- a(); y <- b()) yield x + y }
a() = Some(1)
b() = Some(2)
assert (c() == Some(3))
If my understanding is correct, the problem is that after the first assignment c
doesn't change it's value, so Spinlock.ping
ignores this update thus loosing a new parent b
.
Please consider implementing change propagation between Scala.js and Scala, possibly with a typeclass for serialization, along the lines of autowire. In other words, it would be great to have FRP between the front and back ends, like autowire provides for AJAX.
I think lack of coordinators (in comparison to Reactive extensions) is one of the weakest points of ScalaRx and it is hard to extend them as most of the classes are marked as private or protected (in comparison to Reactive Extensions where there are a lot of methods I can use) there. For instance I want to make reactive that compares current and previous results and maps it to a new Type. Current Reducer does not allow mapping so I have to write my own, but most of the classes that current reducer uses are private so I cannot use them outside of Scala React =(
Moving the example from the Readme into a class without adding an explicit owner in mkRx
leads to an leak. and no compiler error helps you to avoid this.
The class:
class Test(implicit ctx: Ctx.Owner) {
var count = 0
val a = Var(1); val b = Var(2)
def mkRx(i: Int) = Rx { count += 1; i + b() }
val c = Rx {
val newRx = mkRx(a())
newRx()
}
println(c.now, count)
a() = 4
println(c.now, count)
b() = 3
println(c.now, count) //(7,5) -- 5??
(0 to 100).foreach { i => a() = i }
println(c.now, count)
b() = 4
println(c.now, count) //(104,211) -- 211!!!
}
I don't know if there is a way to handle this or it's known issue, but I thought it should be mentioned.
Not having standard safe implicits is a barrier to using scalarx with scalajs. Below are links to several implementations people have come up with. It would be nice to have some standard implicits that we could refer to and that the community could improve over time.
Observations:
Implementations:
I updated a ScalaJS code to Scala 0.4.1
@JSExport
object HelloWorldExample {
val model = Var("world")
and I got the following error:
Uncaught TypeError: undefined is not a function Flow.scala:37
ScalaJS.impls.rx_core_Emitter$class__$init$__Lrx_core_Emitter__V Flow.scala:37
ScalaJS.c.rx_core_Var.init___Lscala_Function0__T Core.scala:128
ScalaJS.c.rx_core_Var$.apply__Lscala_Function0__T__Lrx_core_Var Core.scala:109
ScalaJS.c.example_helloworld_HelloWorldExample$.init___ HelloWorldExample.scala:13
ScalaJS.modules.example_helloworld_HelloWorldExample HelloWorldExample.scala:10
(anonymous function)
Hi,
Nice to see a complex concept condensed into a elegant form. I've been looking around for similar thing in Python. By any chance, you have came across similar implementation in Python. ?
Rx#apply()
scaladocs says : If this is called within the body of another [[Rx]], this will create a dependency between the two [[Rx]]s
And it works here :
val a = Var(1)
val rx = Rx { a() }
rx.foreach(
value => {
println("change")
}
, skipInitial = true
)
a() = a() + 1
// "change" printed
but the same example doesn't work for ArrayBuffer :
val arr = Var(new ArrayBuffer[String](20))
val rx = Rx { arr() }
rx.foreach(
value => {
println("change")
}
, skipInitial = true
)
arr() = arr() += "whatever"
// doesn't print anything
Tried both JS and JVM ... Maybe I'm missing some fundamental fact, but I can't figure it out...
I am using scala react for databiding in my scalajs app. It is easy to use in case of binding to particular values but it is not clear for me how to make collections reactive.
Consider I have something like:
val myCollection:Var[Map[String,MyCaseClass]]
and I have to update DOM when there are some changes there. In such case I have to make something like myCollection.reduce(updatehandler _) that would compare old and new versions and be used by Observer that will react on changes ( move elements in dom tree, delte from DOM, add to dom)
I think that this usecase is very common (not only for scalajs but in other cases when we want to look what changes in collections happened and react) to make some helpers for that.
In the same time I am not sure how to implement it right. The most stratiforward approach is to iterate whole collection and produce Inserted/Deleted/Moved events that would include elements that changed. But maybe there is a better way for this?
Benchmarks (https://groups.google.com/forum/#!topic/scala-js/3KixczxoxCU) show that for simple propagators, it takes 5-10x as long to propagate a change as it takes to initialize the graph. To a large extent this defeats the purpose of using change propagation in the first place. When I end up rewriting this, I need to fix it.
It is hard to understand how all those macroses in Operators work, it would be nice to have an example in Documentation explaining one of them and giving recommendations how to write your own OPerators
Sorry - normally I'd submit a pull request for this kind of thing but unfortunately I don't have time.
Many people new to the project will start by working through the examples in the README
so it's relatively important that it be mistake free. Here are some issues I found:
1. I had to change the following code:
intercept[ArithmeticException]{
b()
}
assert(b.toTry.isInstanceOf[Failure])
To this:
intercept[ArithmeticException]{
b.now // Otherwise it complains "No implicit Ctx.Data is available here!"
}
assert(b.toTry.isInstanceOf[Failure[_]]) // Otherwise it complains "class Failure takes type parameters "assert(b.toTry.isInstanceOf[Failure])
2. An unknown function inside
is referenced a number of times, e.g. like so:
inside(c.toTry){case Success(0) => () }
I think occurances of inside
should be replaced with assertMatch
assertMatch(c.toTry){case Success(0) => ()}
But then it's probably necessary to go on and explain that to use assertMatch
you have to extend TestSuite
from https://github.com/lihaoyi/utest
import utest._
object XyzTests extends TestSuite {
val tests = this {
"pqrTest" - {
assertMatch(b.toTry){case Success(0)=>}
}
}
}
3. In the following code why is a
wrapped in another Rx
when creating b
- isn't a
already a perfectly acceptable Rx
?
val a = Var(1)
val b = Rx{
(Rx{ a() }, Rx{ math.random })
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.