Created
May 1, 2016 05:06
-
-
Save netzwerg/2ec7fb277320408dd7322844c9a19a6f to your computer and use it in GitHub Desktop.
FizzBuzz in Java 8 with Javaslang
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
import javaslang.collection.Stream; | |
/** | |
* An implementation of https://dierk.gitbooks.io/fregegoodness/content/src/docs/asciidoc/fizzbuzz.html | |
* using http://www.javaslang.io | |
* | |
* @author Rahel Lüthy | |
*/ | |
public class FizzBuzz { | |
public static void main(String[] args) { | |
Stream<Integer> numbers = Stream.from(1); | |
Stream<String> fizzes = Stream.of("", "", "fizz").cycle(); | |
Stream<String> buzzes = Stream.of("", "", "", "", "buzz").cycle(); | |
Stream<String> pattern = fizzes.zip(buzzes).map(t -> t._1 + t._2); | |
Stream<String> fizzbuzz = numbers.zip(pattern).map(t -> t._2.isEmpty() ? String.valueOf(t._1) : t._2); | |
fizzbuzz.drop(200).take(5).forEach(System.out::println); | |
} | |
} |
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
fizz | |
202 | |
203 | |
fizz | |
buzz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment