Create a gist now

Instantly share code, notes, and snippets.

FizzBuzz with no Conditionals in Java 8 with RxJava
// Basically a Java copy of https://twitter.com/adereth/status/530740818420957184
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);
}
}
@russel
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