roam/20220316184243-functor.org

26 lines
850 B
Org Mode
Raw Normal View History

2022-03-16 23:56:10 +00:00
:PROPERTIES:
:ID: 7a19b34d-c4bb-462b-8fae-581bf06dfdc4
:END:
#+title: Functor
In [[id:9e68d422-cced-4177-96d1-90f777b9a493][Software Development]], this refers to a parameterized data type that wraps a
value in a context and provides a method for applying a function to the wrapped
value, independent of the container type.
In [[id:9ac78677-2602-4a06-af0a-4ed82e98a9b6][Haskell]], the function for applying a function to a value in a functor is
=fmap=, also available as the infix operator =<$>=.
#+caption: =fmap= in Haskell
#+begin_src haskell :exports code
fmap :: Functor f => (a -> b) -> f a -> f b
#+end_src
* Examples
** Maybe
#+caption: Implementation of Functor for Maybe in Haskell
#+begin_src haskell :exports code
instance Functor Maybe where
fmap func (Just n) = Just (func n)
fmap func Nothing = Nothing
#+end_src