@@ -18,11 +18,14 @@ import Prelude
18
18
import Control.Alt ((<|>))
19
19
import Control.Alternative (class Alternative )
20
20
import Control.Plus (class Plus , empty )
21
-
22
21
import Data.Eq (class Eq1 , eq1 )
23
22
import Data.Foldable (class Foldable , foldl , foldr , foldMap )
23
+ import Data.FoldableWithIndex (class FoldableWithIndex , foldMapWithIndex , foldlWithIndex , foldrWithIndex )
24
+ import Data.FunctorWithIndex (class FunctorWithIndex , mapWithIndex )
25
+ import Data.Maybe (Maybe (..))
24
26
import Data.Ord (class Ord1 , compare1 )
25
27
import Data.Traversable (class Traversable , traverse , sequence )
28
+ import Data.TraversableWithIndex (class TraversableWithIndex , traverseWithIndex )
26
29
27
30
-- | A non-empty container of elements of type a.
28
31
-- |
@@ -88,11 +91,29 @@ instance ord1NonEmpty :: Ord1 f => Ord1 (NonEmpty f) where
88
91
instance functorNonEmpty :: Functor f => Functor (NonEmpty f ) where
89
92
map f (a :| fa) = f a :| map f fa
90
93
94
+ instance functorWithIndex
95
+ :: FunctorWithIndex i f
96
+ => FunctorWithIndex (Maybe i ) (NonEmpty f ) where
97
+ mapWithIndex f (a :| fa) = f Nothing a :| mapWithIndex (f <<< Just ) fa
98
+
91
99
instance foldableNonEmpty :: Foldable f => Foldable (NonEmpty f ) where
92
100
foldMap f (a :| fa) = f a <> foldMap f fa
93
101
foldl f b (a :| fa) = foldl f (f b a) fa
94
102
foldr f b (a :| fa) = f a (foldr f b fa)
95
103
104
+ instance foldableWithIndexNonEmpty
105
+ :: (FoldableWithIndex i f )
106
+ => FoldableWithIndex (Maybe i ) (NonEmpty f ) where
107
+ foldMapWithIndex f (a :| fa) = f Nothing a <> foldMapWithIndex (f <<< Just ) fa
108
+ foldlWithIndex f b (a :| fa) = foldlWithIndex (f <<< Just ) (f Nothing b a) fa
109
+ foldrWithIndex f b (a :| fa) = f Nothing a (foldrWithIndex (f <<< Just ) b fa)
110
+
96
111
instance traversableNonEmpty :: Traversable f => Traversable (NonEmpty f ) where
97
112
sequence (a :| fa) = NonEmpty <$> a <*> sequence fa
98
113
traverse f (a :| fa) = NonEmpty <$> f a <*> traverse f fa
114
+
115
+ instance traversableWithIndexNonEmpty
116
+ :: (TraversableWithIndex i f )
117
+ => TraversableWithIndex (Maybe i ) (NonEmpty f ) where
118
+ traverseWithIndex f (a :| fa) =
119
+ NonEmpty <$> f Nothing a <*> traverseWithIndex (f <<< Just ) fa
0 commit comments