Skip to main content

mapTree

Callable

  • mapTree<T, U>(x: Tree<T>, f: (x: T) => U): Tree<U>

  • Map a function over the tree starting with the root node, and mapping the children recursively.

    This function is fully lazy, meaning that we do not evaluate children until they are iterated on. Iteration of the children modify the iterators.

    Example

    const t = mapTree(tree(1, [tree(2), tree(3)]), (x) => x + 1)
    showTree(t)
    // => └─ 2
    // ├─ 3
    // └─ 4

    showTree(t)
    // => └─ 2

    Type parameters

    • T
    • U

    Parameters

    • x: Tree<T>

      The node root.

    • f: (x: T) => U

    Returns Tree<U>

    A mapped tree.