Traversable
Overview
Hackage link
Class of data structures that can be traversed from left to right, performing an action on each element.
Intuitively, for a Traversable t , t a is like a ”container” for a’s that you can inspect and manipulate.
Defining an instance of Traversable class
Similarly to how we can use and liftM defined and ap to define instances, once we have defined return and (>>=), Data.Traversable provides fmapDefault and foldMapDefault to implement Functor and Foldable, once we defined traverse.
code:classintance.hs
-- Assuming mapT is well defined
instance Functor Tree where
fmap = fmapDefault
instance Foldable Tree where
foldMap = foldMapDefault
instance Traversable Tree where
traverse = mapT
... or you can use Traversable type class (use language extension DeriveTraversable)