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 |
- 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
- 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.
(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 #
An infix synonym for fmap
.
The name of this operator is an allusion to $
.
Note the similarities between their types:
($) :: (a -> b) -> a -> b (<$>) :: Functor f => (a -> b) -> f a -> f b
Whereas $
is function application, <$>
is function
application lifted over a Functor
.
Examples
Convert from a
to a Maybe
Int
using Maybe
String
show
:
>>>
show <$> Nothing
Nothing>>>
show <$> Just 3
Just "3"
Convert from an
to an Either
Int
Int
Either
Int
String
using show
:
>>>
show <$> Left 17
Left 17>>>
show <$> Right 17
Right "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
Int
String
:
>>>
Nothing $> "foo"
Nothing>>>
Just 90210 $> "foo"
Just "foo"
Replace the contents of an
with a constant
Either
Int
Int
String
, 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:
(.
)<$>
u<.>
v<.>
w = u<.>
(v<.>
w) x<.>
(f<$>
y) = (.
f)<$>
x<.>
y f<$>
(x<.>
y) = (f.
)<$>
x<.>
y
The laws imply that .>
and <.
really ignore their
left and right results, respectively, and really
return their right and left results, respectively.
Specifically,
(mf<$>
m).>
(nf<$>
n) = nf<$>
(m.>
n) (mf<$>
m)<.
(nf<$>
n) = mf<$>
(m<.
n)
(<.>) :: f (a -> b) -> f a -> f b infixl 4 Source #
(.>) :: f a -> f b -> f b infixl 4 Source #
(<.) :: f a -> f b -> f a infixl 4 Source #
liftF2 :: (a -> b -> c) -> f a -> f b -> f c Source #
Lift a binary function into a comonad with zipping
(<..>) :: Apply w => w a -> w (a -> b) -> w b infixl 4 Source #
A variant of <.>
with the arguments reversed.
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
WrapApplicative | |
|
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.
MaybeApply | |
|
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 # | |