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.
sequenceList : List r -> Recursion.Rec r t (List t)
Traverse a list where the elements are recursive types.
type RoseTree a
= Node a (List (RoseTree a))
mapRoseTree : (a -> b) -> RoseTree a -> RoseTree b
mapRoseTree f =
runRecursion <|
\(Node a nodes) ->
sequenceList nodes
|> map (Node (f a))
sequenceListThen : List r -> (List t -> Recursion.Rec r t a) -> Recursion.Rec r t a
Traverse a list where the elements are recursive types and then perform a recursive action on the result.
traverseList : (x -> Recursion.Rec r t a) -> List x -> Recursion.Rec r t (List a)
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))
traverseListThen : (x -> Recursion.Rec r t a) -> List x -> (List a -> Recursion.Rec r t b) -> Recursion.Rec r t b
Traverse a list where the elements contain recursive types and then perform a recursive action on the result.
sequenceDict : Dict comparable r -> Recursion.Rec r t (Dict comparable t)
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 Node
sequenceDictThen : Dict comparable r -> (Dict comparable t -> Recursion.Rec r t a) -> Recursion.Rec r t a
Traverse a Dict
where the values are recursive types and then perform a recursive action on the result.
traverseDict : (comparable -> v -> Recursion.Rec r t a) -> Dict comparable v -> Recursion.Rec r t (Dict comparable a)
Traverse a Dict
where the values contain recursive types.
traverseDictThen : (comparable -> v -> Recursion.Rec r t a) -> Dict comparable v -> (Dict comparable a -> Recursion.Rec r t b) -> Recursion.Rec r t b
Traverse a Dict
where the values contain recursive types and then perform a recursive action the result.
sequenceArray : Array r -> Recursion.Rec r t (Array t)
Traverse an Array
where the values are recursive types.
sequenceArrayThen : Array r -> (Array t -> Recursion.Rec r t a) -> Recursion.Rec r t a
Traverse an Array
where the values are recursive types and then perform a recursive action on the result.
traverseArray : (x -> Recursion.Rec r t a) -> Array x -> Recursion.Rec r t (Array a)
Traverse an Array
where the values contain recursive types.
traverseArrayThen : (x -> Recursion.Rec r t a) -> Array x -> (Array a -> Recursion.Rec r t b) -> Recursion.Rec r t b
Traverse an Array
where the values contain recursive types and then perform a recursive action on the result.
sequenceMaybe : Maybe r -> Recursion.Rec r t (Maybe t)
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))
sequenceMaybeThen : Maybe r -> (Maybe t -> Recursion.Rec r t a) -> Recursion.Rec r t a
Traverse a Maybe
where the value might be a recursive type and then perform a recursive action on the result.
traverseMaybe : (x -> Recursion.Rec r t a) -> Maybe x -> Recursion.Rec r t (Maybe a)
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))
traverseMaybeThen : (x -> Recursion.Rec r t a) -> Maybe x -> (Maybe a -> Recursion.Rec r t b) -> Recursion.Rec r t b
Traverse a Maybe
where the value might contain a recursive type and then perform a recursive action on the result.
sequenceResult : Result e r -> Recursion.Rec r t (Result e t)
Traverse a Result
where the success value might be a recursive type.
sequenceResultThen : Result e r -> (Result e t -> Recursion.Rec r t a) -> Recursion.Rec r t a
Traverse a Result
where the success value might be a recursive type and then perform an action on the recursive result.
traverseResult : (v -> Recursion.Rec r t a) -> Result e v -> Recursion.Rec r t (Result e a)
Traverse a Result
where the success value might contain a recursive type.
traverseResultThen : (v -> Recursion.Rec r t a) -> Result e v -> (Result e a -> Recursion.Rec r t b) -> Recursion.Rec r t b
Traverse a Result
where the success value might contain a recursive type and then perform an action on the recursive result.