Created
November 15, 2014 12:27
-
-
Save timyates/0d6b47e429023630a750 to your computer and use it in GitHub Desktop.
FizzBuzz with no Conditionals in Java 8 with RxJava
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
// 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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is RxJava necessary here, I feel this ought to be doable like this using OpenJDK alone.