Skip to content

Commit b61328b

Browse files
authored
Remove fold1 from Foldable1 (#128)
* Remove fold1 from Foldable1 * Add back foldMap1Default * Deprecate and split foldMap1Default into foldMap1DefaultL and foldMap1DefaultR
1 parent 6f7a8fd commit b61328b

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

src/Data/Semigroup/Foldable.purs

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ module Data.Semigroup.Foldable
77
, traverse1_
88
, for1_
99
, sequence1_
10-
, foldMap1Default
11-
, fold1Default
12-
, fold1DefaultR
13-
, fold1DefaultL
1410
, foldr1Default
1511
, foldl1Default
12+
, foldMap1DefaultR
13+
, foldMap1DefaultL
14+
, foldMap1Default
1615
, intercalate
1716
, intercalateMap
1817
, maximum
@@ -29,67 +28,74 @@ import Data.Monoid.Multiplicative (Multiplicative(..))
2928
import Data.Newtype (ala, alaF)
3029
import Data.Ord.Max (Max(..))
3130
import Data.Ord.Min (Min(..))
31+
import Prim.TypeError (class Warn, Text)
3232

3333
-- | `Foldable1` represents data structures with a minimum of one element that can be _folded_.
3434
-- |
35-
-- | - `fold1` folds a structure using a `Semigroup` instance
36-
-- | - `foldMap1` folds a structure by accumulating values in a `Semigroup`
3735
-- | - `foldr1` folds a structure from the right
3836
-- | - `foldl1` folds a structure from the left
37+
-- | - `foldMap1` folds a structure by accumulating values in a `Semigroup`
3938
-- |
4039
-- | Default implementations are provided by the following functions:
4140
-- |
42-
-- | - `fold1Default`
43-
-- | - `fold1DefaultR`
44-
-- | - `fold1DefaultL`
45-
-- | - `foldMap1Default`
4641
-- | - `foldr1Default`
4742
-- | - `foldl1Default`
43+
-- | - `foldMap1DefaultR`
44+
-- | - `foldMap1DefaultL`
4845
-- |
4946
-- | Note: some combinations of the default implementations are unsafe to
5047
-- | use together - causing a non-terminating mutually recursive cycle.
5148
-- | These combinations are documented per function.
5249
class Foldable t <= Foldable1 t where
53-
foldMap1 :: forall a m. Semigroup m => (a -> m) -> t a -> m
54-
fold1 :: forall m. Semigroup m => t m -> m
5550
foldr1 :: forall a. (a -> a -> a) -> t a -> a
5651
foldl1 :: forall a. (a -> a -> a) -> t a -> a
57-
58-
-- | A default implementation of `fold1` using `foldMap1`.
59-
fold1Default :: forall t m. Foldable1 t => Semigroup m => t m -> m
60-
fold1Default = foldMap1 identity
61-
62-
-- | A default implementation of `fold1` using `foldr1`.
63-
fold1DefaultR :: forall t m. Foldable1 t => Semigroup m => t m -> m
64-
fold1DefaultR = foldr1 append
65-
66-
-- | A default implementation of `fold1` using `foldl1`.
67-
fold1DefaultL :: forall t m. Foldable1 t => Semigroup m => t m -> m
68-
fold1DefaultL = foldl1 append
69-
70-
-- | A default implementation of `foldMap1` using `fold1`.
71-
foldMap1Default :: forall t m a. Foldable1 t => Functor t => Semigroup m => (a -> m) -> t a -> m
72-
foldMap1Default f = (map f) >>> fold1
52+
foldMap1 :: forall a m. Semigroup m => (a -> m) -> t a -> m
7353

7454
-- | A default implementation of `foldr1` using `foldMap1`.
55+
-- |
56+
-- | Note: when defining a `Foldable1` instance, this function is unsafe to use
57+
-- | in combination with `foldMap1DefaultR`.
7558
foldr1Default :: forall t a. Foldable1 t => (a -> a -> a) -> t a -> a
7659
foldr1Default = flip (runFoldRight1 <<< foldMap1 mkFoldRight1)
7760

7861
-- | A default implementation of `foldl1` using `foldMap1`.
62+
-- |
63+
-- | Note: when defining a `Foldable1` instance, this function is unsafe to use
64+
-- | in combination with `foldMap1DefaultL`.
7965
foldl1Default :: forall t a. Foldable1 t => (a -> a -> a) -> t a -> a
8066
foldl1Default = flip (runFoldRight1 <<< alaF Dual foldMap1 mkFoldRight1) <<< flip
8167

68+
-- | A default implementation of `foldMap1` using `foldr1`.
69+
-- |
70+
-- | Note: when defining a `Foldable1` instance, this function is unsafe to use
71+
-- | in combination with `foldr1Default`.
72+
foldMap1DefaultR :: forall t m a. Foldable1 t => Functor t => Semigroup m => (a -> m) -> t a -> m
73+
foldMap1DefaultR f = map f >>> foldr1 (<>)
74+
75+
-- | A default implementation of `foldMap1` using `foldl1`.
76+
-- |
77+
-- | Note: when defining a `Foldable1` instance, this function is unsafe to use
78+
-- | in combination with `foldl1Default`.
79+
foldMap1DefaultL :: forall t m a. Foldable1 t => Functor t => Semigroup m => (a -> m) -> t a -> m
80+
foldMap1DefaultL f = map f >>> foldl1 (<>)
81+
82+
-- | Deprecated previous name of `foldMap1DefaultL`.
83+
foldMap1Default :: forall t m a. Warn (Text "'foldMap1Default' is deprecated, use 'foldMap1DefaultL' instead") => Foldable1 t => Functor t => Semigroup m => (a -> m) -> t a -> m
84+
foldMap1Default = foldMap1DefaultL
85+
8286
instance foldableDual :: Foldable1 Dual where
83-
foldMap1 f (Dual x) = f x
84-
fold1 = fold1Default
8587
foldr1 _ (Dual x) = x
8688
foldl1 _ (Dual x) = x
89+
foldMap1 f (Dual x) = f x
8790

8891
instance foldableMultiplicative :: Foldable1 Multiplicative where
89-
foldMap1 f (Multiplicative x) = f x
90-
fold1 = fold1Default
9192
foldr1 _ (Multiplicative x) = x
9293
foldl1 _ (Multiplicative x) = x
94+
foldMap1 f (Multiplicative x) = f x
95+
96+
-- | Fold a data structure, accumulating values in some `Semigroup`.
97+
fold1 :: forall t m. Foldable1 t => Semigroup m => t m -> m
98+
fold1 = foldMap1 identity
9399

94100
newtype Act :: forall k. (k -> Type) -> k -> Type
95101
newtype Act f a = Act (f a)

test/Main.purs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Data.Int (toNumber, pow)
1313
import Data.Maybe (Maybe(..))
1414
import Data.Monoid.Additive (Additive(..))
1515
import Data.Newtype (unwrap)
16-
import Data.Semigroup.Foldable (class Foldable1, foldr1, foldl1, fold1Default, foldr1Default, foldl1Default)
16+
import Data.Semigroup.Foldable (class Foldable1, foldr1, foldl1, foldr1Default, foldl1Default)
1717
import Data.Semigroup.Foldable as Foldable1
1818
import Data.Traversable (class Traversable, sequenceDefault, traverse, sequence, traverseDefault)
1919
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
@@ -36,10 +36,9 @@ instance foldableNEArray :: Foldable NEArray where
3636
foldr f = foldrDefault f
3737

3838
instance foldable1NEArray :: Foldable1 NEArray where
39-
foldMap1 = foldMap1NEArray append
40-
fold1 = fold1Default
4139
foldr1 f = foldr1Default f
4240
foldl1 f = foldl1Default f
41+
foldMap1 = foldMap1NEArray append
4342

4443
maybeMkNEArray :: forall a. Array a -> Maybe (NEArray a)
4544
maybeMkNEArray = mkNEArray Nothing Just

0 commit comments

Comments
 (0)