Tutorial by Examples

import Data.Traversable as Traversable data MyType a = -- ... instance Traversable MyType where traverse = -- ... Every Traversable structure can be made a Foldable Functor using the fmapDefault and foldMapDefault functions found in Data.Traversable. instance Functor MyType where ...
Implementations of traverse usually look like an implementation of fmap lifted into an Applicative context. data Tree a = Leaf | Node (Tree a) a (Tree a) instance Traversable Tree where traverse f Leaf = pure Leaf traverse f (Node l x r) = Node <$> traverse f l <*...
A traversal can be run in the opposite direction with the help of the Backwards applicative functor, which flips an existing applicative so that composed effects take place in reversed order. newtype Backwards f a = Backwards { forwards :: f a } instance Applicative f => Applicative (Backward...
class (Functor t, Foldable t) => Traversable t where {-# MINIMAL traverse | sequenceA #-} traverse :: Applicative f => (a -> f b) -> t a -> f (t b) traverse f = sequenceA . fmap f sequenceA :: Applicative f => t (f a) -> f (t a) sequenceA = t...
The two mapAccum functions combine the operations of folding and mapping. -- A Traversable structure -- | -- A seed value | ...
If a type t is Traversable then values of t a can be split into two pieces: their "shape" and their "contents": data Traversed t a = Traversed { shape :: t (), contents :: [a] } where the "contents" are the same as what you'd "visit" using a Foldable insta...
Noting that zip transposes a tuple of lists into a list of tuples, ghci> uncurry zip ([1,2],[3,4]) [(1,3), (2,4)] and the similarity between the types of transpose and sequenceA, -- transpose exchanges the inner list with the outer list -- +---+-->--+-+ -- | | ...

Page 1 of 1