Skip to content

Instantly share code, notes, and snippets.

@nipafx
Created July 27, 2024 11:16
Show Gist options
  • Save nipafx/25d9aeaa0908f1d70948088aead67998 to your computer and use it in GitHub Desktop.
Save nipafx/25d9aeaa0908f1d70948088aead67998 to your computer and use it in GitHub Desktop.
Some FizzBuzz
// 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