| Safe Haskell | Safe-Inferred |
|---|---|
| Language | GHC2021 |
Control.Block
Contents
Synopsis
- (<$>) :: Functor f => (a -> b) -> f a -> f b
- (<&>) :: Functor f => f a -> (a -> b) -> f b
- fmap :: Functor f => (a -> b) -> f a -> f b
- imap :: FunctorWithIndex i f => (i -> a -> b) -> f a -> f b
- change :: Functor f => f x -> (x -> y) -> f y
- ichange :: FunctorWithIndex i f => f x -> (i -> x -> y) -> f y
- foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m
- ifoldMap :: (FoldableWithIndex i f, Monoid m) => (i -> a -> m) -> f a -> m
- reduce :: (Foldable t, Monoid m) => t x -> (x -> m) -> m
- reduceL :: Foldable t => y -> t x -> (y -> x -> y) -> y
- reduceR :: Foldable t => y -> t x -> (x -> y -> y) -> y
- ireduce :: (FoldableWithIndex i t, Monoid m) => t x -> (i -> x -> m) -> m
- ifor_ :: (FoldableWithIndex i t, Applicative f) => t a -> (i -> a -> f b) -> f ()
- itraverse_ :: (FoldableWithIndex i t, Applicative f) => (i -> a -> f b) -> t a -> f ()
- traverse :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b)
- itraverse :: (TraversableWithIndex i t, Applicative f) => (i -> a -> f b) -> t a -> f (t b)
- for :: (Traversable t, Applicative f) => t a -> (a -> f b) -> f (t b)
- ifor :: (TraversableWithIndex i t, Applicative f) => t a -> (i -> a -> f b) -> f (t b)
- bind :: Monad f => f x -> (x -> f y) -> f y
- ibind :: (FunctorWithIndex i f, Monad f) => f x -> (i -> x -> f y) -> f y
Functor
(<$>) :: 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
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)
fmap :: Functor f => (a -> b) -> f a -> f b #
fmap is used to apply a function of type (a -> b) to a value of type f a,
where f is a functor, to produce a value of type f b.
Note that for any type constructor with more than one parameter (e.g., Either),
only the last type parameter can be modified with fmap (e.g., b in `Either a b`).
Some type constructors with two parameters or more have a instance that allows
both the last and the penultimate parameters to be mapped over.Bifunctor
Examples
Convert from a to a Maybe IntMaybe String
using show:
>>>fmap show NothingNothing>>>fmap show (Just 3)Just "3"
Convert from an to an
Either Int IntEither Int String using show:
>>>fmap show (Left 17)Left 17>>>fmap show (Right 17)Right "17"
Double each element of a list:
>>>fmap (*2) [1,2,3][2,4,6]
Apply even to the second element of a pair:
>>>fmap even (2,2)(2,True)
It may seem surprising that the function is only applied to the last element of the tuple
compared to the list example above which applies it to every element in the list.
To understand, remember that tuples are type constructors with multiple type parameters:
a tuple of 3 elements (a,b,c) can also be written (,,) a b c and its Functor instance
is defined for Functor ((,,) a b) (i.e., only the third parameter is free to be mapped over
with fmap).
It explains why fmap can be used with tuples containing values of different types as in the
following example:
>>>fmap even ("hello", 1.0, 4)("hello",1.0,True)
imap :: FunctorWithIndex i f => (i -> a -> b) -> f a -> f b #
Map with access to the index.
ichange :: FunctorWithIndex i f => f x -> (i -> x -> y) -> f y Source #
Foldable
foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m #
Map each element of the structure into a monoid, and combine the
results with (. This fold is right-associative and lazy in the
accumulator. For strict left-associative folds consider <>)foldMap'
instead.
Examples
Basic usage:
>>>foldMap Sum [1, 3, 5]Sum {getSum = 9}
>>>foldMap Product [1, 3, 5]Product {getProduct = 15}
>>>foldMap (replicate 3) [1, 2, 3][1,1,1,2,2,2,3,3,3]
When a Monoid's ( is lazy in its second argument, <>)foldMap can
return a result even from an unbounded structure. For example, lazy
accumulation enables Data.ByteString.Builder to efficiently serialise
large data structures and produce the output incrementally:
>>>import qualified Data.ByteString.Lazy as L>>>import qualified Data.ByteString.Builder as B>>>let bld :: Int -> B.Builder; bld i = B.intDec i <> B.word8 0x20>>>let lbs = B.toLazyByteString $ foldMap bld [0..]>>>L.take 64 lbs"0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24"
ifoldMap :: (FoldableWithIndex i f, Monoid m) => (i -> a -> m) -> f a -> m #
ireduce :: (FoldableWithIndex i t, Monoid m) => t x -> (i -> x -> m) -> m Source #
ifor_ :: (FoldableWithIndex i t, Applicative f) => t a -> (i -> a -> f b) -> f () #
Traverse elements with access to the index i, discarding the results (with the arguments flipped).
ifor_≡flipitraverse_
When you don't need access to the index then for_ is more flexible in what it accepts.
for_a ≡ifor_a.const
itraverse_ :: (FoldableWithIndex i t, Applicative f) => (i -> a -> f b) -> t a -> f () #
Traversable
traverse :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b) #
Map each element of a structure to an action, evaluate these actions
from left to right, and collect the results. For a version that ignores
the results see traverse_.
Examples
Basic usage:
In the first two examples we show each evaluated action mapping to the output structure.
>>>traverse Just [1,2,3,4]Just [1,2,3,4]
>>>traverse id [Right 1, Right 2, Right 3, Right 4]Right [1,2,3,4]
In the next examples, we show that Nothing and Left values short
circuit the created structure.
>>>traverse (const Nothing) [1,2,3,4]Nothing
>>>traverse (\x -> if odd x then Just x else Nothing) [1,2,3,4]Nothing
>>>traverse id [Right 1, Right 2, Right 3, Right 4, Left 0]Left 0
itraverse :: (TraversableWithIndex i t, Applicative f) => (i -> a -> f b) -> t a -> f (t b) #
Traverse an indexed container.
itraverse≡itraverseOfitraversed
for :: (Traversable t, Applicative f) => t a -> (a -> f b) -> f (t b) #
ifor :: (TraversableWithIndex i t, Applicative f) => t a -> (i -> a -> f b) -> f (t b) #
Monad
ibind :: (FunctorWithIndex i f, Monad f) => f x -> (i -> x -> f y) -> f y Source #