2. Introduction
• Functional Programming, programming that
transform and compose stream of immutable
sequences by applying map, filter and reduce.
• Reactive Programming. This model of
programming focuses on data flow and
change propagation.
3. Easy analogy for FRP is
a spreadsheet
application.
You chain the formula,
whenever the source
cell is changed, the
result will change too.
7. Observable<String> helloObservable = Observable.create(
new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> sub) {
sub.onNext("Hello, World!");
sub.onCompleted();
};
});
helloObservable.subscribe( new Action1<String>() {
@Override
public void call(String s) {
System.out.println("Subscriber : " + s);
}
});
Observable
Observable hook
subscriber
8. Try to add another subscribers
helloObservable.subscriber( new Action1<String> {
@Override
public void call(String s) {
System.out.println("Sub2 : " + s);
}
});
Output:
Subscriber : Hello, World!
Sub2 : Hello, World!
9. Transformation
It’s part of functional, allow us to transform
observable such as map, filter and others.
Ex: (marble notation)
10. List<String> names = Arrays.asList("Jily", "Iqbal", "Dini",
"Reza", "Bagus", "Pp", "Akbar");
Observable<String> helloObservable = Observable.from(names);
helloObservable.subscribe( new Action1<String>() {
@Override
public void call( String s ) {
String greet = "Sub 1 on " + currentThreadName() +
": Hello " + s + "!";
System.out.println(greet);
}
Output:
Sub 1 on main: Hello Jily!
Sub 1 on main: Hello Iqbal!
Sub 1 on main: Hello Dini!
Sub 1 on main: Hello Reza!
Sub 1 on main: Hello Bagus!
Sub 1 on main: Hello Pp!
Sub 1 on main: Hello Akbar!
11. helloObservable.map( new Func1<String, String>() {
@Override
public String call( String s ) {
return s.toUpperCase();
}
}).subscribe( new Action1<String>() {
@Override
public void call( String s ) {
String greet = "Sub 1 on " + currentThreadName() +
": Hello " + s + "!";
System.out.println(greet);
}
});
We want to print uppercased name of the person
Output:
Sub 1 on main: Hello JILY!
Sub 1 on main: Hello IQBAL!
Sub 1 on main: Hello DINI!
Sub 1 on main: Hello REZA!
Sub 1 on main: Hello BAGUS!
Sub 1 on main: Hello PP!
Sub 1 on main: Hello AKBAR!
12. helloObservable.map( s -> s.toUpperCase() )
.subscribe( s -> {
String greet = "Sub 1 on " + currentThreadName() +
": Hello" + s + "!";
System.out.println(greet);
});
helloObservable.map( s -> s.toUpperCase() )
.map( s -> "Sub 1 on " + currentThreadName() + ": Hello " +
s + "!")
.subscribe( s -> System.out.println(s));
Concise & Concise (by lambda expression)
More readable
The formula itself does not change, but the change of value from the source cell propagates to the result cell.
1. The helloObservable will emits "Hello World".
2. helloObservable is being subscribed. The subscriber is an instance of Action1. It takes 1 parameter in form of a String and print them.
If you change the string from "Hello World!" to "Bonjour Monde!". All subscribers will print the changed code. This is the change propagation that shapes the reactive programming.