foldl 1.1.6 → 1.2.0
raw patch · 2 files changed
+55/−14 lines, 2 filesdep +contravariantPVP ok
version bump matches the API change (PVP)
Dependencies added: contravariant
API changes (from Hackage documentation)
+ Control.Foldl: folded :: (Contravariant f, Applicative f, Foldable t) => (a -> f a) -> (t a -> f (t a))
- Control.Foldl: type Handler a b = forall x. (b -> Constant (Endo x) b) -> a -> Constant (Endo x) a
+ Control.Foldl: type Handler a b = forall x. (b -> Const (Dual (Endo x)) b) -> a -> Const (Dual (Endo x)) a
- Control.Foldl: type HandlerM m a b = forall x. (b -> Constant (EndoM m x) b) -> a -> Constant (EndoM m x) a
+ Control.Foldl: type HandlerM m a b = forall x. (b -> Const (Dual (EndoM m x)) b) -> a -> Const (Dual (EndoM m x)) a
Files
- foldl.cabal +2/−1
- src/Control/Foldl.hs +53/−13
foldl.cabal view
@@ -1,5 +1,5 @@ Name: foldl-Version: 1.1.6+Version: 1.2.0 Cabal-Version: >=1.8.0.2 Build-Type: Simple License: BSD3@@ -29,6 +29,7 @@ transformers >= 0.2.0.0 && < 0.5 , vector >= 0.7 && < 0.12, containers < 0.6 ,+ contravariant < 1.5 , profunctors < 5.3 , comonad >= 4.0 && < 6 Exposed-Modules:
src/Control/Foldl.hs view
@@ -108,6 +108,7 @@ , EndoM(..) , HandlerM , handlesM+ , folded -- * Re-exports -- $reexports@@ -118,12 +119,12 @@ import Control.Applicative import Control.Foldl.Internal (Maybe'(..), lazy, Either'(..), hush)-import Control.Monad ((>=>))+import Control.Monad ((<=<)) import Control.Monad.Primitive (PrimMonad, RealWorld) import Control.Comonad import Data.Foldable (Foldable)-import Data.Functor.Constant (Constant(Constant, getConstant)) import Data.Functor.Identity (Identity, runIdentity)+import Data.Functor.Contravariant (Contravariant(..)) import Data.Monoid import Data.Profunctor import Data.Sequence ((|>))@@ -797,17 +798,46 @@ function to accept a 'Fold' or 'FoldM' using the 'purely' or 'impurely' combinators. - For example, the @pipes@ library implements a @foldM@ function in+ For example, the @pipes@ library implements @fold@ and @foldM@ functions in @Pipes.Prelude@ with the following type: -> foldM+> Pipes.Prelude.fold > :: Monad m+> -> (x -> a -> x) -> x -> (x -> b) -> Producer a m () -> m b+>+> Pipes.Prelude.foldM+> :: Monad m > => (x -> a -> m x) -> m x -> (x -> m b) -> Producer a m () -> m b - @foldM@ is set up so that you can wrap it with 'impurely' to accept a- 'FoldM' instead:+ Both @fold@ and @foldM@ is set up so that you can wrap them with either+ 'purely' or 'impurely' to accept a 'Fold' or 'FoldM', respectively: -> impurely foldM :: Monad m => FoldM m a b -> Producer a m () -> m b+> purely Pipes.Prelude.fold+> :: Monad m => Fold a b -> Producer a m () -> m b+>+> impurely Pipes.Prelude.foldM+> :: Monad m => FoldM m a b -> Producer a m () -> m b++ Similarly the @ofoldlUnwrap@ and @ofoldMUnwrap@ functions from the+ @monotraversable@ package are written to interoperate with this library:++> ofoldlUnwrap+> :: MonoFoldable mono+> => (x -> Element mono -> x) -> x -> (x -> b) -> mono -> b +>+> ofoldMUnwrap+> :: (Monad m, MonoFoldable mono)+> => (x -> Element mono -> m x) -> m x -> (x -> m b) -> mono -> m b ++ You can wrap these to accept 'Fold' or 'FoldM', too:++> purely ofoldlUnwrap+> :: MonoFoldable mono+> => Fold (Element mono) b -> mono -> b+>+> impurely ofoldMUnwrap+> :: MonoFoldable mono+> => FoldM m (Element mono) b -> mono -> m b -} -- | Upgrade a fold to accept the 'Fold' type@@ -943,7 +973,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 -> Const (Dual (Endo x)) b) -> a -> Const (Dual (Endo x)) a {-| @(handles t folder)@ transforms the input of a `Fold` using a lens, traversal, or prism:@@ -976,26 +1006,26 @@ handles :: Handler a b -> Fold b r -> Fold a r handles k (Fold step begin done) = Fold step' begin done where- step' = flip (appEndo . getConstant . k (Constant . Endo . flip step))+ step' = flip (appEndo . getDual . getConst . k (Const . Dual . Endo . flip step)) {-# INLINABLE handles #-} {-| > instance Monad m => Monoid (EndoM m a) where > mempty = EndoM return-> mappend (EndoM f) (EndoM g) = EndoM (f >=> g)+> mappend (EndoM f) (EndoM g) = EndoM (f <=< g) -} newtype EndoM m a = EndoM { appEndoM :: a -> m a } instance Monad m => Monoid (EndoM m a) where mempty = EndoM return- mappend (EndoM f) (EndoM g) = EndoM (f >=> g)+ mappend (EndoM f) (EndoM g) = EndoM (f <=< g) {-| A Handler for the upstream input of `FoldM` 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 -> Const (Dual (EndoM m x)) b) -> a -> Const (Dual (EndoM m x)) a {-| @(handlesM t folder)@ transforms the input of a `FoldM` using a lens, traversal, or prism:@@ -1018,8 +1048,18 @@ handlesM :: Monad m => HandlerM m a b -> FoldM m b r -> FoldM m a r handlesM k (FoldM step begin done) = FoldM step' begin done where- step' = flip (appEndoM . getConstant . k (Constant . EndoM . flip step))+ step' = flip (appEndoM . getDual . getConst . k (Const . Dual . EndoM . flip step)) {-# INLINABLE handlesM #-}++{-|+> folded :: Foldable t => Fold (t a) a+>+> handles folded :: Foldable t => Fold a r -> Fold (t a) r+-}+folded+ :: (Contravariant f, Applicative f, Foldable t)+ => (a -> f a) -> (t a -> f (t a))+folded k ts = contramap (\_ -> ()) (F.traverse_ k ts) {- $reexports @Control.Monad.Primitive@ re-exports the 'PrimMonad' type class