fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.function.Function;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11.  
  12. interface Monad<T> {
  13. public <U> Monad<U> bind(Function<T, Monad<U>> f);
  14. }
  15.  
  16. class Maybe<T> implements Monad<T> {
  17. public Maybe(T val) {
  18. this.val = val;
  19. }
  20.  
  21. public T getVal() {
  22. return val;
  23. }
  24.  
  25. @Override
  26. public <U> Monad<U> bind(Function<T, Monad<U>> f) {
  27. if (val == null)
  28. return new Maybe<U>(null);
  29. return f.apply(val);
  30. }
  31.  
  32. private final T val;
  33. }
  34.  
  35.  
  36. public static void main (String[] args) throws java.lang.Exception
  37. {
  38. Maybe<Integer> x = new Maybe<>(5);
  39. Monad<Integer> y = x.bind(v -> new Maybe<Integer>(v+1))
  40. .bind(v -> new Maybe<Integer>(v*2));
  41. System.out.println( ((Maybe<Integer>)y).getVal() );
  42. }
  43. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:38: error: non-static variable this cannot be referenced from a static context
		Maybe<Integer> x = new Maybe<>(5);
		                   ^
Main.java:39: error: non-static variable this cannot be referenced from a static context
		Monad<Integer> y = x.bind(v -> new Maybe<Integer>(v+1))
		                               ^
Main.java:40: error: non-static variable this cannot be referenced from a static context
		                    .bind(v -> new Maybe<Integer>(v*2));
		                               ^
3 errors
stdout
Standard output is empty