returnsFunction: Checks whether a function returns a function?

returnsFunctionR Documentation

Checks whether a function returns a function?

Description

This function statically analyzes the code in a function to see if any of the possible return values could be a function.

Usage

returnsFunction(fun, recursive = FALSE, envir = globalenv(),
                functionsReturningFunctions = getFunReturningFunList(),
                any = FALSE)

Arguments

fun

function object

recursive

a logical value controlling whether we examine a return value that is a call to another function to see if that function returns a function.

envir

environment in which we look for functions identified by name

functionsReturningFunctions

a character vector of the names of functions known to return a function object.

any

currently ignored

Value

A list of the language objects that identify the return values that are functions.

Author(s)

Duncan Temple Lang

Examples

f3 = function(x) {
    if(length(x) > 3) {
        y = x[-(1:3)]
        function(z)
            prod(z + y)
    } else
        sum
}
# returns anonymous function and sum
returnsFunction(f3)


f3.5 = function(x) {
    sum = 2
    if(length(x) > 3) {
        y = x[-(1:3)]
        function(z)
            prod(z + y)
    } else
        sum
}
# returns only the anonymous function since sum
# is defined locally and not a function
returnsFunction(f3.5)


f2 = function(x)
    Vectorize(bar)
returnsFunction(f2, functionsReturningFunctions = "Vectorize")

duncantl/CodeAnalysis documentation built on Feb. 21, 2024, 10:49 p.m.