| Copyright | (C) 2011-2015 Edward Kmett |
|---|---|
| License | BSD-style (see the file LICENSE) |
| Maintainer | Edward Kmett <ekmett@gmail.com> |
| Stability | provisional |
| Portability | portable |
| Safe Haskell | Safe |
| Language | Haskell98 |
Data.Functor.Apply
Description
- class Functor f where
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- ($>) :: Functor f => f a -> b -> f b
- class Functor f => Apply f where
- (<..>) :: Apply w => w a -> w (a -> b) -> w b
- liftF2 :: Apply w => (a -> b -> c) -> w a -> w b -> w c
- liftF3 :: Apply w => (a -> b -> c -> d) -> w a -> w b -> w c -> w d
- newtype WrappedApplicative f a = WrapApplicative {
- unwrapApplicative :: f a
- newtype MaybeApply f a = MaybeApply {
- runMaybeApply :: Either (f a) a
Functors
class Functor f where
The Functor class is used for types that can be mapped over.
Instances of Functor should satisfy the following laws:
fmap id == id fmap (f . g) == fmap f . fmap g
The instances of Functor for lists, Maybe and IO
satisfy these laws.
Minimal complete definition
Instances
(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4
An infix synonym for fmap.
Examples
Convert from a to a Maybe Int using Maybe Stringshow:
>>>show <$> NothingNothing>>>show <$> Just 3Just "3"
Convert from an to an Either Int IntEither IntString using show:
>>>show <$> Left 17Left 17>>>show <$> Right 17Right "17"
Double each element of a list:
>>>(*2) <$> [1,2,3][2,4,6]
Apply even to the second element of a pair:
>>>even <$> (2,2)(2,True)
($>) :: Functor f => f a -> b -> f b infixl 4
Flipped version of <$.
Examples
Replace the contents of a with a constant Maybe IntString:
>>>Nothing $> "foo"Nothing>>>Just 90210 $> "foo"Just "foo"
Replace the contents of an with a constant
Either Int IntString, resulting in an :Either Int String
>>>Left 8675309 $> "foo"Left 8675309>>>Right 8675309 $> "foo"Right "foo"
Replace each element of a list with a constant String:
>>>[1,2,3] $> "foo"["foo","foo","foo"]
Replace the second element of a pair with a constant String:
>>>(1,2) $> "foo"(1,"foo")
Since: 4.7.0.0
Apply - a strong lax semimonoidal endofunctor
class Functor f => Apply f where Source
A strong lax semi-monoidal endofunctor.
This is equivalent to an Applicative without pure.
Laws:
associative composition: (.) <$> u <.> v <.> w = u <.> (v <.> w)
Minimal complete definition
Methods
(<.>) :: f (a -> b) -> f a -> f b infixl 4 Source
(.>) :: f a -> f b -> f b infixl 4 Source
a .> b = const id <$> a <.> b
(<.) :: f a -> f b -> f a infixl 4 Source
a <. b = const <$> a <.> b
Instances
(<..>) :: Apply w => w a -> w (a -> b) -> w b infixl 4 Source
A variant of <.> with the arguments reversed.
liftF2 :: Apply w => (a -> b -> c) -> w a -> w b -> w c Source
Lift a binary function into a comonad with zipping
liftF3 :: Apply w => (a -> b -> c -> d) -> w a -> w b -> w c -> w d Source
Lift a ternary function into a comonad with zipping
Wrappers
newtype WrappedApplicative f a Source
Wrap an Applicative to be used as a member of Apply
Constructors
| WrapApplicative | |
Fields
| |
Instances
| Functor f => Functor (WrappedApplicative f) Source | |
| Applicative f => Applicative (WrappedApplicative f) Source | |
| Alternative f => Alternative (WrappedApplicative f) Source | |
| Applicative f => Apply (WrappedApplicative f) Source | |
| Alternative f => Alt (WrappedApplicative f) Source | |
| Alternative f => Plus (WrappedApplicative f) Source |
newtype MaybeApply f a Source
Transform a Apply into an Applicative by adding a unit.
Constructors
| MaybeApply | |
Fields
| |
Instances
| Functor f => Functor (MaybeApply f) Source | |
| Apply f => Applicative (MaybeApply f) Source | |
| Comonad f => Comonad (MaybeApply f) Source | |
| Extend f => Extend (MaybeApply f) Source | |
| Apply f => Apply (MaybeApply f) Source |