Instantly share code, notes, and snippets.

What would you like to do?
FizzBuzz with no Conditionals in Java 8 with RxJava
// Basically a Java copy of
import rx.Observable;
import java.util.Arrays;
import java.util.function.Function;
public class FizzBuzz {
public static void main(String[] args) {
Function<Integer,String> f = a -> "Fizz";
Function<Integer,String> b = a -> "Buzz";
Function<Integer,String> fb = a -> "FizzBuzz";
Function<Integer,String> s = Object::toString;
Observable<Function<Integer,String>> fns = Observable.from(Arrays.asList(s, s, f, s, b, f, s, s, f, b, s, f, s, s, fb)).repeat();
Observable.range(1, 100)
.zipWith(fns, (n, fn) -> fn.apply(n)).subscribe(System.out::println);

This comment has been minimized.

Copy link

russel commented Apr 4, 2015

Is RxJava necessary here, I feel this ought to be doable like this using OpenJDK alone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment