Skip to content

Commit 1a2d94e

Browse files
authored
Merge pull request #42 from MonoidMusician/master
Functor/Foldable/TraversableWithIndex
2 parents ff50250 + 579b6c0 commit 1a2d94e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/Data/Either.purs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import Data.Bifunctor (class Bifunctor)
99
import Data.Bitraversable (class Bitraversable)
1010
import Data.Eq (class Eq1)
1111
import Data.Foldable (class Foldable)
12+
import Data.FoldableWithIndex (class FoldableWithIndex)
1213
import Data.Functor.Invariant (class Invariant, imapF)
14+
import Data.FunctorWithIndex (class FunctorWithIndex)
1315
import Data.Maybe (Maybe(..), maybe, maybe')
1416
import Data.Ord (class Ord1)
1517
import 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
-- | ```
3538
derive instance functorEither :: Functor (Either a)
3639

40+
instance functorWithIndexEither :: FunctorWithIndex Unit (Either a) where
41+
mapWithIndex f = map $ f unit
42+
3743
instance 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+
189203
instance 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+
203221
instance bitraversableEither :: Bitraversable Either where
204222
bitraverse f _ (Left a) = Left <$> f a
205223
bitraverse _ g (Right b) = Right <$> g b

0 commit comments

Comments
 (0)