9.10.1 Traversing with Writer

by dimitrs_papadimitriou

HTML

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

JavaScript

class Either {
    map(f) {
      throw new Error('You have to implement the method map!');
    }
    matchWith(pattern) {
      throw new Error('You have to implement the method matchWith!');
    }
    bind(f) {
      return this.matchWith({
        left: (e) => new Left(e),
        right: (v) => f(v)
      })
    }
 
  }

  class Right extends Either {
    constructor(value) {
      super();
      this.value = value;
    }

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

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

  }

  class Left extends Either {
    constructor(value) {
      super();
      this.value = value;
    }

    map(f) {
      return new Left(this.value);
    }
    matchWith(pattern) {
      return pattern.left(this.value)
    }

  }
      
      
      
      new Right(5)
      .map(x => x + 1)
      .bind(x => new Left(x + 1))
      .bind(x => new Left(x + 1))
      .map(x => x + 1)
      .matchWith({
        right: (v) => console.log("the result: " + v),
        left: (error) => console.log("error during execution " + error)
      })

    Promise.resolve(5)
      .then(x => x + 1)
      .then(x => Promise.reject(x + 1))
      .then(x => Promise.reject(x + 1))
      .then(x => x + 1)
      .then((v) => console.log("the result: " + v))
      .catch((error) => console.log("error during execution " + error))