A small tour through ScalaRx (Part I)

Today’s post is dedicated to a library based on reactive functional programming: ScalaRx.

The main objective of this library is that, with the use of values (val), we are going to define an operations flow, which will change dynamically when any of the values changes. We call those values reactive variables.

To use the library, it is enough to add it as a dependency:

libraryDependencies += "com.lihaoyi" %%% "scalarx" % "0.3.0"

and to import rx:

import rx._

Reactive variables

Reactive variables will be the main elements we will be able to work with. To define them, the constructor Var will be used:

val reactiveVar = Var(0)

This way, we are defining a reactive value containing a 0. As can be observed, we can use val instead of var, even though the value will change. This is so because the state of the reactive variable is handled with ScalaRx.

Once we have a reactive variable, we can combine it with other static values or other reactive variables. So that our mastery and expertise can be noticed, we’ll perform a complex operation. We’ll be doing the sum of two reactive variables.

val sum1 = Var(1)
val sum2 = Var(4)
val result = Rx{ sum1() + sum2() }
result.now // Returns 5

In order to make reference to the use of the value of the variable in a reactive way, parenthesis are used. Furthermore, given that we want that the value of result changes as a function of the reactive values sum1 and sum2, we enclose it within an Rx block. If we want to evaluate result just after its definition, we’ll be able to do so with the now method. We will obviously be getting a wonderful 5.

Everything looks pretty simple right now. Let’s try to see how reactive changes do work. To do so, let’s change the value of the first summand. For that purpose, we will use again the parenthesis to make reference to the fact that we are changing the value contained in the reactive variable.

sum1() = 2

This update of the first summand will cause a reactive change that will affect the whole flow defined. In this case, it will only affect the result value.

result.now // Returns 6

This has been an extremely simple example. However, more complex flows could be created so that, when the value of a reactive variable is updated and following a ripple effect, a reaction is triggered at the rest of elements.


You may now be thinking “I already do this by replacing the reactive variables by our var friends and done”. Well, that’s true. However, Rx is intended to get notifications of the changes in the reactive variables and be able to respond to them (here you will find a more complete explanation). Observables are used for this purpose. We’ll have to wait to the second part of the post to get to them.

See you soon!


One thought on “A small tour through ScalaRx (Part I)


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s