Upload
faren-faren
View
340
Download
5
Embed Size (px)
Citation preview
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.
Easy analogy for FRP is a spreadsheet application.
You chain the formula, whenever the source cell is changed, the result will change too.
FRP
ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern,
and functional programming
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
Try to add another subscribershelloObservable.subscriber( new Action1<String> { @Override public void call(String s) { System.out.println("Sub2 : " + s); } });
Output:Subscriber : Hello, World!Sub2 : Hello, World!
Transformation
It’s part of functional, allow us to transform observable such as map, filter and others.Ex: (marble notation)
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!
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!
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
References
• https://github.com/faren/RxAndroidProject• https://www.ykode.com/2015/02/20/android-
frp-rxjava-retrolambda.html
• http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/