@@ -9,10 +9,13 @@ import Data.Bifunctor (class Bifunctor)
9
9
import Data.Bitraversable (class Bitraversable )
10
10
import Data.Eq (class Eq1 )
11
11
import Data.Foldable (class Foldable )
12
+ import Data.FoldableWithIndex (class FoldableWithIndex )
12
13
import Data.Functor.Invariant (class Invariant , imapF )
14
+ import Data.FunctorWithIndex (class FunctorWithIndex )
13
15
import Data.Maybe (Maybe (..), maybe , maybe' )
14
16
import Data.Ord (class Ord1 )
15
17
import Data.Traversable (class Traversable )
18
+ import Data.TraversableWithIndex (class TraversableWithIndex )
16
19
17
20
-- | The `Either` type is used to represent a choice between two types of value.
18
21
-- |
@@ -34,6 +37,9 @@ data Either a b = Left a | Right b
34
37
-- | ```
35
38
derive instance functorEither :: Functor (Either a )
36
39
40
+ instance functorWithIndexEither :: FunctorWithIndex Unit (Either a ) where
41
+ mapWithIndex f = map $ f unit
42
+
37
43
instance invariantEither :: Invariant (Either a ) where
38
44
imap = imapF
39
45
@@ -186,6 +192,14 @@ instance foldableEither :: Foldable (Either a) where
186
192
foldMap f (Left _) = mempty
187
193
foldMap f (Right x) = f x
188
194
195
+ instance foldableWithIndexEither :: FoldableWithIndex Unit (Either a ) where
196
+ foldrWithIndex _ z (Left _) = z
197
+ foldrWithIndex f z (Right x) = f unit x z
198
+ foldlWithIndex _ z (Left _) = z
199
+ foldlWithIndex f z (Right x) = f unit z x
200
+ foldMapWithIndex f (Left _) = mempty
201
+ foldMapWithIndex f (Right x) = f unit x
202
+
189
203
instance bifoldableEither :: Bifoldable Either where
190
204
bifoldr f _ z (Left a) = f a z
191
205
bifoldr _ g z (Right b) = g b z
@@ -200,6 +214,10 @@ instance traversableEither :: Traversable (Either a) where
200
214
sequence (Left x) = pure (Left x)
201
215
sequence (Right x) = Right <$> x
202
216
217
+ instance traversableWithIndexEither :: TraversableWithIndex Unit (Either a ) where
218
+ traverseWithIndex _ (Left x) = pure (Left x)
219
+ traverseWithIndex f (Right x) = Right <$> f unit x
220
+
203
221
instance bitraversableEither :: Bitraversable Either where
204
222
bitraverse f _ (Left a) = Left <$> f a
205
223
bitraverse _ g (Right b) = Right <$> g b
0 commit comments