Created
August 28, 2017 06:35
-
-
Save fixxer/cb136af3cef9ecbcb2911926d1a1fa05 to your computer and use it in GitHub Desktop.
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
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
class FizzBuzz { | |
private static class P { | |
final int divisor; | |
final String val; | |
P(int divisor, String val) { | |
this.divisor = divisor; | |
this.val = val; | |
} | |
} | |
private static String fizzBuzz(int n) { | |
return Stream.of(new P(3, "Fizz"), new P(5, "Buzz")) | |
.filter(p -> 0 == n % p.divisor) | |
.map(p -> p.val) | |
.reduce((s1, s2) -> s1 + s2) | |
.orElse(String.valueOf(n)); | |
} | |
public static void main(String[] argc) { | |
IntStream.range(1, 100) | |
.mapToObj(FizzBuzz::fizzBuzz) | |
.forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment