Maybe Functor pattern matching

by dimitrs_papadimitriou

HTML

<script src="https://rawgit.com/eu81273/jsfiddle-console/master/console.js"></script>

JavaScript

class Maybe {
    map(f) {
       throw new Error('You have to implement the method map!'); 
    }
    matchWith(pattern) {
      throw new Error('You have to implement the method matchWith!');
    }

   
  }

    class Some extends Maybe {

    constructor(value) {
      super();
      this.value = value;
    }

    map(f) {
      return new Some(f(this.value))
    }

    matchWith(pattern) {
      return pattern.some(this.value)
    }

    bind(f) {
      return f(this.value)
    }
  }

    class None extends Maybe {
    map(f) {
      return new None();
    }
    matchWith(pattern) {
      return pattern.none()
    } 
    bind(f) {
      return new None()
    } 
  }


var value  = new None();

 value.matchWith({
   none:()=>console.log("none"),
   some:(v)=>console.log(v)
 })