micahhahn/elm-safe-recursion/2.2.0

Recursion.Fold

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

Prefer to use the functions that accept a continuation when possible (fold____Then) as they will be more efficient than folding and then mapping after.

List

foldList : (t -> a -> a) -> a -> List r -> Rec r t a

Fold a list of items which are recursive types.

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


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


                Node trees ->
                    foldList (+) 0 trees
foldListThen : (t -> a -> a) -> a -> List r -> (a -> Rec r t b) -> Rec r t b

Fold a list of items which are recurisve types and then perform a recursive action with the result.

foldMapList : (x -> a -> Rec r t a) -> a -> List x -> Rec r t a

Fold a list of items which contain recursive types.

type KeyedRoseTree a
    = Leaf a
    | Node (List ( String, KeyedRoseTree a ))


countRoseTreeLeaves : KeyedRoseTree a -> Int
countRoseTreeLeaves =
    runRecursion <|
        \tree ->
            case tree of
                Leaf _ ->
                    base 1


                Node trees ->
                    foldMapList
                        (\( _, rec ) count -> recurseMap rec ((+) count))
                        0
                        trees
foldMapListThen : (x -> a -> Rec r t a) -> a -> List x -> (a -> Rec r t b) -> Rec r t b

Fold a list of items which contain recursive types and then perform a recursive action with the result.

Dict

foldDict : (comparable -> t -> a -> a) -> a -> Dict comparable r -> Rec r t a

Fold a Dict whose values are recursive types.

type HashTrie a
    = Leaf a
    | Node (Dict Char (HashTrie a))


countHashTrie : HashTrie a -> Int
countHashTrie =
    runRecursion <|
        \tree ->
            case tree of
                Leaf _ ->
                    base 1


                Node trees ->
                    foldDict (\_ x count -> x + count) 0 trees
(comparable -> t -> a -> a)
-> a
-> Dict comparable r
-> (a -> Rec r t b)
-> Rec r t b

Fold a Dict whose values are recursive types and then perform a recursive action with the result.

foldMapDict : (comparable -> v -> a -> Rec r t a) -> a -> Dict comparable v -> Rec r t a

Fold a Dict whose values contain recursive types.

type HashTrie a
    = Leaf a
    | Node (Dict Char ( Int, HashTrie a ))


countHashTrie : HashTrie a -> Int
countHashTrie =
    runRecursion <|
        \tree ->
            case tree of
                Leaf _ ->
                    base 1


                Node trees ->
                    foldMapDict (\_ ( _, v ) count -> recurseMap v (\x -> x + count)) 0 trees
(comparable -> v -> a -> Rec r t a)
-> a
-> Dict comparable v
-> (a -> Rec r t b)
-> Rec r t b

Fold a Dict whose values contain recursive types and then perform a recursive action with the result.

Array

foldArray : (t -> a -> a) -> a -> Array r -> Rec r t a

Fold an Array whose items are recursive types.

foldArrayThen : (t -> a -> a) -> a -> Array r -> (a -> Rec r t b) -> Rec r t b

Fold an Array whose items are recursive types and then perform a recursive action with the result.

foldMapArray : (x -> a -> Rec r t a) -> a -> Array x -> Rec r t a

Fold an Array whose items contain recursive types.

foldMapArrayThen : (x -> a -> Rec r t a) -> a -> Array x -> (a -> Rec r t b) -> Rec r t b

Fold an Array whose items contain recursive types and then perform a recursive action with the result.