Last active
April 11, 2020 20:51
-
-
Save cchacin/9926141 to your computer and use it in GitHub Desktop.
Java 8 FizzBuzz
This file contains hidden or 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
public class FizzBuzz { | |
static java.util.function.Predicate<Integer> divBy(int n) { return i -> (i % n) == 0;} | |
static String fizzBuzz(Integer intIn){ | |
String fizz = divBy(3).test(intIn) ? "Fizz" : ""; | |
String buzz = divBy(5).test(intIn) ? "Buzz" : ""; | |
return (fizz.isEmpty() && buzz.isEmpty()) ? intIn.toString() : fizz + buzz; | |
} | |
public static void main(String[] args) { | |
int start = Integer.parseInt(args[0]); | |
int end = Integer.parseInt(args[1]); | |
java.util.stream.IntStream | |
.rangeClosed(start, end) | |
.mapToObj(Java8Carlos::fizzBuzz) | |
.forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment