Created
June 13, 2014 17:48
-
-
Save epb-644/2c35edf2b63f1b5306d3 to your computer and use it in GitHub Desktop.
rxjava version of FIZZBUZZ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rx.Observable; | |
import rx.observables.ConnectableObservable; | |
public class RxFizzBuzz { | |
public static void main(String[] args) { | |
final ConnectableObservable<Integer> range = Observable.range(1, 100).publish(); | |
final Observable<String> out = Observable.merge( | |
range.filter((n) -> n % 15 == 0).map((n) -> "FIZZBUZZ"), | |
range.filter((n) -> n % 15 != 0 && n % 3 == 0).map((n) -> "FIZZ"), | |
range.filter((n) -> n % 15 != 0 && n % 5 == 0).map((n) -> "BUZZ"), | |
range.filter((n) -> n %3 != 0 && n % 5 != 0).map((n) -> Integer.toString(n))); | |
out.subscribe((str) -> System.out.println(str)); | |
range.connect(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment