@@ -5,12 +5,11 @@ module Data.NonEmpty
5
5
, singleton
6
6
, (:|)
7
7
, foldl1
8
- , foldMap1
9
- , fold1
10
8
, fromNonEmpty
11
9
, oneOf
12
10
, head
13
11
, tail
12
+ , module Data.Semigroup.Foldable
14
13
) where
15
14
16
15
import Prelude
@@ -24,6 +23,8 @@ import Data.FoldableWithIndex (class FoldableWithIndex, foldMapWithIndex, foldlW
24
23
import Data.FunctorWithIndex (class FunctorWithIndex , mapWithIndex )
25
24
import Data.Maybe (Maybe (..))
26
25
import Data.Ord (class Ord1 , compare1 )
26
+ import Data.Semigroup.Foldable (fold1 , foldMap1 )
27
+ import Data.Semigroup.Foldable as SF
27
28
import Data.Traversable (class Traversable , traverse , sequence )
28
29
import Data.TraversableWithIndex (class TraversableWithIndex , traverseWithIndex )
29
30
@@ -48,14 +49,6 @@ singleton a = a :| empty
48
49
foldl1 :: forall f a . Foldable f => (a -> a -> a ) -> NonEmpty f a -> a
49
50
foldl1 f (a :| fa) = foldl f a fa
50
51
51
- -- | Fold a non-empty structure, collecting results in a `Semigroup`.
52
- foldMap1 :: forall f a s . Semigroup s => Foldable f => (a -> s ) -> NonEmpty f a -> s
53
- foldMap1 f (a :| fa) = foldl (\s a1 -> s <> f a1) (f a) fa
54
-
55
- -- | Fold a non-empty structure.
56
- fold1 :: forall f s . Semigroup s => Foldable f => NonEmpty f s -> s
57
- fold1 = foldMap1 id
58
-
59
52
fromNonEmpty :: forall f a r . (a -> f a -> r ) -> NonEmpty f a -> r
60
53
fromNonEmpty f (a :| fa) = a `f` fa
61
54
@@ -117,3 +110,9 @@ instance traversableWithIndexNonEmpty
117
110
=> TraversableWithIndex (Maybe i ) (NonEmpty f ) where
118
111
traverseWithIndex f (a :| fa) =
119
112
NonEmpty <$> f Nothing a <*> traverseWithIndex (f <<< Just ) fa
113
+
114
+ instance foldable1NonEmpty
115
+ :: (Functor f , Foldable f )
116
+ => SF.Foldable1 (NonEmpty f ) where
117
+ fold1 (a :| fa) = foldl append a fa
118
+ foldMap1 = SF .foldMap1Default
0 commit comments