Created
March 20, 2014 04:11
-
-
Save stuart-marks/9657079 to your computer and use it in GitHub Desktop.
Yet another FizzBuzz, using Java 8 lambda and streams.
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 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