Skip to content

Instantly share code, notes, and snippets.

@fixxer
Created August 28, 2017 06:35
Show Gist options
  • Save fixxer/cb136af3cef9ecbcb2911926d1a1fa05 to your computer and use it in GitHub Desktop.
Save fixxer/cb136af3cef9ecbcb2911926d1a1fa05 to your computer and use it in GitHub Desktop.
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