Created
November 13, 2020 16:03
-
-
Save ulrich/c8c65176a8fd3785f2f172eb27b2e1fb to your computer and use it in GitHub Desktop.
Fizzbuz Java style 1
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
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; | |
} | |
} |
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
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