unfoldable-0.1.0: Class of data structures that can be unfolded from a seed value.

Safe HaskellSafe-Infered

Data.Unfoldable

Contents

Synopsis

Documentation

class Unfoldable f whereSource

Data structures that can be unfolded.

For example, given a data type

 data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)

a suitable instance would be

 instance Unfoldable Tree where
   unfoldMap f = choose
     [ spread $ pure Empty
     , spread $ Leaf <$> to f
     , spread $ Node <$> to (unfoldMap f) <*> to f <*> to (unfoldMap f)
     ]

i.e. it follows closely the instance for Traversable, with the addition of choose, spread and to.

The instance can be simplified to:

 instance Unfoldable Tree where
   unfoldMap f = choose
     [ const Empty
     , Leaf . f
     , spread $ Node <$> to (unfoldMap f) <*> to f <*> to (unfoldMap f)
     ]

Methods

unfoldMap :: Splittable s => (s -> a) -> s -> f aSource

Given a function to generate an element from a seed, and an initial seed, generate a structure.

Specific unfolds

unfold :: (Unfoldable f, Splittable s) => s -> f sSource

Helper functions

spread :: Splittable s => State ([s], Int) a -> s -> aSource

to :: (s -> a) -> State ([s], Int) aSource