Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Parsing inner functions #4

Open
yulric opened this issue Mar 16, 2022 · 0 comments
Open

Parsing inner functions #4

yulric opened this issue Mar 16, 2022 · 0 comments

Comments

@yulric
Copy link
Contributor

yulric commented Mar 16, 2022

An inner function is a function declared inside of another one, for example,

function_one <- function() {
    function_two <- function() {

    }
}

In the above R code, function_two is an inner function.

Things to consider when parsing an inner function:

  • The name of the inner function: The name should make it clear that this is a function inside of another function and should have the name of the parent function in it. Proposing that we use the dot notation to represent that the inner function is a property of the parent function. For example, in the above example the name of the inner function would be function_one.function_two.
  • Handling closure variables: The inner function is able to use variables that are in the parent function's environment. For example,
    function_one <- function(a) {
        function_two <- function(b) {
             return(a + b)
        }
    }
    
    In the above example, function_two is able to use the variable a even though its not declared anywhere in its scope. In PMML however each function has only one scope, its own. To accommodate this R feature when the inner function is parsed into PMML:
    • Any accessed variables that are not in its scope will need to be added as extra parameters to the inner function's signature
    • The names of these new parameters should use the same dot notation as in the inner function's name.
    • Keeping the above points into account, the PMML for function_two would look like,
    <Apply function="function_one.function_two">
        <ParameterField name="b" dataType="double" />
        <ParameterField name="function_one.a" dataType="double" />
        <Apply function="+">
            <FieldRef field="function_one.a"/>>
            <FieldRef field="b"/>
        </Apply>
    </Apply>
    
  • Closure variables as well as variables within the inner function can be mutated which should be taken into account
    • Before parsing the function body we need to apply the mutation algorithm which changes all the mutated variables names in the tokens
  • The inner function can mutate the closured variables using <<-
  • The inner function can have parameters with the same name as the parent function
  • When the parent function calls the inner function
  • Keep in mind that inner functions can have functions themselves and so on, need to make sure all the above points apply to them as well.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant