UP | HOME

foldable and traversable (haskell)

1. Foldable type class

Foldable is a typeclass (see Typeclasses in Haskell)

The key method of a Foldable instance is foldMap

class Foldable a where
foldMap :: Monoid m => (a -> m) -> t a -> m

Basically, you have a foldable context. You map everything in that context to a monoid (haskell) and then you use mappend to collapse all the monoids into one value.

2. Traversable type class

The key method of a Traversable instance is traverse

class (Functor t, Foldable t) => Traversable t where
traverse :: Applicative f => (a -> f b) -> t a -> f (t b)

Basically, you have some structure. You apply a function to everything in that structure (without changing it) to get a new context for each piece, and combine those pieces to get a new context for the structure.

See also: sequenceA in applicative functors.

Created: 2024-07-15 Mon 01:27