What is a functor?

A functor is any data type that can be mapped over using map(), as long it abides by two laws:

If your map function is the identity function (i.e. it just returns each element without transformation) then your input and output must be the same.

If you compose function A and function B to make function C then use the result with map(), the result should be the same as mapping first A then B individually.

Both of those laws sound obvious, but that doesn’t mean they aren’t important. Arrays are functors, sets are functors, optionals are functors, and more.

Sponsored You’re already busy updating your app for Swift 4.2 and iOS 12, so why not let Instabug help you find and fix bugs? Add just two lines of code to your project and receive comprehensive reports with all the feedback you need to ship a world-class app – click here to learn more!