RxJava for 100% beginners(part 1)

I believe that most of the Android developers are well aware of the existence of “Reactive Programming/RxJava” nowadays. You can always see blog posts related to RxJava in every single Android Weekly email in the past few months(Im not kidding…).

However, its a bit hard for beginner who doesn’t have knowledge about functional programming to adopt the syntax from RxJava. In Scala, you can find a lot of operators similar to RxJava’s, like map(), flatmap(),filter()…. If you tried Scala or any other functional programming language (Which RxJava borrow ideas and syntaxs from)before, you may have some advantages over those who doesn’t. But in this series, Im gonna go through some basics of functional programming first in case you don’t have any idea what it is. Luckily in Java8, we have Stream API , the “Stream” class is very similar to “Observable” from RxJava, so we are gonna pick up some stuffs from Java8 Stream API in order to understand RxJava’s Observable.

So here we are, lets get started!

So for beginners, the advantages for Functional Programming can be 2 major one:

  1. Function can be used as a parameter to other functions, thats the so called “first-class object”.
  2. It treats operations like “functions” in the math, we only care about results, or output from the last function to avoid caring about state changing , mutable object ..

It treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data- Wikipedia

In RxJava/Java8 Stream API, they support lambda expressions, which reflect the first advantage above. Lets take an example of this:

a simple lambda example

lets take a look at the difference between line 11 and line 21, they both print String “action start”, but the second one is much shorted, no need to create the instance of the interface “NewAction” and override its “call” method anymore, we simply just code the method’s body and pass it to execute(). That’s not magic !

In Java 8, you can enjoy this lambda expression if the interface you wanna create instance from just contains one method, then you can just put the method body to where you wanna it to be without creating the instance of the interface in your code.

However, we are not really using function as parameter here, its just the help from the compiler that helps us to write less code, makes it looks like we are passing a function as a parameter.

Ok, lets take a look at the below sample for advantage #2!

stream from Java 8

In the example above, we transform the arraylist to a stream(this concept of stream is not the InputStream or OutputSteam from Java), and modify the stream by applying different operators. We first map every element from the stream(which is a stream of Integer object) by add 1, then filter out those Integer which are less than 10, then eventually we take only the first 10 elements from the stream. Noted that every time we apply an operator, we ”create” a new stream, and thats why we can apply those operators in a chain.

But wait, whats the advantage of having such a weird structure ?

I already mentioned that in functional programming language , we aim to not have “states” and just focus on the result of function execution.

For example we have a sequence of execution of functions :

funcA() -> funB() ->funcC();

In this case, funC() only handle the result from funcB(), and it will not use result from funcA(), this is the most ideal scenario for functional programming. Does it sounds silly? I bet thats what you wanna ask and this requirement seems not “advanced” right?

are you kidding me?

But just think about it, I believe most of the us have done something like this :

we are using mutable state here!

in the example , we use a global variable to control the execution of C(), but the variable is mutable and it has been changed in A()! So even though the sequence of the functions are A()->B()->C(), C() is not relied purely on B(), and thats against the rule of functional programming.

Maybe we can change a little bit of the code.

This is much better now ! , B() and C() don’t rely on any global variable anymore, the executions of B() and C() are now purely decided by its own parameters.

Or we can make it even more “RxJava” like

the code above demonstrates how we can make it more functional like !

But the question is -> we still have a boolean flag in Exe class ! Isnt that against the rule of functional programming?

Well, thats not true, always remember, we dont want mutable variable to take control of the execution of our methods chain , thats true. But it doesn’t mean that we can’t use global variables. In the code above, every time we fire A() , B() or C(), we return a new instance of class Exe, we are not sharing the same instance of Exe along the way, which means only the last instance of Exe will control the execution of the current method calls.

So lets review Java 8 Stream API again , those operators like map(), filter(), limit() will create a new “Stream” and pass to the next operator call. You can always just modify the stream generated by the last operator.

So thats it for part 1, in the next post im gonna go through some operators and API from RxJava.

Here’s the link for the next post :

https://medium.com/@qingzhong/rxjava-for-100-beginners-part-2-write-your-code-in-a-more-elegant-way-ccadfe61bb81

--

--

Android Developer in Viki

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store