@@ -9,10 +9,13 @@ import Data.Bifunctor (class Bifunctor)
99import Data.Bitraversable (class Bitraversable )
1010import Data.Eq (class Eq1 )
1111import Data.Foldable (class Foldable )
12+ import Data.FoldableWithIndex (class FoldableWithIndex )
1213import Data.Functor.Invariant (class Invariant , imapF )
14+ import Data.FunctorWithIndex (class FunctorWithIndex )
1315import Data.Maybe (Maybe (..), maybe , maybe' )
1416import Data.Ord (class Ord1 )
1517import Data.Traversable (class Traversable )
18+ import Data.TraversableWithIndex (class TraversableWithIndex )
1619
1720-- | The `Either` type is used to represent a choice between two types of value.
1821-- |
@@ -34,6 +37,9 @@ data Either a b = Left a | Right b
3437-- | ```
3538derive instance functorEither :: Functor (Either a )
3639
40+ instance functorWithIndexEither :: FunctorWithIndex Unit (Either a ) where
41+ mapWithIndex f = map $ f unit
42+
3743instance invariantEither :: Invariant (Either a ) where
3844 imap = imapF
3945
@@ -186,6 +192,14 @@ instance foldableEither :: Foldable (Either a) where
186192 foldMap f (Left _) = mempty
187193 foldMap f (Right x) = f x
188194
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+
189203instance bifoldableEither :: Bifoldable Either where
190204 bifoldr f _ z (Left a) = f a z
191205 bifoldr _ g z (Right b) = g b z
@@ -200,6 +214,10 @@ instance traversableEither :: Traversable (Either a) where
200214 sequence (Left x) = pure (Left x)
201215 sequence (Right x) = Right <$> x
202216
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+
203221instance bitraversableEither :: Bitraversable Either where
204222 bitraverse f _ (Left a) = Left <$> f a
205223 bitraverse _ g (Right b) = Right <$> g b
0 commit comments