File tree Expand file tree Collapse file tree 3 files changed +39
-1
lines changed
Expand file tree Collapse file tree 3 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,9 @@ module Data.FoldableWithIndex
1111 , allWithIndex
1212 , anyWithIndex
1313 , findWithIndex
14+ , foldrDefault
15+ , foldlDefault
16+ , foldMapDefault
1417 ) where
1518
1619import Prelude
@@ -271,3 +274,25 @@ findWithIndex p = foldlWithIndex go Nothing
271274 where
272275 go i Nothing x | p i x = Just x
273276 go i r _ = r
277+
278+ -- | A default implementation of `foldr` using `foldrWithIndex`
279+ foldrDefault
280+ :: forall i f a b
281+ . FoldableWithIndex i f
282+ => (a -> b -> b ) -> b -> f a -> b
283+ foldrDefault f = foldrWithIndex (const f)
284+
285+ -- | A default implementation of `foldl` using `foldlWithIndex`
286+ foldlDefault
287+ :: forall i f a b
288+ . FoldableWithIndex i f
289+ => (b -> a -> b ) -> b -> f a -> b
290+ foldlDefault f = foldlWithIndex (const f)
291+
292+ -- | A default implementation of `foldMap` using `foldMapWithIndex`
293+ foldMapDefault
294+ :: forall i f a m
295+ . FoldableWithIndex i f
296+ => Monoid m
297+ => (a -> m ) -> f a -> m
298+ foldMapDefault f = foldMapWithIndex (const f)
Original file line number Diff line number Diff line change 11module Data.FunctorWithIndex
2- ( class FunctorWithIndex , mapWithIndex
2+ ( class FunctorWithIndex , mapWithIndex , mapDefault
33 ) where
44
55import Prelude
@@ -55,3 +55,7 @@ instance functorWithIndexDisj :: FunctorWithIndex Unit Disj where
5555
5656instance functorWithIndexMultiplicative :: FunctorWithIndex Unit Multiplicative where
5757 mapWithIndex f = map $ f unit
58+
59+ -- | A default implementation of Functor's `map` in terms of `mapWithIndex`
60+ mapDefault :: forall i f a b . FunctorWithIndex i f => (a -> b ) -> f a -> f b
61+ mapDefault f = mapWithIndex (const f)
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ module Data.TraversableWithIndex
66 , mapAccumLWithIndex
77 , scanrWithIndex
88 , mapAccumRWithIndex
9+ , traverseDefault
910 , module Data.Traversable.Accum
1011 ) where
1112
@@ -166,3 +167,11 @@ mapAccumRWithIndex
166167 -> f a
167168 -> Accum s (f b )
168169mapAccumRWithIndex f s0 xs = stateR (traverseWithIndex (\i a -> StateR \s -> f i s a) xs) s0
170+
171+ -- | A default implementation of `traverse` in terms of `traverseWithIndex`
172+ traverseDefault
173+ :: forall i t a b m
174+ . TraversableWithIndex i t
175+ => Applicative m
176+ => (a -> m b ) -> t a -> m (t b )
177+ traverseDefault f = traverseWithIndex (const f)
You can’t perform that action at this time.
0 commit comments