Skip to content

Instantly share code, notes, and snippets.

@tk3369
Created October 27, 2019 00:19
Show Gist options
  • Save tk3369/0844330912b35d17eed9357cede7e133 to your computer and use it in GitHub Desktop.
Save tk3369/0844330912b35d17eed9357cede7e133 to your computer and use it in GitHub Desktop.
Functors

What are functors?

Functors are types that can be mapped over.

Two laws (https://wiki.haskell.org/Functor#Functor_Laws):

  1. mapping an identity function over a functor would return the same functor
  2. mapping function g then f is equivalent to mapping the composed function (f ∘ g)

fmap is a mapping function that operates on functors. It can be defined for specific objects so that the provided mapping function can be applied in the container. For example, fmap(f, M), where M is a monad, a wrapped value of something, is used to apply funciton f over the monad value and returns a monad result. That is, it should return M(f(M.value)).

References

Intro about functors https://medium.com/@dtinth/what-is-a-functor-dcf510b098b6

Lot's of pictures http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html#monads

Haskell tutorial http://learnyouahaskell.com/functors-applicative-functors-and-monoids

Don't be confused

In Julia, the feature of callable structs are sometimes called functors, as mentioned in the language manual section function like objects.

Likewise, in C++, a functor is just a data type that behaves like a function. See tutorial at https://www.geeksforgeeks.org/functors-in-cpp/.

While these are also called functors, they are not the same as functors as presceibed in funcitonal programming literature and Haskell language.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment