Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package skittles.monad.factorymethod;
- import javax.lang.model.type.NullType;
- import java.util.function.Function;
- public abstract class Monad<T, E> {
- private final T value;
- public Monad(T value) {
- this.value = value;
- }
- public T extract() {
- return this.value;
- }
- public Monad<NullType, E> bind(Function<T, Monad<NullType, E>> f) {
- return f.apply(value);
- }
- public Monad<NullType, E> sequencer(Monad<NullType, E> b) {
- return bind(__ -> b);
- }
- // abstract Monad can use this factory method to create new monads, but let
- // sub-classes decide how exactly and which impl of Monad to create
- public abstract Monad<T, E> create(T value);
- }
- package skittles.monad.factorymethod;
- public class Kek<T, E> extends Monad<T, E> {
- public Kek(T value) {
- super(value);
- }
- @Override
- public Monad<T, E> create(T value) {
- return new Kek<>(value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement