Advertisement
dburyak

skittles monad - factory method approach

Aug 6th, 2022
1,386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. package skittles.monad.factorymethod;
  2.  
  3. import javax.lang.model.type.NullType;
  4. import java.util.function.Function;
  5.  
  6. public abstract class Monad<T, E> {
  7.     private final T value;
  8.  
  9.     public Monad(T value) {
  10.         this.value = value;
  11.     }
  12.  
  13.     public T extract() {
  14.         return this.value;
  15.     }
  16.  
  17.     public Monad<NullType, E> bind(Function<T, Monad<NullType, E>> f) {
  18.         return f.apply(value);
  19.     }
  20.  
  21.     public Monad<NullType, E> sequencer(Monad<NullType, E> b) {
  22.         return bind(__ -> b);
  23.     }
  24.  
  25.     // abstract Monad can use this factory method to create new monads, but let
  26.     // sub-classes decide how exactly and which impl of Monad to create
  27.     public abstract Monad<T, E> create(T value);
  28. }
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. package skittles.monad.factorymethod;
  41.  
  42. public class Kek<T, E> extends Monad<T, E> {
  43.     public Kek(T value) {
  44.         super(value);
  45.     }
  46.  
  47.     @Override
  48.     public Monad<T, E> create(T value) {
  49.         return new Kek<>(value);
  50.     }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement