Skip to content

Instantly share code, notes, and snippets.

@jforge
Created February 26, 2016 09:36
Show Gist options
  • Select an option

  • Save jforge/e78f69dfe02bcddcc853 to your computer and use it in GitHub Desktop.

Select an option

Save jforge/e78f69dfe02bcddcc853 to your computer and use it in GitHub Desktop.
import java.util.function.Function;
import java.util.stream.IntStream;
public class FizzBuzz {
public static void main(String[] args) {
Function<Integer, Boolean> divisible3 = l -> l % 3 == 0;
Function<Integer, Boolean> divisible5 = l -> l % 5 == 0;
Function<Integer, String> outDivisibleFizz = i -> divisible3.apply(i) ? "Fizz" : "";
Function<Integer, String> outDivisibleBuzz = i -> divisible5.apply(i) ? "Buzz" : "";
IntStream.rangeClosed(1, 100).mapToObj(i -> {
String out = "";
out += outDivisibleFizz.apply(i);
out += outDivisibleBuzz.apply(i);
return out.isEmpty() ? i : out;
}).forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment