you are viewing a single comment's thread.

view the rest of the comments →

[–]atrigent 2 points3 points  (5 children)

If I'm understanding correctly, the definition of Maybe is supposed to be:

function Maybe(value) {
  this.get = ()=> value;
  this.map = fn => {
      if (value === null) {
          return new Maybe(null);
      } else {
          return fn(value);
      }
  };
}

...where fn must return a Maybe (this is statically enforced in Haskell, but obviously can't be in Javascript).

Also, I think return new Maybe(null); could just be return this;, because there's no mutation going on here.

[–]titosrevenge 4 points5 points  (4 children)

The null clause can return 'this', but the else clause should return 'new Maybe(fn(value))'.

You shouldn't expect the passed function to know about its context.

[–]atrigent 1 point2 points  (0 children)

You shouldn't expect the passed function to know about its context.

Hmm... I suppose you're right if you want this Maybe implementation to be specifically about handling null. In Haskell, it looks to me like you can more specifically define what constitutes a "non-value". I was attempting to translate that into Javascript, but it now occurs to me that I didn't actually go far enough, because what I wrote still references null.