@@ -7,12 +7,11 @@ module Data.Semigroup.Foldable
7
7
, traverse1_
8
8
, for1_
9
9
, sequence1_
10
- , foldMap1Default
11
- , fold1Default
12
- , fold1DefaultR
13
- , fold1DefaultL
14
10
, foldr1Default
15
11
, foldl1Default
12
+ , foldMap1DefaultR
13
+ , foldMap1DefaultL
14
+ , foldMap1Default
16
15
, intercalate
17
16
, intercalateMap
18
17
, maximum
@@ -29,67 +28,74 @@ import Data.Monoid.Multiplicative (Multiplicative(..))
29
28
import Data.Newtype (ala , alaF )
30
29
import Data.Ord.Max (Max (..))
31
30
import Data.Ord.Min (Min (..))
31
+ import Prim.TypeError (class Warn , Text )
32
32
33
33
-- | `Foldable1` represents data structures with a minimum of one element that can be _folded_.
34
34
-- |
35
- -- | - `fold1` folds a structure using a `Semigroup` instance
36
- -- | - `foldMap1` folds a structure by accumulating values in a `Semigroup`
37
35
-- | - `foldr1` folds a structure from the right
38
36
-- | - `foldl1` folds a structure from the left
37
+ -- | - `foldMap1` folds a structure by accumulating values in a `Semigroup`
39
38
-- |
40
39
-- | Default implementations are provided by the following functions:
41
40
-- |
42
- -- | - `fold1Default`
43
- -- | - `fold1DefaultR`
44
- -- | - `fold1DefaultL`
45
- -- | - `foldMap1Default`
46
41
-- | - `foldr1Default`
47
42
-- | - `foldl1Default`
43
+ -- | - `foldMap1DefaultR`
44
+ -- | - `foldMap1DefaultL`
48
45
-- |
49
46
-- | Note: some combinations of the default implementations are unsafe to
50
47
-- | use together - causing a non-terminating mutually recursive cycle.
51
48
-- | These combinations are documented per function.
52
49
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
55
50
foldr1 :: forall a . (a -> a -> a ) -> t a -> a
56
51
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
73
53
74
54
-- | 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`.
75
58
foldr1Default :: forall t a . Foldable1 t => (a -> a -> a ) -> t a -> a
76
59
foldr1Default = flip (runFoldRight1 <<< foldMap1 mkFoldRight1)
77
60
78
61
-- | 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`.
79
65
foldl1Default :: forall t a . Foldable1 t => (a -> a -> a ) -> t a -> a
80
66
foldl1Default = flip (runFoldRight1 <<< alaF Dual foldMap1 mkFoldRight1) <<< flip
81
67
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
+
82
86
instance foldableDual :: Foldable1 Dual where
83
- foldMap1 f (Dual x) = f x
84
- fold1 = fold1Default
85
87
foldr1 _ (Dual x) = x
86
88
foldl1 _ (Dual x) = x
89
+ foldMap1 f (Dual x) = f x
87
90
88
91
instance foldableMultiplicative :: Foldable1 Multiplicative where
89
- foldMap1 f (Multiplicative x) = f x
90
- fold1 = fold1Default
91
92
foldr1 _ (Multiplicative x) = x
92
93
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
93
99
94
100
newtype Act :: forall k . (k -> Type ) -> k -> Type
95
101
newtype Act f a = Act (f a )
0 commit comments