@@ -24,6 +24,7 @@ import Data.FoldableWithIndex (class FoldableWithIndex, foldMapWithIndex, foldlW
24
24
import Data.FunctorWithIndex (class FunctorWithIndex , mapWithIndex )
25
25
import Data.Maybe (Maybe (..))
26
26
import Data.Ord (class Ord1 , compare1 )
27
+ import Data.Semigroup.Foldable as F1
27
28
import Data.Traversable (class Traversable , traverse , sequence )
28
29
import Data.TraversableWithIndex (class TraversableWithIndex , traverseWithIndex )
29
30
@@ -50,11 +51,11 @@ foldl1 f (a :| fa) = foldl f a fa
50
51
51
52
-- | Fold a non-empty structure, collecting results in a `Semigroup`.
52
53
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
+ foldMap1 = F1 .foldMap1
54
55
55
56
-- | Fold a non-empty structure.
56
57
fold1 :: forall f s . Semigroup s => Foldable f => NonEmpty f s -> s
57
- fold1 = foldMap1 id
58
+ fold1 = F1 .fold1
58
59
59
60
fromNonEmpty :: forall f a r . (a -> f a -> r ) -> NonEmpty f a -> r
60
61
fromNonEmpty f (a :| fa) = a `f` fa
@@ -117,3 +118,7 @@ instance traversableWithIndexNonEmpty
117
118
=> TraversableWithIndex (Maybe i ) (NonEmpty f ) where
118
119
traverseWithIndex f (a :| fa) =
119
120
NonEmpty <$> f Nothing a <*> traverseWithIndex (f <<< Just ) fa
121
+
122
+ instance foldable1NonEmpty :: Foldable f => F1.Foldable1 (NonEmpty f ) where
123
+ fold1 = foldMap1 id
124
+ foldMap1 f (a :| fa) = foldl (\s a1 -> s <> f a1) (f a) fa
0 commit comments