Skip to content

Instantly share code, notes, and snippets.

@netzwerg
Created May 1, 2016 05:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netzwerg/2ec7fb277320408dd7322844c9a19a6f to your computer and use it in GitHub Desktop.
Save netzwerg/2ec7fb277320408dd7322844c9a19a6f to your computer and use it in GitHub Desktop.
FizzBuzz in Java 8 with Javaslang
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);
}
}
fizz
202
203
fizz
buzz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment