/** * Sets a {@link Supplier} to execute if this matches. */ public <R> FluentMatchingR<T, R> get(Supplier<R> supplier) { return get(new FluentMatchingR<>(value), supplier); }
private long factMatching(long i) { return match(i) .when(eq(0L)).get(() -> 1L) .when(any()).get(x -> x * factMatching(x - 1)) .getMatch(); } }
/** * Fizzbuzz benchmark using motif pattern matching. */ @Benchmark public void fizzBuzzPatternMatching() { IntStream.range(0, 101).forEach( n -> System.out.println( match(Tuple2.of(n % 3, n % 5)) .when(tuple2(eq(0), eq(0))).get(() -> "FizzBuzz") .when(tuple2(eq(0), any())).get(y -> "Fizz") .when(tuple2(any(), eq(0))).get(x -> "Buzz") .orElse(String.valueOf(n)) .getMatch() ) ); } }
/** * An implementation of factorial using motif pattern matching. */ public long factMatching(long n) { return match(n) .when(caseLong(0)).get(() -> 1L) .when(caseLong(any())).get(i -> i * factMatching(i - 1)) .getMatch(); } }