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, ...

Get Learning Reactive Programming with Java 8 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.