microsoft / advanced-formula-environment

Create, edit, and reuse formulas in Excel
https://aka.ms/get-afe
MIT License
113 stars 11 forks source link

When importing a Gist into a new namespace, recursive functions break #14

Closed cjhesse closed 2 years ago

cjhesse commented 2 years ago

Importing a recursive LAMBDA function into a new namespace breaks its self-reference. The same is true if the function references another being imported.

I wrote a LAMBDA function to translate a string into Morse code:

Morse.Translate = LAMBDA(str, [idx],
    LET(
        idx, MAX(1, idx),
        IF(
            idx > LEN(str),
            "",
            CONCAT(
                IFERROR(
                    XLOOKUP(MID(str, idx, 1), Morse.Letters, Morse.Code),
                    "?"
                ),
                IF(idx = LEN(str), "", " "),
                Morse.Translate(str, idx + 1)
            )
        )
    )
);

If I import this function from Gist and assign it to a new namespace, say TestName, it becomes TestName.Morse.Translate, and the references to Morse.Letters, Morse.Code, and itself (Morse.Translate) are all broken, resulting in a #NAME? error.

Morse.Letters is an array of A-Z and 0-9 and Morse.Code is the corresponding translation. Morse.Translate uses recursion to loop through each character of the input string to perform the conversion.

cjhesse commented 2 years ago

I'm not sure what I did the first time, but I no longer see this behavior. It appears to be handling calling other functions or recursive calls to itself just fine. It may have been another error on my part. Closing.