Skip to content

Commit 651d3b4

Browse files
committed
Foldable1 instance
1 parent 23f2a37 commit 651d3b4

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Data/NonEmpty.purs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Data.FoldableWithIndex (class FoldableWithIndex, foldMapWithIndex, foldlW
2424
import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
2525
import Data.Maybe (Maybe(..))
2626
import Data.Ord (class Ord1, compare1)
27+
import Data.Semigroup.Foldable as F1
2728
import Data.Traversable (class Traversable, traverse, sequence)
2829
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
2930

@@ -50,11 +51,11 @@ foldl1 f (a :| fa) = foldl f a fa
5051

5152
-- | Fold a non-empty structure, collecting results in a `Semigroup`.
5253
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
5455

5556
-- | Fold a non-empty structure.
5657
fold1 :: forall f s. Semigroup s => Foldable f => NonEmpty f s -> s
57-
fold1 = foldMap1 id
58+
fold1 = F1.fold1
5859

5960
fromNonEmpty :: forall f a r. (a -> f a -> r) -> NonEmpty f a -> r
6061
fromNonEmpty f (a :| fa) = a `f` fa
@@ -117,3 +118,7 @@ instance traversableWithIndexNonEmpty
117118
=> TraversableWithIndex (Maybe i) (NonEmpty f) where
118119
traverseWithIndex f (a :| fa) =
119120
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

Comments
 (0)