Book Image

Learning Reactive Programming with Java 8

By : Nickolay Tzvetinov
Book Image

Learning Reactive Programming with Java 8

By: Nickolay Tzvetinov

Overview of this book

Table of Contents (15 chapters)
Learning Reactive Programming with Java 8
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Implementing the reactive sum example with lambdas


So this time, our main piece of code will be quite similar to the previous one:

ConnectableObservable<String> input = CreateObservable.from(System.in);

Observable<Double> a = varStream("a", input);
Observable<Double> b = varStream("b", input);

reactiveSum(a, b); // The difference

input.connect();

The only difference is that we are going to take a more functional approach in calculating our sum and not to keep the same state. We won't be implementing the Observer interface; instead, we are going to pass lambdas to subscribe. This solution is much cleaner.

The CreateObservable.from(InputStream) method is a lot like we used previously. We will skip it and look at the Observable<Double> varStream(String, Observable<String>) method, which creates the Observable instances representing the collectors:

public static Observable<Double> varStream(
  final String name, Observable<String> input) {
    final Pattern...