-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Higher-order functions with their function arguments at the end -- -- Higher-order functions with their function arguments at the end @package control-block @version 0.0.0 module Control.Block -- | 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. -- --
-- >>> show <$> Nothing -- Nothing -- -- >>> show <$> Just 3 -- Just "3" ---- -- Convert from an Either Int Int to an -- 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 => (a -> b) -> f a -> f b infixl 4 <$> -- | Flipped version of <$>. -- --
-- (<&>) = flip fmap ---- --
-- >>> Just 2 <&> (+1) -- Just 3 ---- --
-- >>> [1,2,3] <&> (+1) -- [2,3,4] ---- --
-- >>> Right 3 <&> (+1) -- Right 4 --(<&>) :: Functor f => f a -> (a -> b) -> f b infixl 1 <&> -- | 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 -- Bifunctor instance that allows both the last and the -- penultimate parameters to be mapped over. -- --
-- >>> fmap show Nothing -- Nothing -- -- >>> fmap show (Just 3) -- Just "3" ---- -- Convert from an Either Int Int to an Either 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)
--
fmap :: Functor f => (a -> b) -> f a -> f b
-- | Map with access to the index.
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
-- | 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.
--
--
-- >>> 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" --foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m -- | Fold a container by mapping value to an arbitrary Monoid with -- access to the index i. -- -- When you don't need access to the index then foldMap is more -- flexible in what it accepts. -- --
-- foldMap ≡ ifoldMap . const --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 -- | Traverse elements with access to the index i, discarding the -- results (with the arguments flipped). -- --
-- ifor_ ≡ flip itraverse_ ---- -- When you don't need access to the index then for_ is more -- flexible in what it accepts. -- --
-- for_ a ≡ ifor_ a . const --ifor_ :: (FoldableWithIndex i t, Applicative f) => t a -> (i -> a -> f b) -> f () -- | Traverse elements with access to the index i, discarding the -- results. -- -- When you don't need access to the index then traverse_ is -- more flexible in what it accepts. -- --
-- traverse_ l = itraverse . const --itraverse_ :: (FoldableWithIndex i t, Applicative f) => (i -> a -> f b) -> t a -> f () -- | 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_. -- --
-- >>> 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 --traverse :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b) -- | Traverse an indexed container. -- --
-- itraverse ≡ itraverseOf itraversed --itraverse :: (TraversableWithIndex i t, Applicative f) => (i -> a -> f b) -> t a -> f (t b) -- | for is traverse with its arguments flipped. For a -- version that ignores the results see for_. for :: (Traversable t, Applicative f) => t a -> (a -> f b) -> f (t b) -- | Traverse with an index (and the arguments flipped). -- --
-- for a ≡ ifor a . const -- ifor ≡ flip itraverse --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