diff --git a/CHANGELOG.md b/CHANGELOG.md index 81e1e50..728aac9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Breaking changes: New features: +- Added a `FunctorWithIndex` instance for `Cofree` (#118 by @mikesol) + Bugfixes: Other improvements: diff --git a/src/Control/Comonad/Cofree.purs b/src/Control/Comonad/Cofree.purs index 91bb6e8..15a0d6a 100644 --- a/src/Control/Comonad/Cofree.purs +++ b/src/Control/Comonad/Cofree.purs @@ -22,6 +22,7 @@ import Control.Monad.Free (Free, runFreeM) import Control.Monad.Rec.Class (class MonadRec) import Control.Monad.State (State, StateT(..), runState, runStateT, state) import Data.Eq (class Eq1, eq1) +import Data.FunctorWithIndex (class FunctorWithIndex) import Data.Foldable (class Foldable, foldr, foldl, foldMap) import Data.Lazy (Lazy, force, defer) import Data.Ord (class Ord1, compare1) @@ -145,6 +146,11 @@ instance functorCofree :: Functor f => Functor (Cofree f) where where loop (Cofree fa) = Cofree ((\(Tuple a b) -> Tuple (f a) (loop <$> b)) <$> fa) +instance functorWithIndexCofree :: Functor f => FunctorWithIndex Int (Cofree f) where + mapWithIndex f = loop 0 + where + loop n (Cofree fa) = Cofree ((\(Tuple a b) -> Tuple (f n a) (loop (n + 1) <$> b)) <$> fa) + instance foldableCofree :: Foldable f => Foldable (Cofree f) where foldr f = flip go where