Svoboda | Graniru | BBC Russia | Golosameriki | Facebook

To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
Languages
Recent
Show all languages
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Functor (functional programming)

From Wikipedia, the free encyclopedia

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).

YouTube Encyclopedic

  • 1/3
    Views:
    24 839
    11 543
    15 811
  • C++ Functors
  • AFP 6 - Applicative Functors
  • Functors Applicatives and Monads in Haskell - Part 1 (Functors)

Transcription

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

References

  1. ^ Yorgey, Brent. "Functor > Laws". HaskellWiki. Retrieved 17 June 2023.
  2. ^ "Functors". Functional Pearls. University of Maryland. Retrieved 12 December 2022.

External links

This page was last edited on 12 September 2023, at 13:26
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.