Skip to content

Commit 95c4e7e

Browse files
Add Functor-, Foldable-, and TraversableWithIndex instances to EnvT (#113)
* Add Functor-, Foldable-, and TraversableWithIndex to EnvT * Update changelog Co-authored-by: Jordan Martinez <[email protected]>
1 parent 1e5d419 commit 95c4e7e

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Add `Foldable`, `FoldableWithIndex`, and `Traversable` instances for `EnvT` (#113 by @abaco)
1011

1112
Bugfixes:
1213

src/Control/Comonad/Env/Trans.purs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import Control.Comonad (class Comonad, extract)
88
import Control.Comonad.Trans.Class (class ComonadTrans)
99
import Control.Extend (class Extend, (<<=))
1010

11+
import Data.FoldableWithIndex (class FoldableWithIndex, foldrWithIndex, foldlWithIndex, foldMapWithIndex)
12+
import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
1113
import Data.Traversable (class Traversable, class Foldable, foldl, foldr, foldMap, traverse, sequence)
14+
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
1215
import Data.Tuple (Tuple(..))
1316
import Data.Newtype (class Newtype)
1417

@@ -55,3 +58,14 @@ instance foldableEnvT :: Foldable f => Foldable (EnvT e f) where
5558
instance traversableEnvT :: Traversable f => Traversable (EnvT e f) where
5659
sequence (EnvT (Tuple a x)) = EnvT <$> Tuple a <$> sequence x
5760
traverse f (EnvT (Tuple a x)) = EnvT <$> Tuple a <$> traverse f x
61+
62+
instance functorWithIndexEnvT :: FunctorWithIndex i w => FunctorWithIndex i (EnvT e w) where
63+
mapWithIndex f (EnvT (Tuple e x)) = EnvT $ Tuple e (mapWithIndex f x)
64+
65+
instance foldableWithIndexEnvT :: FoldableWithIndex i w => FoldableWithIndex i (EnvT e w) where
66+
foldlWithIndex f a (EnvT (Tuple _ x)) = foldlWithIndex f a x
67+
foldrWithIndex f a (EnvT (Tuple _ x)) = foldrWithIndex f a x
68+
foldMapWithIndex f (EnvT (Tuple _ x)) = foldMapWithIndex f x
69+
70+
instance traversableWithIndexEnvT :: TraversableWithIndex i w => TraversableWithIndex i (EnvT e w) where
71+
traverseWithIndex f (EnvT (Tuple e x)) = EnvT <$> Tuple e <$> traverseWithIndex f x

0 commit comments

Comments
 (0)