Safe Haskell | Safe-Infered |
---|
- class Unfoldable f where
- unfoldMap :: Splittable s => (s -> a) -> s -> f a
- unfold :: (Unfoldable f, Splittable s) => s -> f s
- leftMost :: Unfoldable f => f ()
- rightMost :: Unfoldable f => f ()
- spread :: Splittable s => State ([s], Int) a -> s -> a
- to :: (s -> a) -> State ([s], Int) a
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) ]
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.
Unfoldable [] | |
Unfoldable Maybe | |
(Bounded a, Enum a) => Unfoldable (Either a) | |
(Bounded a, Enum a) => Unfoldable ((,) a) |
Specific unfolds
unfold :: (Unfoldable f, Splittable s) => s -> f sSource
leftMost :: Unfoldable f => f ()Source
rightMost :: Unfoldable f => f ()Source
Helper functions
spread :: Splittable s => State ([s], Int) a -> s -> aSource