foldl 1.4.17 → 1.4.18
raw patch · 4 files changed
+104/−15 lines, 4 filesdep ~basedep ~containersdep ~randomPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base, containers, random
API changes (from Hackage documentation)
+ Control.Foldl: instance Data.Profunctor.Closed.Closed Control.Foldl.Fold
+ Control.Foldl: lifts :: Monad m => m b -> FoldM m a b
+ Control.Foldl.NonEmpty: instance Control.Arrow.Arrow Control.Foldl.NonEmpty.Fold1
+ Control.Foldl.NonEmpty: instance Control.Arrow.ArrowChoice Control.Foldl.NonEmpty.Fold1
+ Control.Foldl.NonEmpty: instance Control.Category.Category Control.Foldl.NonEmpty.Fold1
+ Control.Foldl.NonEmpty: instance Data.Functor.Extend.Extend (Control.Foldl.NonEmpty.Fold1 a)
+ Control.Foldl.NonEmpty: instance Data.Profunctor.Choice.Choice Control.Foldl.NonEmpty.Fold1
+ Control.Foldl.NonEmpty: instance Data.Profunctor.Closed.Closed Control.Foldl.NonEmpty.Fold1
+ Control.Foldl.NonEmpty: instance Data.Profunctor.Sieve.Cosieve Control.Foldl.NonEmpty.Fold1 GHC.Base.NonEmpty
+ Control.Foldl.NonEmpty: instance Data.Profunctor.Strong.Strong Control.Foldl.NonEmpty.Fold1
+ Control.Foldl.NonEmpty: instance Data.Semigroupoid.Semigroupoid Control.Foldl.NonEmpty.Fold1
+ Control.Foldl.NonEmpty: nest :: Apply f => Fold1 a b -> Fold1 (f a) (f b)
Files
- CHANGELOG.md +11/−3
- foldl.cabal +4/−4
- src/Control/Foldl.hs +21/−4
- src/Control/Foldl/NonEmpty.hs +68/−4
CHANGELOG.md view
@@ -1,7 +1,15 @@+1.4.18++- Add [`lifts`](https://github.com/Gabriella439/foldl/pull/214)+- Add [`nest`](https://github.com/Gabriella439/foldl/pull/215) for `Fold1`+- Add [`Choice`, `Closed`, `Cosieve`, `Extend`, `Semigroupoid`, `Category`, `Strong`, `Arrow` and `ArrowChoice`](https://github.com/Gabriella439/foldl/pull/215) instances for `Fold1`+- Add [`Closed`](https://github.com/Gabriella439/foldl/pull/215) instance for `Fold`+- [Define `stimes` from `EndoM`'s Semigroup instance for 0](https://github.com/Gabriella439/foldl/pull/217)+ 1.4.17 -- Add [Fold1 utilities](): `purely`, `purely_`, `premap`, `handles`, `foldOver`, `folded1`-- Add pattern synonym `Fold1_` that makes the initial, step and extraction functions explicit.+- Add [Fold1 utilities](https://github.com/Gabriella439/foldl/pull/212): `purely`, `purely_`, `premap`, `handles`, `foldOver`, `folded1`+- Add pattern synonym [`Fold1_`](https://github.com/Gabriella439/foldl/pull/212) that makes the initial, step and extraction functions explicit. 1.4.16 @@ -15,7 +23,7 @@ - Add [`Control.Foldl.NonEmpty.nonEmpty`](https://github.com/Gabriella439/foldl/pull/186) - Add [`Control.Foldl.NonEmpty.toFold`](https://github.com/Gabriella439/foldl/pull/191)- - [Generalize `fold1` to work with `Foldable1`](https://github.com/Gabriella439/foldl/pull/185)+- [Generalize `fold1` to work with `Foldable1`](https://github.com/Gabriella439/foldl/pull/185) 1.4.13
foldl.cabal view
@@ -1,5 +1,5 @@ Name: foldl-Version: 1.4.17+Version: 1.4.18 Cabal-Version: >=1.10 Build-Type: Simple License: BSD3@@ -60,7 +60,7 @@ HS-Source-Dirs: bench Main-Is: Foldl.hs Build-Depends:- base,+ base >= 4.11.0.0 && < 5, criterion, foldl, profunctors@@ -72,7 +72,7 @@ HS-Source-Dirs: bench Main-Is: Scanl.hs Build-Depends:- base,+ base >= 4.11.0.0 && < 5, criterion, foldl GHC-Options: -O2 -Wall -rtsopts -with-rtsopts=-T@@ -83,7 +83,7 @@ HS-Source-Dirs: test Main-Is: doctest.hs Build-Depends:- base,+ base >= 4.11.0.0 && < 5, doctest >= 0.16 GHC-Options: -threaded Default-Language: Haskell2010
src/Control/Foldl.hs view
@@ -118,6 +118,7 @@ , impurely_ , generalize , simplify+ , lifts , hoists , duplicateM , _Fold1@@ -162,6 +163,7 @@ import Data.HashMap.Strict (HashMap) import Data.Map.Strict (Map) import Data.Monoid hiding ((<>))+import Data.Semigroup (Semigroup(stimes), stimesMonoid) import Data.Semigroupoid (Semigroupoid) import Data.Functor.Extend (Extend(..)) import Data.Profunctor@@ -219,7 +221,7 @@ >>> in Control.Foldl.Optics.prism Just maybeEither >>> :} ->>> both f (x, y) = (,) <$> f x <.> f y+>>> both f (x, y) = (,) <$> f x <*> f y -} @@ -245,9 +247,13 @@ rmap = fmap instance Choice Fold where- right' (Fold step begin done) = Fold (liftA2 step) (Right begin) (fmap done)+ right' = nest {-# INLINE right' #-} +instance Closed Fold where+ closed = nest+ {-# INLINE closed #-}+ instance Cosieve Fold [] where cosieve = fold {-# INLINE cosieve #-}@@ -1164,6 +1170,14 @@ hoists phi (FoldM step begin done) = FoldM (\a b -> phi (step a b)) (phi begin) (phi . done) {-# INLINABLE hoists #-} +{- | Lift a monadic value to a 'FoldM';+ works like 'Control.Monad.Trans.Class.lift'.++> lifts . pure = pure+-}+lifts :: Monad m => m b -> FoldM m a b+lifts mb = FoldM (\() _ -> pure ()) (pure ()) (\() -> mb)+ {-| Allows to continue feeding a 'FoldM' even after passing it to a function that closes it. @@ -1230,11 +1244,11 @@ {-| @(postmapM f folder)@ returns a new 'FoldM' where f is applied to the final value. -> postmapM return = id+> postmapM pure = id > > postmapM (f >=> g) = postmapM g . postmapM f -> postmapM k (pure r) = k r+> postmapM k (pure r) = lifts (k r) -} postmapM :: Monad m => (a -> m r) -> FoldM m x a -> FoldM m x r postmapM f (FoldM step begin done) = FoldM step begin done'@@ -1403,6 +1417,9 @@ instance Monad m => Semigroup (EndoM m a) where (EndoM f) <> (EndoM g) = EndoM (f <=< g) {-# INLINE (<>) #-}++ stimes = stimesMonoid+ {-# INLINE stimes #-} instance Monad m => Monoid (EndoM m a) where mempty = EndoM return
src/Control/Foldl/NonEmpty.hs view
@@ -3,6 +3,7 @@ {-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE ViewPatterns #-} {-# LANGUAGE CPP #-}+{-# LANGUAGE MultiParamTypeClasses #-} {-| This module provides a `Fold1` type that is a \"non-empty\" analog of the `Fold` type, meaning that it requires at least one input element in order to@@ -57,16 +58,24 @@ , handles , foldOver , folded1+ , nest ) where import Control.Applicative (liftA2, Const(..))+import Control.Arrow (Arrow (..), ArrowChoice (..))+import Control.Category (Category ())+import qualified Control.Category+import Control.Comonad (Comonad(..)) import Control.Foldl (Fold(..)) import Control.Foldl.Internal (Either'(..)) import Data.List.NonEmpty (NonEmpty(..)) import Data.Monoid (Dual(..))-import Data.Functor.Apply (Apply)-import Data.Profunctor (Profunctor(..))+import Data.Functor.Apply (Apply (..))+import Data.Functor.Extend (Extend (..))+import Data.Profunctor+import Data.Profunctor.Sieve (Cosieve(..)) import Data.Semigroup.Foldable (Foldable1(..), traverse1_)+import Data.Semigroupoid (Semigroupoid (..)) import Data.Functor.Contravariant (Contravariant(..)) import Prelude hiding (head, last, minimum, maximum)@@ -83,7 +92,7 @@ >>> _2 f (x, y) = fmap (\i -> (x, i)) (f y) ->>> both f (x, y) = (,) <$> f x <.> f y+>>> both1 f (x, y) = (,) <$> f x <.> f y -} @@ -137,6 +146,18 @@ rmap = fmap {-# INLINE rmap #-} +instance Choice Fold1 where+ right' = nest+ {-# INLINE right' #-}++instance Closed Fold1 where+ closed = nest+ {-# INLINE closed #-}++instance Cosieve Fold1 NonEmpty where+ cosieve = Control.Foldl.NonEmpty.fold1+ {-# INLINE cosieve #-}+ instance Applicative (Fold1 a) where pure b = Fold1 (pure (pure b)) {-# INLINE pure #-}@@ -144,6 +165,10 @@ Fold1 l <*> Fold1 r = Fold1 (liftA2 (<*>) l r) {-# INLINE (<*>) #-} +instance Extend (Fold1 a) where+ duplicated (Fold1 f) = Fold1 $ fmap fromFold . duplicated . f+ {-# INLINE duplicated #-}+ instance Semigroup b => Semigroup (Fold1 a b) where (<>) = liftA2 (<>) {-# INLINE (<>) #-}@@ -155,6 +180,36 @@ mappend = (<>) {-# INLINE mappend #-} +instance Semigroupoid Fold1 where+ o (Fold1 l1) (Fold1 r1) = Fold1 f1+ where+ f1 a = let r = r1 a+ l = l1 $ extract r+ in o l r+ {-# INLINE o #-}++instance Category Fold1 where+ (.) = o+ {-# INLINE (.) #-}++ id = last+ {-# INLINE id #-}++instance Strong Fold1 where+ first' f = (,) <$> lmap fst f <*> lmap snd last+ {-# INLINE first' #-}++instance Arrow Fold1 where+ arr f = f <$> last+ {-# INLINE arr #-}++ first = first'+ {-# INLINE first #-}++instance ArrowChoice Fold1 where+ left = left'+ {-# INLINE left #-}+ instance Num b => Num (Fold1 a b) where fromInteger = pure . fromInteger {-# INLINE fromInteger #-}@@ -404,7 +459,7 @@ {- | @(foldOver f folder xs)@ folds all values from a Lens, Traversal1 or Fold1 optic with the given folder ->>> foldOver (_2 . both) Foldl1.nonEmpty (1, (2, 3))+>>> foldOver (_2 . both1) Foldl1.nonEmpty (1, (2, 3)) 2 :| [3] > Foldl1.foldOver f folder xs == Foldl1.fold1 folder (xs ^.. f)@@ -431,3 +486,12 @@ folded1 k ts = contramap (\_ -> ()) (traverse1_ k ts) {-# INLINABLE folded1 #-} +{-| Nest a fold in an Apply.+-}+nest :: Apply f => Fold1 a b -> Fold1 (f a) (f b)+nest (Fold1_ i s e) =+ Fold1_+ (fmap i)+ (liftF2 s)+ (fmap e)+{-# INLINABLE nest #-}