Skip to content

Instantly share code, notes, and snippets.

@epb-644
Created June 13, 2014 17:48
Show Gist options
  • Save epb-644/2c35edf2b63f1b5306d3 to your computer and use it in GitHub Desktop.
Save epb-644/2c35edf2b63f1b5306d3 to your computer and use it in GitHub Desktop.
rxjava version of FIZZBUZZ
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