Skip to content

Instantly share code, notes, and snippets.

@timyates
Created November 15, 2014 12:27
Show Gist options
  • Save timyates/0d6b47e429023630a750 to your computer and use it in GitHub Desktop.
Save timyates/0d6b47e429023630a750 to your computer and use it in GitHub Desktop.
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
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