Skip to content

Instantly share code, notes, and snippets.

@ulrich
Created November 13, 2020 16:03
Show Gist options
  • Save ulrich/c8c65176a8fd3785f2f172eb27b2e1fb to your computer and use it in GitHub Desktop.
Save ulrich/c8c65176a8fd3785f2f172eb27b2e1fb to your computer and use it in GitHub Desktop.
Fizzbuz Java style 1
package tech.ingenico.ah;
import java.util.function.Function;
import java.util.stream.IntStream;
public class FizzBuzz {
public static final Function<Integer, Integer> MODULO_3 = v -> v % 3;
public static final Function<Integer, Integer> MODULO_5 = v -> v % 5;
public static void run(IntStream intStream) {
intStream.parallel().forEach(value -> {
if (siDivisiblePar(MODULO_3, value) && siDivisiblePar(MODULO_5, value)) {
System.out.println("FizzBuzz: " + value);
return;
}
if (siDivisiblePar(MODULO_3, value)) {
System.out.println("Fizz: " + value);
return;
}
if (siDivisiblePar(MODULO_5, value)) {
System.out.println("Buzz: " + value);
}
});
}
private static boolean siDivisiblePar(Function<Integer, Integer> f, int valeur) {
return f.apply(valeur) == 0;
}
}
package tech.ingenico.ah;
import org.junit.jupiter.api.Test;
import java.util.stream.IntStream;
public class FizzBuzzTest {
@Test
public void shouldDisplay() {
try (IntStream intStream = IntStream.rangeClosed(0, 1_000)) {
FizzBuzz.run(intStream);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment