Skip to content

Instantly share code, notes, and snippets.

@stuart-marks
Created March 20, 2014 04:11
Show Gist options
  • Save stuart-marks/9657079 to your computer and use it in GitHub Desktop.
Save stuart-marks/9657079 to your computer and use it in GitHub Desktop.
Yet another FizzBuzz, using Java 8 lambda and streams.
import java.util.function.IntFunction;
import java.util.stream.IntStream;
public class FizzBuzz {
static <R> IntFunction<R> ifmod(int m, R r, IntFunction<R> f) {
return (int i) -> (i % m == 0) ? r : f.apply(i);
}
public static void main(String[] args) {
IntStream.rangeClosed(1, 100)
.mapToObj(ifmod(15, "FizzBuzz",
ifmod(5, "Buzz",
ifmod(3, "Fizz", Integer::toString))))
.forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment