Last active
March 29, 2019 14:28
-
-
Save ykon/a68c8b8034c7bb82686570cf98deef25 to your computer and use it in GitHub Desktop.
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
/* | |
javac --release 12 --enable-preview -Xlint:unchecked TestSwitchExp.java | |
java --enable-preview TestSwitchExp | |
*/ | |
import java.util.stream.IntStream; | |
public class TestSwitchExp { | |
static class Tuple<X, Y> { | |
public final X x; | |
public final Y y; | |
public Tuple(X x, Y y) { | |
this.x = x; | |
this.y = y; | |
} | |
} | |
enum FizzBuzzT { | |
Fizz, | |
Buzz, | |
FizzBuzz, | |
Number, | |
} | |
static Tuple<FizzBuzzT, Integer> as_fizzbuzz(int n) { | |
var f = (n % 15 == 0) ? FizzBuzzT.FizzBuzz | |
: (n % 3 == 0) ? FizzBuzzT.Fizz | |
: (n % 5 == 0) ? FizzBuzzT.Buzz | |
: FizzBuzzT.Number; | |
return new Tuple<FizzBuzzT, Integer>(f, n); | |
} | |
static String fizzbuzz_to_str(Tuple<FizzBuzzT, Integer> t) { | |
return switch (t.x) { | |
case FizzBuzz -> "FizzBuzz"; | |
case Fizz -> "Fizz"; | |
case Buzz -> "Buzz"; | |
default -> t.y.toString(); | |
}; | |
} | |
public static void main(String[] args) { | |
IntStream.range(1, 101) | |
.mapToObj(TestSwitchExp::as_fizzbuzz) | |
.map(TestSwitchExp::fizzbuzz_to_str) | |
.forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment