micahhahn / elm-safe-recursion / 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 -> Recursion.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 -> Recursion.Rec r t b) -> Recursion.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 -> Recursion.Rec r t a) -> a -> List x -> Recursion.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 -> Recursion.Rec r t a) -> a -> List x -> (a -> Recursion.Rec r t b) -> Recursion.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 -> Recursion.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

foldDictThen : (comparable -> t -> a -> a) -> a -> Dict comparable r -> (a -> Recursion.Rec r t b) -> Recursion.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 -> Recursion.Rec r t a) -> a -> Dict comparable v -> Recursion.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

foldMapDictThen : (comparable -> v -> a -> Recursion.Rec r t a) -> a -> Dict comparable v -> (a -> Recursion.Rec r t b) -> Recursion.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 -> Recursion.Rec r t a

Fold an Array whose items are recursive types.

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

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

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

Fold an Array whose items contain recursive types.

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

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