Skip to content

Add Functor-, Foldable-, and TraversableWithIndex instances to EnvT #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

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

Bugfixes:

Expand Down
14 changes: 14 additions & 0 deletions src/Control/Comonad/Env/Trans.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import Control.Comonad (class Comonad, extract)
import Control.Comonad.Trans.Class (class ComonadTrans)
import Control.Extend (class Extend, (<<=))

import Data.FoldableWithIndex (class FoldableWithIndex, foldrWithIndex, foldlWithIndex, foldMapWithIndex)
import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
import Data.Traversable (class Traversable, class Foldable, foldl, foldr, foldMap, traverse, sequence)
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
import Data.Tuple (Tuple(..))
import Data.Newtype (class Newtype)

Expand Down Expand Up @@ -55,3 +58,14 @@ instance foldableEnvT :: Foldable f => Foldable (EnvT e f) where
instance traversableEnvT :: Traversable f => Traversable (EnvT e f) where
sequence (EnvT (Tuple a x)) = EnvT <$> Tuple a <$> sequence x
traverse f (EnvT (Tuple a x)) = EnvT <$> Tuple a <$> traverse f x

instance functorWithIndexEnvT :: FunctorWithIndex i w => FunctorWithIndex i (EnvT e w) where
mapWithIndex f (EnvT (Tuple e x)) = EnvT $ Tuple e (mapWithIndex f x)

instance foldableWithIndexEnvT :: FoldableWithIndex i w => FoldableWithIndex i (EnvT e w) where
foldlWithIndex f a (EnvT (Tuple _ x)) = foldlWithIndex f a x
foldrWithIndex f a (EnvT (Tuple _ x)) = foldrWithIndex f a x
foldMapWithIndex f (EnvT (Tuple _ x)) = foldMapWithIndex f x

instance traversableWithIndexEnvT :: TraversableWithIndex i w => TraversableWithIndex i (EnvT e w) where
traverseWithIndex f (EnvT (Tuple e x)) = EnvT <$> Tuple e <$> traverseWithIndex f x