micahhahn/elm-safe-recursion/1.0.1

NOTE: the latest version is 2.2.0

Recursion.Fold

This module contains functions for folding common collections types that can contain recursive data structures.

List

foldlList : (x -> Rec a b c) -> (c -> d -> d) -> d -> List x -> Rec a b d

Reduces a list that contains recursive types from left to right.

type RoseTree a
    = Leaf a
    | Node (List (RoseTree a))


countRoseTreeLeaves : RoseTree a -> Int
countRoseTreeLeaves =
    runRecursion
        (\tree ->
            case tree of
                Leaf _ ->
                    base 1


                Node trees ->
                    foldlList recurse (+) 0 trees
        )
foldrList : (x -> Rec a b c) -> (c -> d -> d) -> d -> List x -> Rec a b d

Fold a list that contains recursive types from right to left.

type RoseTree a
    = Leaf a
    | Node (List (RoseTree a))


countRoseTreeLeaves : RoseTree a -> Int
countRoseTreeLeaves =
    runRecursion
        (\tree ->
            case tree of
                Leaf _ ->
                    base 1


                Node trees ->
                    foldrList recurse (+) 0 trees
        )

Favor foldlList if possible as it will be more efficient.

Dict

(comparable -> v -> Rec a b c)
-> (c -> d -> d)
-> d
-> Dict comparable v
-> Rec a b d

Fold a dictionary that contains recursive types in its value type in ascending order of keys.

(comparable -> v -> Rec a b c)
-> (c -> d -> d)
-> d
-> Dict comparable v
-> Rec a b d

Fold a dictionary that contains recursive types in its value type in descending order of keys.

Array

foldlArray : (x -> Rec a b c) -> (c -> d -> d) -> d -> Array x -> Rec a b d

Fold an array that contains recursive types from left to right.

foldrArray : (x -> Rec a b c) -> (c -> d -> d) -> d -> Array x -> Rec a b d

Fold an array that contains recursive types from right to left.