indexed-traversable 0.1.1 → 0.1.2
raw patch · 5 files changed
+59/−29 lines, 5 filesdep ~basedep ~semigroupsPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base, semigroups
API changes (from Hackage documentation)
Files
- Changelog.md +8/−0
- indexed-traversable.cabal +7/−5
- src/Data/Foldable/WithIndex.hs +6/−6
- src/Data/Traversable/WithIndex.hs +5/−5
- src/WithIndex.hs +33/−13
Changelog.md view
@@ -1,3 +1,11 @@+# 0.1.2 [2021-10-30]++- Changed `(<$>)` + `(<*>)` to `liftA2` to potentially avoid extra `fmap`.+- Add `(#..)`, coercive composition after a 2-parameter function.+- Switched references to lens 'Of'- functions to base functions.++Thanks to wygulmage for contributions.+ # 0.1.1 [2020-12-27] - Mark all modules as `Safe`.
indexed-traversable.cabal view
@@ -1,6 +1,6 @@ cabal-version: 1.12 name: indexed-traversable-version: 0.1.1+version: 0.1.2 build-type: Simple license: BSD2 license-file: LICENSE@@ -45,7 +45,9 @@ || ==8.4.4 || ==8.6.5 || ==8.8.4- || ==8.10.2+ || ==8.10.7+ || ==9.0.1+ || ==9.2.1 source-repository head type: git@@ -67,9 +69,9 @@ build-depends: array >=0.3.0.2 && <0.6- , base >=4.3 && <4.16+ , base >=4.3 && <4.17 , containers >=0.4.0.0 && <0.7- , transformers >=0.3.0.0 && <0.6+ , transformers >=0.3.0.0 && <0.7 if !impl(ghc >=7.8) build-depends: tagged >=0.8.5 && <0.9@@ -81,7 +83,7 @@ build-depends: base-orphans >=0.8.3 && <0.9 , semigroups >=0.18.4 && <0.20- , transformers-compat >=0.6.6 && <0.7+ , transformers-compat >=0.6.6 && <0.8 if (impl(ghc >=7.0) && impl(ghc <7.6)) build-depends: ghc-prim
src/Data/Foldable/WithIndex.hs view
@@ -42,7 +42,7 @@ -- 'any' ≡ 'iany' '.' 'const' -- @ iany :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Bool-iany f = getAny #. ifoldMap (\i -> Any #. f i)+iany f = getAny #. ifoldMap (Any #.. f) {-# INLINE iany #-} -- | Return whether or not all elements in a container satisfy a predicate, with access to the index @i@.@@ -53,7 +53,7 @@ -- 'all' ≡ 'iall' '.' 'const' -- @ iall :: FoldableWithIndex i f => (i -> a -> Bool) -> f a -> Bool-iall f = getAll #. ifoldMap (\i -> All #. f i)+iall f = getAll #. ifoldMap (All #.. f) {-# INLINE iall #-} -- | Return whether or not none of the elements in a container satisfy a predicate, with access to the index @i@.@@ -85,7 +85,7 @@ -- 'traverse_' l = 'itraverse' '.' 'const' -- @ itraverse_ :: (FoldableWithIndex i t, Applicative f) => (i -> a -> f b) -> t a -> f ()-itraverse_ f = void . getTraversed #. ifoldMap (\i -> Traversed #. f i)+itraverse_ f = void . getTraversed #. ifoldMap (Traversed #.. f) {-# INLINE itraverse_ #-} -- | Traverse elements with access to the index @i@, discarding the results (with the arguments flipped).@@ -112,7 +112,7 @@ -- 'mapM_' ≡ 'imapM' '.' 'const' -- @ imapM_ :: (FoldableWithIndex i t, Monad m) => (i -> a -> m b) -> t a -> m ()-imapM_ f = liftM skip . getSequenced #. ifoldMap (\i -> Sequenced #. f i)+imapM_ f = liftM skip . getSequenced #. ifoldMap (Sequenced #.. f) {-# INLINE imapM_ #-} -- | Run monadic actions for each target of an 'IndexedFold' or 'Control.Lens.IndexedTraversal.IndexedTraversal' with access to the index,@@ -122,10 +122,10 @@ -- 'iforM_' ≡ 'flip' 'imapM_' -- @ ----- When you don't need access to the index then 'Control.Lens.Fold.forMOf_' is more flexible in what it accepts.+-- When you don't need access to the index then 'Control.Monad.forM_' is more flexible in what it accepts. -- -- @--- 'Control.Lens.Fold.forMOf_' l a ≡ 'iforMOf' l a '.' 'const'+-- 'Control.Monad.forM_' a ≡ 'iforM' a '.' 'const' -- @ iforM_ :: (FoldableWithIndex i t, Monad m) => t a -> (i -> a -> m b) -> m () iforM_ = flip imapM_
src/Data/Traversable/WithIndex.hs view
@@ -49,7 +49,7 @@ -- 'mapM' ≡ 'imapM' '.' 'const' -- @ imapM :: (TraversableWithIndex i t, Monad m) => (i -> a -> m b) -> t a -> m (t b)-imapM f = unwrapMonad #. itraverse (\i -> WrapMonad #. f i)+imapM f = unwrapMonad #. itraverse (WrapMonad #.. f) {-# INLINE imapM #-} -- | Map each element of a structure to a monadic action,@@ -66,10 +66,10 @@ -- | Generalizes 'Data.Traversable.mapAccumR' to add access to the index. ----- 'imapAccumROf' accumulates state from right to left.+-- 'imapAccumR' accumulates state from right to left. -- -- @--- 'Control.Lens.Traversal.mapAccumR' ≡ 'imapAccumR' '.' 'const'+-- 'Data.Traversable.mapAccumR' ≡ 'imapAccumR' '.' 'const' -- @ imapAccumR :: TraversableWithIndex i t => (i -> s -> a -> (s, b)) -> s -> t a -> (s, t b) imapAccumR f s0 a = swap (Lazy.runState (forwards (itraverse (\i c -> Backwards (Lazy.state (\s -> swap (f i s c)))) a)) s0)@@ -77,10 +77,10 @@ -- | Generalizes 'Data.Traversable.mapAccumL' to add access to the index. ----- 'imapAccumLOf' accumulates state from left to right.+-- 'imapAccumL' accumulates state from left to right. -- -- @--- 'Control.Lens.Traversal.mapAccumLOf' ≡ 'imapAccumL' '.' 'const'+-- 'Data.Traversable.mapAccumL' ≡ 'imapAccumL' '.' 'const' -- @ imapAccumL :: TraversableWithIndex i t => (i -> s -> a -> (s, b)) -> s -> t a -> (s, t b) imapAccumL f s0 a = swap (Lazy.runState (itraverse (\i c -> Lazy.state (\s -> swap (f i s c))) a) s0)
src/WithIndex.hs view
@@ -22,7 +22,7 @@ flip, id, seq, snd, ($!), ($), (.), zip) import Control.Applicative- (Applicative (..), Const (..), ZipList (..), (<$>))+ (Applicative (..), Const (..), ZipList (..), (<$>), liftA2) import Control.Applicative.Backwards (Backwards (..)) import Control.Monad.Trans.Identity (IdentityT (..)) import Control.Monad.Trans.Reader (ReaderT (..))@@ -94,7 +94,8 @@ #endif imapDefault :: TraversableWithIndex i f => (i -> a -> b) -> f a -> f b-imapDefault f = runIdentity #. itraverse (\i a -> Identity (f i a))+-- imapDefault f = runIdentity #. itraverse (\i a -> Identity (f i a))+imapDefault f = runIdentity #. itraverse (Identity #.. f) {-# INLINE imapDefault #-} -------------------------------------------------------------------------------@@ -120,6 +121,12 @@ #endif -- | A variant of 'ifoldMap' that is strict in the accumulator.+ --+ -- When you don't need access to the index then 'Data.Foldable.foldMap'' is more flexible in what it accepts.+ --+ -- @+ -- 'foldMap'' ≡ 'ifoldMap'' '.' 'const'+ -- @ ifoldMap' :: Monoid m => (i -> a -> m) -> f a -> m ifoldMap' f = ifoldl' (\i acc a -> mappend acc (f i a)) mempty {-# INLINE ifoldMap' #-}@@ -132,7 +139,7 @@ -- 'Data.Foldable.foldr' ≡ 'ifoldr' '.' 'const' -- @ ifoldr :: (i -> a -> b -> b) -> b -> f a -> b- ifoldr f z t = appEndo (ifoldMap (\i -> Endo #. f i) t) z+ ifoldr f z t = appEndo (ifoldMap (Endo #.. f) t) z {-# INLINE ifoldr #-} -- | Left-associative fold of an indexed container with access to the index @i@.@@ -143,7 +150,7 @@ -- 'Data.Foldable.foldl' ≡ 'ifoldl' '.' 'const' -- @ ifoldl :: (i -> b -> a -> b) -> b -> f a -> b- ifoldl f z t = appEndo (getDual (ifoldMap (\i -> Dual #. Endo #. flip (f i)) t)) z+ ifoldl f z t = appEndo (getDual (ifoldMap (\ i -> Dual #. Endo #. flip (f i)) t)) z {-# INLINE ifoldl #-} -- | /Strictly/ fold right over the elements of a structure with access to the index @i@.@@ -163,7 +170,7 @@ -- When you don't need access to the index then 'Control.Lens.Fold.foldlOf'' is more flexible in what it accepts. -- -- @- -- 'Control.Lens.Fold.foldlOf'' l ≡ 'ifoldlOf'' l '.' 'const'+ -- 'Data.Foldable.foldl'' l ≡ 'ifoldl'' l '.' 'const' -- @ ifoldl' :: (i -> b -> a -> b) -> b -> f a -> b ifoldl' f z0 xs = ifoldr f' id xs z0@@ -171,7 +178,7 @@ {-# INLINE ifoldl' #-} ifoldMapDefault :: (TraversableWithIndex i f, Monoid m) => (i -> a -> m) -> f a -> m-ifoldMapDefault f = getConst #. itraverse (\i a -> Const (f i a))+ifoldMapDefault f = getConst #. itraverse (Const #.. f) {-# INLINE ifoldMapDefault #-} -------------------------------------------------------------------------------@@ -285,7 +292,7 @@ {-# INLINE ifoldMap #-} instance TraversableWithIndex Int NonEmpty where itraverse f ~(a :| as) =- (:|) <$> f 0 a <*> traverse (uncurry' f) (zip [1..] as)+ liftA2 (:|) (f 0 a) (traverse (uncurry' f) (zip [1..] as)) {-# INLINE itraverse #-} -------------------------------------------------------------------------------@@ -364,7 +371,7 @@ {-# INLINE ifoldMap #-} instance (TraversableWithIndex i f, TraversableWithIndex j g) => TraversableWithIndex (Either i j) (Product f g) where- itraverse f (Pair a b) = Pair <$> itraverse (f . Left) a <*> itraverse (f . Right) b+ itraverse f (Pair a b) = liftA2 Pair (itraverse (f . Left) a) (itraverse (f . Right) b) {-# INLINE itraverse #-} -------------------------------------------------------------------------------@@ -404,11 +411,11 @@ {-# INLINE imap #-} instance FoldableWithIndex i f => FoldableWithIndex i (Reverse f) where- ifoldMap f = getDual . ifoldMap (\i -> Dual #. f i) . getReverse+ ifoldMap f = getDual #. ifoldMap (Dual #.. f) . getReverse {-# INLINE ifoldMap #-} instance TraversableWithIndex i f => TraversableWithIndex i (Reverse f) where- itraverse f = fmap Reverse . forwards . itraverse (\i -> Backwards . f i) . getReverse+ itraverse f = fmap Reverse . forwards . itraverse (Backwards #.. f) . getReverse {-# INLINE itraverse #-} -------------------------------------------------------------------------------@@ -440,7 +447,7 @@ {-# INLINE ifoldMap #-} instance TraversableWithIndex [Int] Tree where- itraverse f (Node a as) = Node <$> f [] a <*> itraverse (\i -> itraverse (f . (:) i)) as+ itraverse f (Node a as) = liftA2 Node (f [] a) (itraverse (\i -> itraverse (f . (:) i)) as) {-# INLINE itraverse #-} -- -- | The position in the 'Seq' is available as the index.@@ -579,7 +586,7 @@ {-# INLINE ifoldMap #-} instance (TraversableWithIndex i f, TraversableWithIndex j g) => TraversableWithIndex (Either i j) (f :*: g) where- itraverse q (fa :*: ga) = (:*:) <$> itraverse (q . Left) fa <*> itraverse (q . Right) ga+ itraverse q (fa :*: ga) = liftA2 (:*:) (itraverse (q . Left) fa) (itraverse (q . Right) ga) {-# INLINE itraverse #-} instance (FunctorWithIndex i f, FunctorWithIndex j g) => FunctorWithIndex (Either i j) (f :+: g) where@@ -628,12 +635,19 @@ #if __GLASGOW_HASKELL__ >=708 (#.) :: Coercible b c => (b -> c) -> (a -> b) -> (a -> c) _ #. x = coerce x++(#..) :: Coercible b c => (b -> c) -> (i -> a -> b) -> (i -> a -> c)+_ #.. x = coerce x #else (#.) :: (b -> c) -> (a -> b) -> (a -> c) _ #. x = unsafeCoerce x++(#..) :: (b -> c) -> (i -> a -> b) -> (i -> a -> c)+_ #.. x = unsafeCoerce x #endif-infixr 9 #.+infixr 9 #., #.. {-# INLINE (#.) #-}+{-# INLINE (#..)#-} skip :: a -> () skip _ = ()@@ -700,6 +714,12 @@ (j, ff) -> case ma j of ~(k, fa) -> (k, ff <*> fa) {-# INLINE (<*>) #-}+#if __GLASGOW_HASKELL__ >=821+ liftA2 f (Indexing ma) (Indexing mb) = Indexing $ \ i -> case ma i of+ (j, ja) -> case mb j of+ ~(k, kb) -> (k, liftA2 f ja kb)+ {-# INLINE liftA2 #-}+#endif ------------------------------------------------------------------------------- -- Strict curry