36 lines
1.4 KiB
Org Mode
36 lines
1.4 KiB
Org Mode
|
:PROPERTIES:
|
||
|
:ID: 10eb4672-19ab-4275-a110-5446c96e7e24
|
||
|
:END:
|
||
|
#+title: Applicative
|
||
|
|
||
|
In [[id:9e68d422-cced-4177-96d1-90f777b9a493][Software Development]], this refers to a [[id:7a19b34d-c4bb-462b-8fae-581bf06dfdc4][Functor]] that provides a method for
|
||
|
applying a function wrapped in such a type to a value wrapped in the same type.
|
||
|
|
||
|
In [[id:9ac78677-2602-4a06-af0a-4ed82e98a9b6][Haskell]], the function for applying a wrapped function to a value wrapped in
|
||
|
the same type is available as the infix operator =<*>=, and facilitates the
|
||
|
application of a function taking an arbitrary number of arguments over multiple
|
||
|
wrapped values. An applicative must also implement the =pure= function, which
|
||
|
takes a single argument and returns it wrapped.
|
||
|
|
||
|
#+caption: Applicative functions in Haskell
|
||
|
#+begin_src haskell :exports code
|
||
|
pure :: Applicative f => a -> f a
|
||
|
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
|
||
|
#+end_src
|
||
|
|
||
|
In the following example, =fmap= (=<$>=) first applies the =add= function to the
|
||
|
value =Just 1=, resulting in a function of the type =Just (Int -> Int)=. The
|
||
|
applicative infix operator (=<*>=) then applies that function to the remaining
|
||
|
value =Just 2=, resulting in =Just 3=.
|
||
|
|
||
|
#+caption: Applying a function over multiple wrapped values
|
||
|
#+begin_src haskell :cache yes :exports both
|
||
|
add :: Int -> Int -> Int
|
||
|
add x y = x + y
|
||
|
|
||
|
add <$> Just 1 <*> Just 2
|
||
|
#+end_src
|
||
|
|
||
|
#+RESULTS[10751e7c3100f27569974ee18b3f34498166dfc5]:
|
||
|
: Prelude> Just 3
|