Skip to content

Commit 9f231ff

Browse files
authored
Deprecate monomorphic foldl1 (#42)
* Deprecate monomorphic foldl1 * Suggest the polymorphic foldl1 from Data.Semigroup.Foldable instead
1 parent 291a90a commit 9f231ff

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/Data/NonEmpty.purs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
2323
import Data.Maybe (Maybe(..), maybe)
2424
import Data.Ord (class Ord1)
2525
import Data.Semigroup.Foldable (class Foldable1, foldMap1)
26+
import Data.Semigroup.Foldable (foldl1) as Foldable1
2627
import Data.Traversable (class Traversable, traverse, sequence)
2728
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
2829
import Data.Tuple (uncurry)
2930
import Data.Unfoldable (class Unfoldable, unfoldr)
3031
import Data.Unfoldable1 (class Unfoldable1)
32+
import Prim.TypeError (class Warn, Text)
3133

3234
-- | A non-empty container of elements of type a.
3335
-- |
@@ -47,8 +49,8 @@ singleton :: forall f a. Plus f => a -> NonEmpty f a
4749
singleton a = a :| empty
4850

4951
-- | Fold a non-empty structure, collecting results using a binary operation.
50-
foldl1 :: forall f a. Foldable f => (a -> a -> a) -> NonEmpty f a -> a
51-
foldl1 f (a :| fa) = foldl f a fa
52+
foldl1 :: forall f a. Foldable f => Warn (Text "'Data.NonEmpty.foldl1' is deprecated, use 'Data.Semigroup.Foldable.foldl1' instead") => (a -> a -> a) -> NonEmpty f a -> a
53+
foldl1 = Foldable1.foldl1
5254

5355
fromNonEmpty :: forall f a r. (a -> f a -> r) -> NonEmpty f a -> r
5456
fromNonEmpty f (a :| fa) = a `f` fa
@@ -108,7 +110,7 @@ instance foldable1NonEmpty :: Foldable f => Foldable1 (NonEmpty f) where
108110
fold1 = foldMap1 identity
109111
foldMap1 f (a :| fa) = foldl (\s a1 -> s <> f a1) (f a) fa
110112
foldr1 f (a :| fa) = maybe a (f a) $ foldr (\a1 -> Just <<< maybe a1 (f a1)) Nothing fa
111-
foldl1 = foldl1
113+
foldl1 f (a :| fa) = foldl f a fa
112114

113115
instance unfoldable1NonEmpty :: Unfoldable f => Unfoldable1 (NonEmpty f) where
114116
unfoldr1 f b = uncurry (:|) $ unfoldr (map f) <$> f b

0 commit comments

Comments
 (0)