Last active 1726664443

From https://rohan.ga

kristofer's Avatar kristofer revised this gist 1726664443. Go to revision

1 file changed, 34 insertions

leonfizzbuzz.java(file created)

@@ -0,0 +1,34 @@
1 + package com.github.curriculeon;
2 +
3 + import java.util.*;
4 + import java.util.function.Predicate;
5 + import java.util.stream.Collectors;
6 + import java.util.stream.IntStream;
7 +
8 +
9 + // And https://github.com/Git-Leon personal memory optimizations of above.
10 + // https://github.com/Git-Leon/FizzBuzz/blob/master/src/main/java/com/github/curriculeon/Main.java
11 +
12 + public class Main {
13 + public static void main(String[] args) {
14 + System.out.println(
15 + fizzBuzz(
16 + IntStream.rangeClosed(1, 100).toArray(),
17 + new LinkedHashMap<Predicate<Integer>, String>() {{
18 + put(x1 -> x1 % 3 == 0, "Fizz");
19 + put(x -> x % 5 == 0, "Buzz");
20 + }}
21 + )
22 + );
23 + }
24 +
25 + private static String fizzBuzz(int[] arr, Map<Predicate<Integer>, String> preds) {
26 + return Arrays.stream(arr)
27 + .mapToObj(x -> preds.entrySet().stream()
28 + .filter(entry -> entry.getKey().test(x))
29 + .map(Map.Entry::getValue)
30 + .reduce(String::concat)
31 + .orElse(String.valueOf(x)))
32 + .collect(Collectors.joining(" "));
33 + }
34 + }

kristofer's Avatar kristofer revised this gist 1726663544. Go to revision

1 file changed, 29 insertions

fizbuzz.java(file created)

@@ -0,0 +1,29 @@
1 + import java.util.*;
2 + import java.util.function.Predicate;
3 + import java.util.stream.Collectors;
4 + import java.util.stream.IntStream;
5 +
6 + // wowey, wow, wow.
7 +
8 + public class Main {
9 + public static void main(String[] args) {
10 + int[] arr = IntStream.rangeClosed(1, 100).toArray();
11 +
12 + Map<Predicate<Integer>, String> preds = new LinkedHashMap<>();
13 + preds.put(x -> x % 3 == 0, "Fizz");
14 + preds.put(x -> x % 5 == 0, "Buzz");
15 +
16 + System.out.println(fizzBuzz(arr, preds));
17 + }
18 +
19 + private static String fizzBuzz(int[] arr, Map<Predicate<Integer>, String> preds) {
20 + return Arrays.stream(arr)
21 + .mapToObj(x -> preds.entrySet().stream()
22 + .filter(entry -> entry.getKey().test(x))
23 + .map(Map.Entry::getValue)
24 + .reduce(String::concat)
25 + .orElse(String.valueOf(x)))
26 + .collect(Collectors.joining(" "));
27 + }
28 + }
29 +
Newer Older