encyclopegia.org

Functor (functional programming)

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 instance of Functor must support a method fmap , which maps a function over the elements of the instance. Functors in Haskell should also obey the so-called functor laws, 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 instead 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 functors, monads, and comonads, 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 may have yet to run).

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 instance of Functor must support a method fmap , which maps a function over the elements of the instance. Functors in Haskell should also obey the so-called functor laws, 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 instead 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 functors, monads, and comonads, 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 may have yet to run). 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 and a function , the function will apply to every element of . For example, if is , adding 1 to each element of can be expressed as . ==References==

References

No reference rows published for this article.

Cite this article

MLA

"Functor (functional programming)." encyclopegia.org, 2025, https://encyclopegia.org/wiki/functor-functional-programming. Accessed 24 November 2026.

Chicago

"Functor (functional programming)." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/functor-functional-programming.

APA

encyclopegia.org. (2025). Functor (functional programming). Retrieved November 24, 2026, from https://encyclopegia.org/wiki/functor-functional-programming

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Wilmarys Moreno

enwiki · references available

Raymond Pitman (sailor)

enwiki · references available

Journal of Nepal Medical Association

enwiki · references available

Gordon Flood

enwiki · references available

Cootamundra World War II Fuel Depot

enwiki · references available

James Fairbank

enwiki · references available

Mihai Romilă

enwiki · references available

Duggingen railway station

enwiki · references available

Grellingen railway station

enwiki · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1283290520 · 0.5 · snapshot enwiki-001283290520

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.