HOME

TheInfoList



OR:

In
functional programming In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declar ...
, a functor is a
design pattern A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" b ...
inspired by the definition from category theory that allows one to apply a
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-oriente ...
to values inside a
generic type Generic programming is a style of computer programming in which algorithms are written in terms of types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneered by ...
without changing the structure of the generic type. In Haskell this idea can be captured in a
type class In computer science, a type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class T and ...
: class Functor f where fmap :: (a -> b) -> f a -> f b with conditions called ''functor laws'' (where . stands for
function composition In mathematics, function composition is an operation that takes two functions and , and produces a function such that . In this operation, the function is applied to the result of applying the function to . That is, the functions and ...
), fmap id = id fmap (g . h) = (fmap g) . (fmap h) In Scala a trait can be used: trait Functor [__ Functors_form_a_base_for_more_complex_abstractions_like_
[__ Functors_form_a_base_for_more_complex_abstractions_like_Applicative_functor">Applicative_Functor,_
[__ Functors_form_a_base_for_more_complex_abstractions_like_Applicative_functor">Applicative_Functor,_Monad_(functional_programming)">Monad_ Monad_may_refer_to: _Philosophy *_Monad_(philosophy),_a_term_meaning_"unit" **Monism,_the_concept_of_"one_essence"_in__the_metaphysical_and_theological_theory **_Monad_(Gnosticism),_the_most_primal_aspect_of_God_in_Gnosticism *_''Great_Monad'',_an_...
,_and_Monad_(functional_programming)#Comonads.html" "title="Monad_(functional_programming).html" "title="Applicative_functor.html" ;"title="[_.html" ;"title="[_">[_ Functors form a base for more complex abstractions like Applicative functor">Applicative Functor, Monad (functional programming)">Monad Monad may refer to: Philosophy * Monad (philosophy), a term meaning "unit" **Monism, the concept of "one essence" in the metaphysical and theological theory ** Monad (Gnosticism), the most primal aspect of God in Gnosticism * ''Great Monad'', an ...
, and Monad (functional programming)#Comonads">Comonad In category theory, a branch of mathematics, a monad (also triple, triad, standard construction and fundamental construction) is a monoid in the category of endofunctors. An endofunctor is a functor mapping a category to itself, and a monad is an ...
, 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).


Examples

In Haskell, lists are a simple example of a functor. We may implement 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 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 .


See also

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


References


External links


Section about Functor in Haskell Typeclassopedia

Chapter 11 Functors, Applicative Functors and Monoids in Learn You a Haskell for Great Good!



Section about Functor in lemastero/scala_typeclassopedia
{{Design Patterns Patterns Functional programming Software design patterns Programming idioms