K
K
Kaizaki2017-11-28 17:14:28
JavaScript
Kaizaki, 2017-11-28 17:14:28

Why use functor?

Thank you for your attention! Tell me, what is the advantage of a functor, because it will simply apply the function on the value stored in the object? We could do this before without an object (new to FP). Don't scold me if this is a dumb question :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Kolesnichenko, 2017-11-29
@KolesnichenkoDS

A functor, in the context of functional programming, is an interface (or typeclass) that can be implemented for different types. In order for a type to implement a functor, it must have a kind * -> *(that is, it must be a generic with one parameter) and a function must be implemented for it map: The name and signature may differ depending on the implementation, for example, in Haskell this function is called , and in Fantasy Land the signature looks like this: Also , certain laws must be observed for the function / method , I will not copy them here, I will just leave the link: https://github.com/fantasyland/fantasy-land#functor A few examples of functors:

  • Array- mapapplies a function to each element of the array.
  • Future- mapapplies the function to the value when it resolves.
  • Maybe- mapapplies the function to the value if it exists, otherwise returns Nothing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question