foldl 1.1.2 → 1.1.3
raw patch · 2 files changed
+38/−8 lines, 2 filesdep ~profunctorsPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: profunctors
API changes (from Hackage documentation)
+ Control.Foldl: hoists :: Monad m => (forall x. m x -> n x) -> FoldM m a b -> FoldM n a b
+ Control.Foldl: impurely_ :: Monad m => (forall x. (x -> a -> m x) -> m x -> m x) -> FoldM m a b -> m b
+ Control.Foldl: mapM_ :: Monad m => (a -> m ()) -> FoldM m a ()
+ Control.Foldl: purely_ :: (forall x. (x -> a -> x) -> x -> x) -> Fold a b -> b
Files
- foldl.cabal +2/−2
- src/Control/Foldl.hs +36/−6
foldl.cabal view
@@ -1,5 +1,5 @@ Name: foldl-Version: 1.1.2+Version: 1.1.3 Cabal-Version: >=1.8.0.2 Build-Type: Simple License: BSD3@@ -29,7 +29,7 @@ transformers >= 0.2.0.0 && < 0.5 , vector >= 0.7 && < 0.12, containers < 0.6,- profunctors < 5.2,+ profunctors < 5.3, comonad == 4.* Exposed-Modules: Control.Foldl,
src/Control/Foldl.hs view
@@ -73,6 +73,7 @@ , findIndex , random , randomN+ , Control.Foldl.mapM_ , sink -- * Generic Folds@@ -90,9 +91,12 @@ -- * Utilities -- $utilities , purely+ , purely_ , impurely+ , impurely_ , generalize , simplify+ , hoists , duplicateM , _Fold1 , premap@@ -120,7 +124,7 @@ import Data.Functor.Identity (Identity, runIdentity) import Data.Monoid import Data.Profunctor-import Data.Sequence ((<|))+import Data.Sequence ((|>)) import Data.Vector.Generic (Vector, Mutable) import Data.Vector.Generic.Mutable (MVector) import System.Random.MWC (GenIO, createSystemRandom, uniformR)@@ -481,7 +485,7 @@ lastN :: Int -> Fold a [a] lastN n = Fold step begin done where- step s a = a <| s'+ step s a = s' |> a where s' = if Seq.length s < n@@ -662,6 +666,11 @@ v <- V.freeze mv return (Just v) +-- | Converts an effectful function to a fold. Specialized version of 'sink'.+mapM_ :: Monad m => (a -> m ()) -> FoldM m a ()+mapM_ = sink+{-# INLINABLE mapM_ #-}+ {-| Converts an effectful function to a fold > sink (f <> g) = sink f <> sink g -- if `(<>)` is commutative@@ -782,6 +791,11 @@ purely f (Fold step begin done) = f step begin done {-# INLINABLE purely #-} +-- | Upgrade a more traditional fold to accept the `Fold` type+purely_ :: (forall x . (x -> a -> x) -> x -> x) -> Fold a b -> b+purely_ f (Fold step begin done) = done (f step begin)+{-# INLINABLE purely_ #-}+ -- | Upgrade a monadic fold to accept the 'FoldM' type impurely :: Monad m@@ -791,6 +805,15 @@ impurely f (FoldM step begin done) = f step begin done {-# INLINABLE impurely #-} +-- | Upgrade a more traditional monadic fold to accept the `FoldM` type+impurely_+ :: Monad m+ => (forall x . (x -> a -> m x) -> m x -> m x) -> FoldM m a b -> m b+impurely_ f (FoldM step begin done) = do+ x <- f step begin+ done x+{-# INLINABLE impurely_ #-}+ {-| Generalize a `Fold` to a `FoldM` > generalize (pure r) = pure r@@ -819,13 +842,20 @@ done' x = runIdentity (done x) {-# INLINABLE simplify #-} -{-| Allows to continue feeding a 'FoldM' even after passing it to a function ++{- | Shift a 'FoldM' from one monad to another with a morphism such as 'lift' or 'liftIO';+ the effect is the same as 'Control.Monad.Morph.hoist'.+-}+hoists :: Monad m => (forall x . m x -> n x) -> FoldM m a b -> FoldM n a b+hoists phi (FoldM step begin done) = FoldM (\a b -> phi (step a b)) (phi begin) (phi . done)++{-| Allows to continue feeding a 'FoldM' even after passing it to a function that closes it. For pure 'Fold's, this is provided by the 'Control.Comonad.Comonad' instance. -} duplicateM :: Applicative m => FoldM m a b -> FoldM m a (FoldM m a b)-duplicateM (FoldM step begin done) = +duplicateM (FoldM step begin done) = FoldM step begin (\x -> pure (FoldM step (pure x) done)) {-# INLINABLE duplicateM #-} @@ -889,7 +919,7 @@ Any lens, traversal, or prism will type-check as a `Handler` -} type Handler a b =- forall x . (b -> Constant (Endo x) b) -> a -> Constant (Endo x) a + forall x . (b -> Constant (Endo x) b) -> a -> Constant (Endo x) a {-| @(handles t folder)@ transforms the input of a `Fold` using a lens, traversal, or prism:@@ -940,7 +970,7 @@ Any lens, traversal, or prism will type-check as a `HandlerM` -} type HandlerM m a b =- forall x . (b -> Constant (EndoM m x) b) -> a -> Constant (EndoM m x) a + forall x . (b -> Constant (EndoM m x) b) -> a -> Constant (EndoM m x) a {-| @(handlesM t folder)@ transforms the input of a `FoldM` using a lens, traversal, or prism: