leaf_apply: Apply a function to leaves of a list.

Description Usage Arguments Details Value

Description

This function applies a function to leaves of a nested list (depth-first) if and only if the class of the leaf matches one of the names in class.

Usage

1
2
3
4
leaf_apply(l, f, class = NULL, name = "", sep = ".",
  strict.lists = FALSE, is.parent = if (strict.lists) function(x, n,
  ...) {     class(x)[1] == "list" } else function(x, n, ...) {    
  is.list(x) && (is.null(class) || !inherits(x, as.character(class))) })

Arguments

l

the list to be traversed.

f

the function to be applied to the leaves of the list.

class

a vector of class names (as quoted strings). All and only objects inheriting from any of these will be considered a leaf, even if they are also lists. Any objects that are neither lists nor inherit from class will be ignored.

name

a prefix for the names in the resulting list

sep

the separator to be used when creating the names of the leaves from the list nesting.

strict.lists

a logical. If TRUE, an item will only be considered a list if "list" is the first among its classes. Setting this to TRUE may be simpler than enumerating all of the possible leaf classes using class.

is.parent

a function of an item and item name that returns a logical indicating whether a node is a leaf. See the default vaule for examples.

Details

f must be of the form f(item, item_name) where item is a member of l and item_name is the concatenated name of item created by applying sep between node names in the list.

Value

a flat, named list of the results of applying f to the leaves of l.


EconModels/MacroGrowth documentation built on Dec. 17, 2019, 10:41 p.m.