-
-
Save theqp/3a24d5cc33ddaf224944124501183bd6 to your computer and use it in GitHub Desktop.
This file contains 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
import java.util.function.Function; | |
class Program { | |
public sealed interface Option<T> permits Some, None { } | |
public record Some<T>(T value) implements Option<T> { } | |
public record None<T>() implements Option<T> { } | |
public static <T, U> Option<U> map(Option<T> o, Function<T, U> f) { | |
return o instanceof Some<T> s | |
? new Some<>(f.apply(s.value())) | |
: new None<>(); | |
} | |
public static void main(String[] args) { | |
// prints Some[value=hi!] | |
System.out.println(map(new Some<>("hi"), s -> s + "!")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment