Created
July 27, 2024 11:16
-
-
Save nipafx/25d9aeaa0908f1d70948088aead67998 to your computer and use it in GitHub Desktop.
Some 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
// based on https://github.com/Ocean-Moist/FizzBuzz/blob/master/src/Main.java | |
// from https://rohan.ga/blog/java/ | |
// run on JDK 23 with `java --enable-preview FizzBuzz.java` | |
void main() { | |
var testers = List.of( | |
new Tester(x -> x % 3 == 0, "Fizz"), | |
new Tester(x -> x % 5 == 0, "Buzz")); | |
IntStream | |
.rangeClosed(1, 100) | |
.mapToObj(n -> testers.stream() | |
.filter(tester -> tester.predicate.test(n)) | |
.map(Tester::text) | |
.reduce(String::concat) | |
.orElse(n + "")) | |
.forEach(IO::println); | |
} | |
record Tester(IntPredicate predicate, String text) { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment