Skip to content

Instantly share code, notes, and snippets.

@theqp

theqp/Main.java Secret

Created September 15, 2021 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theqp/3a24d5cc33ddaf224944124501183bd6 to your computer and use it in GitHub Desktop.
Save theqp/3a24d5cc33ddaf224944124501183bd6 to your computer and use it in GitHub Desktop.
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