DEV Community

Discussion on: Functional Programming in JS: Functor - Monad's little brother

Collapse
 
iquardt profile image
Iven Marquardt • Edited

Your implementation of the just value constructor is wrong:

   static just(value) {
        if (value === null || value === undefined) {
           throw new Error("Can't construct a value from null/undefined");
        }
        return new Maybe(value);
    }
Enter fullscreen mode Exit fullscreen mode

just must not know anything about value. It is like forbidding the array functor to deal with nested arrays:

[[1], [2,3], [4,5]].map(xs => xs.length); // [1,2,2]
Enter fullscreen mode Exit fullscreen mode

This is a perfectly valid example. With this in mind it is also valid to map over a value of type just<nothing>.

If a value is of type just<a> or nothing is not determined inside of a constructor, it is determined at the call side where the constructor is invoked.

Collapse
 
mpodlasin profile image
mpodlasin

Yup, that's a fair criticism - I will update my example.

Thanks!!!