Functor (functional programming)

Short description: Design pattern in pure functional programming

Applying `fmap (+1)` to a binary tree of integers increments each integer in the tree by one.

In functional programming, a functor is a design pattern inspired by the definition from category theory that allows one to apply a function to values inside a generic type without changing the structure of the generic type. In Haskell this idea can be captured in a type class:

class Functor f where
fmap :: (a -> b) -> f a -> f b

This declaration says that any type of Functor must support a method `fmap`, which maps a function over the element(s) of the Functor.

Functors in Haskell should also obey functor laws,[1] which state that the mapping operation preserves the identity function and composition of functions:

fmap id = id
fmap (g . h) = (fmap g) . (fmap h)

(where `.` stands for function composition).

In Scala a trait can be used:

trait Functor[F[_]] {
def map[A,B](a: F[A])(f: A => B): F[B]

Functors form a base for more complex abstractions like Applicative Functor, Monad, and Comonad, all of which build atop a canonical functor structure. Functors are useful in modeling functional effects by values of parameterized data types. Modifiable computations are modeled by allowing a pure function to be applied to values of the "inner" type, thus creating the new overall value which represents the modified computation (which might yet to be run).

## Contents

* 1 Examples
* 2 See also
* 3 References
* 4 External links

## Examples

In Haskell, lists are a simple example of a functor. We may implement `fmap` as

fmap f []     = []
fmap f (x:xs) = (f x) : fmap f xs

A binary tree may similarly be described as a functor:

data Tree a = Leaf | Node a (Tree a) (Tree a)
instance Functor Tree where
fmap f Leaf         = Leaf
fmap f (Node x l r) = Node (f x) (fmap f l) (fmap f r)

If we have a binary tree `tr :: Tree a` and a function `f :: a -> b`, the function `fmap f tr` will apply `f` to every element of `tr`. For example, if `a` is `Int`, adding 1 to each element of `tr` can be expressed as `fmap (+ 1) tr`.[2]

## See also

* Functor in category theory
* Applicative functor, a special type of functor

## References

1. ↑ Yorgey, Brent. "Functor > Laws". https://wiki.haskell.org/Typeclassopedia#Laws.
2. ↑ "Functors". University of Maryland. http://www.cs.umd.edu/class/spring2019/cmsc388F/lectures/functors.html.

## External links

* Section about Functor in Haskell Typeclassopedia
* Chapter 11 Functors, Applicative Functors and Monoids in Learn You a Haskell for Great Good!
* Documentation for Functor in Cats library
* Section about Functor in lemastero/scala_typeclassopedia

* v
* t
* e

Software design patterns

Creational|

* Abstract factory
* Builder
* Dependency injection
* Factory method
* Lazy initialization
* Multiton
* Object pool
* Prototype
* RAII
* Singleton

Structural|

* Adapter
* Bridge
* Composite
* Decorator
* Delegation
* Facade
* Flyweight
* Front controller
* Marker interface
* Module
* Proxy
* Twin

Behavioral|

* Blackboard
* Chain of responsibility
* Command
* Interpreter
* Iterator
* Mediator
* Memento
* Null object
* Observer
* Servant
* Specification
* State
* Strategy
* Template method
* Visitor

Functional|

* Closure
* Currying
* Function composition
* Functor
* Monad
* Generator

Concurrency|

* Active object
* Actor
* Balking
* Barrier
* Binding properties
* Coroutine
* Compute kernel
* Double-checked locking
* Event-based asynchronous
* Fiber
* Futex
* Futures and promises
* Guarded suspension
* Immutable object
* Join
* Lock
* Messaging
* Monitor
* Nuclear
* Proactor
* Reactor
* Read write lock
* Scheduler
* Thread pool
* Thread-local storage

Architectural|

* ADR
* Active record
* Broker
* Client–server
* CBD
* DAO
* DTO
* DDD
* ECB
* ECS
* EDA
* Front controller
* Identity map
* Interceptor
* Implicit invocation
* Inversion of control
* Model 2
* MOM
* Microservices
* MVA
* MVC
* MVP
* MVVM
* Monolithic
* Multitier
* Naked objects
* ORB
* P2P
* Publish–subscribe
* PAC
* REST
* SOA
* Service locator
* SN
* SBA
* Specification

Cloud
Distributed|

* Ambassador
* Anti-Corruption Layer
* Bulkhead
* Cache-Aside
* Circuit Breaker
* CQRS
* Compensating Transaction
* Competing Consumers
* Compute Resource Consolidation
* Event Sourcing
* External Configuration Store
* Federated Identity
* Gatekeeper
* Index Table
* Leader Election
* MapReduce
* Materialized View
* Pipes
* Filters
* Priority Queue
* Publisher-Subscriber
* Queue-Based Load Leveling
* Retry
* Scheduler Agent Supervisor
* Sharding
* Sidecar
* Strangler
* Throttling
* Valet Key

Other|

* Business delegate
* Composite entity
* Intercepting filter
* Lazy loading
* Mangler
* Mock object
* Type tunnel
* Method chaining

Books|

* Design Patterns
* Enterprise Integration Patterns

People|

* Christopher Alexander
* Erich Gamma
* Ralph Johnson
* John Vlissides
* Grady Booch
* Kent Beck
* Ward Cunningham
* Martin Fowler
* Robert Martin
* Jim Coplien
* Douglas Schmidt
* Linda Rising

Communities|

* The Hillside Group
* The Portland Pattern Repository

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Functor (functional programming). Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Functor_(functional_programming)&oldid=275515"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template