Created
April 18, 2015 19:08
-
-
Save timyates/fbb4aa0bfe1fc36fc0a3 to your computer and use it in GitHub Desktop.
Java 8 FizzBuzz with no conditionals, and no 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
package fizzbuzz; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.function.Function; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
public class FBJ8 { | |
public static void main(String[] args) { | |
Function<Integer,String> n = Object::toString; | |
Function<Integer,String> f = num -> "Fizz"; | |
Function<Integer,String> b = num -> "Buzz"; | |
Function<Integer,String> fb = num -> "FizzBuzz"; | |
List<Function<Integer,String>> fns = Arrays.asList(n,n,f,n,b,f,n,n,f,b,n,f,n,n,fb); | |
Iterator<Function<Integer,String>> funcs = Stream.generate(() -> fns).flatMap(Collection::stream).iterator(); | |
IntStream.range(1, 500) | |
.mapToObj(i -> funcs.next().apply(i)) | |
.limit(205) | |
.skip(200) | |
.forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment