Skip to content

Commit 468948e

Browse files
authored
Merge pull request #88 from matthewleon/default-from-indexed
default non-indexed impls using indexed impls
2 parents 96f4b0c + f213eb0 commit 468948e

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/Data/FoldableWithIndex.purs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module Data.FoldableWithIndex
1111
, allWithIndex
1212
, anyWithIndex
1313
, findWithIndex
14+
, foldrDefault
15+
, foldlDefault
16+
, foldMapDefault
1417
) where
1518

1619
import 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)

src/Data/FunctorWithIndex.purs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Data.FunctorWithIndex
2-
( class FunctorWithIndex, mapWithIndex
2+
( class FunctorWithIndex, mapWithIndex, mapDefault
33
) where
44

55
import Prelude
@@ -55,3 +55,7 @@ instance functorWithIndexDisj :: FunctorWithIndex Unit Disj where
5555

5656
instance 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)

src/Data/TraversableWithIndex.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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)
168169
mapAccumRWithIndex 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)

0 commit comments

Comments
 (0)