Skip to content

Commit daa432b

Browse files
committed
Add back foldMap1Default
1 parent af7f480 commit daa432b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/Data/Semigroup/Foldable.purs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Data.Semigroup.Foldable
99
, sequence1_
1010
, foldr1Default
1111
, foldl1Default
12+
, foldMap1Default
1213
, intercalate
1314
, intercalateMap
1415
, maximum
@@ -36,6 +37,11 @@ import Data.Ord.Min (Min(..))
3637
-- |
3738
-- | - `foldr1Default`
3839
-- | - `foldl1Default`
40+
-- | - `foldMap1Default`
41+
-- |
42+
-- | Note: some combinations of the default implementations are unsafe to
43+
-- | use together - causing a non-terminating mutually recursive cycle.
44+
-- | These combinations are documented per function.
3945
class Foldable t <= Foldable1 t where
4046
foldr1 :: forall a. (a -> a -> a) -> t a -> a
4147
foldl1 :: forall a. (a -> a -> a) -> t a -> a
@@ -46,9 +52,19 @@ foldr1Default :: forall t a. Foldable1 t => (a -> a -> a) -> t a -> a
4652
foldr1Default = flip (runFoldRight1 <<< foldMap1 mkFoldRight1)
4753

4854
-- | A default implementation of `foldl1` using `foldMap1`.
55+
-- |
56+
-- | Note: when defining a `Foldable1` instance, this function is unsafe to use
57+
-- | in combination with `foldMap1Default`.
4958
foldl1Default :: forall t a. Foldable1 t => (a -> a -> a) -> t a -> a
5059
foldl1Default = flip (runFoldRight1 <<< alaF Dual foldMap1 mkFoldRight1) <<< flip
5160

61+
-- | A default implementation of `foldMap1` using `foldl1`.
62+
-- |
63+
-- | Note: when defining a `Foldable1` instance, this function is unsafe to use
64+
-- | in combination with `foldl1Default`.
65+
foldMap1Default :: forall t m a. Foldable1 t => Functor t => Semigroup m => (a -> m) -> t a -> m
66+
foldMap1Default f = map f >>> foldl1 (<>)
67+
5268
instance foldableDual :: Foldable1 Dual where
5369
foldr1 _ (Dual x) = x
5470
foldl1 _ (Dual x) = x

0 commit comments

Comments
 (0)