DEV Community

Functors from first principle - explained with JS

Snir David on August 17, 2019

In this post, I will explain what functors are and why they are useful in a different way than I was taught. When I tried to learn about the functo...
Collapse
 
craigmc08 profile image
Craig McIlwrath

Why did you write your Maybe.map implementation that way? Wouldn't it make more sense to use the function keyword and the Maybe prototype so that you don't have to leave that note about this?

Something like this:

Maybe.prototype.map = function map(fun) {
  if (this.status === 'nothing') {
    return;
  }

  return fun(this.value);
}

And as an additional note, your (and my rewritten) definition of maybe doesn't satisfy the laws of a functor (maybe.map(noop) !== noop(maybe))

Collapse
 
snird profile image
Snir David

You are absolutely right (:
I tried to focus on explaining the functors concept, rather than implementation in any language.
JS was chosen as it has easy syntax readable by many.
I didn't want to go into things such as prototype etc'.

But your comment is most welcome, in case it will confuse other people, I hope they will be able to get their answer here (:

Collapse
 
ezzabuzaid profile image
ezzabuzaid

Thank you, very helpful article