This module provides traversals for common data structures that contain recursive types.
Prefer to use the functions that accept a continuation when possible (sequence____Then or traverse____Then) as they will be more efficient than folding and then mapping after.
A traversal is a transformation over a structure that preserves the shape of the structure. In this module, the traversal functions allow us to convert from a structure containing recursive types into a recursive type containing the structure.
If you are trying to write a map function over a recursive data structure, a traversal is likely what you want.
Traverse a list where the elements are recursive types and then perform a recursive action on the result.
Traverse a list where the elements contain recursive types.
type KeyedRoseTree a
= Node a (List ( String, KeyedRoseTree a ))
mapKeyedRoseTree : (a -> b) -> KeyedRoseTree a -> KeyedRoseTree b
mapKeyedRoseTree f =
runRecursion <|
\(Node a nodes) ->
traverseList (\( s, tree ) -> recurseMap tree (Tuple.pair s)) nodes
|> map (Node (f a))Traverse a list where the elements contain recursive types and then perform a recursive action on the result.
Traverse a Dict where the values are recursive types.
type HashTrie a
= Leaf a
| Node (Dict Char (HashTrie a))
mapHashTrie : (a -> b) -> HashTrie a -> HashTrie b
mapHashTrie f =
runRecursion <|
\tree ->
case tree of
Leaf a ->
base (Leaf (f a))
Node dict ->
sequenceDict dict
|> map NodeTraverse a Dict where the values are recursive types and then perform a recursive action on the result.
Traverse a Dict where the values contain recursive types.
Traverse a Dict where the values contain recursive types and then perform a recursive action the result.
Traverse an Array where the values are recursive types.
Traverse an Array where the values are recursive types and then perform a recursive action on the result.
Traverse an Array where the values contain recursive types.
Traverse an Array where the values contain recursive types and then perform a recursive action on the result.
Traverse a Maybe where the value might be a recursive type.
type NonEmpty a
= NonEmpty a (Maybe (NonEmpty a))
mapNonEmpty : (a -> b) -> NonEmpty a -> NonEmpty b
mapNonEmpty f =
runRecursion <|
\(NonEmpty v maybe) ->
sequenceMaybe maybe
|> map (NonEmpty (f v))Traverse a Maybe where the value might be a recursive type and then perform a recursive action on the result.
Traverse a Maybe where the value might contain a recursive type.
type SeparatedList sep val
= SeparatedList val (Maybe ( sep, SeparatedList sep val ))
mapSeparatedList : (a -> b) -> SeparatedList sep a -> SeparatedList sep b
mapSeparatedList f =
runRecursion <|
\(SeparatedList a maybeOthers) ->
maybeOthers
|> traverseMaybe (\( sep, sepList ) -> recurseMap sepList (Tuple.pair sep))
|> map (SeparatedList (f a))Traverse a Maybe where the value might contain a recursive type and then perform a recursive action on the result.
Traverse a Result where the success value might be a recursive type.
Traverse a Result where the success value might be a recursive type and then perform an action on the recursive result.
Traverse a Result where the success value might contain a recursive type.
Traverse a Result where the success value might contain a recursive type and then perform an action on the recursive result.
Traverse a list where the elements are recursive types.