Forum Discussion

ecovonrein's avatar
ecovonrein
Iron Contributor
Apr 29, 2023

Syntax for calling Lambda via INDIRECT

Does anyone know the syntax for calling a Lambda via INDIRECT?  I can write

 

=Calipso!audit(1000)

 

but I cannot write

 

=INDIRECT("Calipso!audit")(1000)

 

nor

 

=INDIRECT("Calipso!audit(1000)")

 

Any pointers?  Thanks!

  • ecovonrein 

    Afraid that doesn't work, as with any other named functions. As variant you may create for the set of you functions something like

    one = LAMBDA(x, x+1);
    two = LAMBDA(x, x+2);
    
    callFunction=LAMBDA(str,
        SWITCH( str,
            "one", one,
            "two", two)
    );

    and call it as

    • ecovonrein's avatar
      ecovonrein
      Iron Contributor
      Thanks. Not great. I was surprised that I can define

      audit=Calipso!audit

      and call

      =audit(1000)

      Extrapolating from there, I expected

      =INDIRECT("Calipdo!audit")(1000)

      to work.

      Your SWITCH does not work for me because the list of possible targets is non-exhaustive, as is the places from where the call might come.

      Well, there's hoping that MS will get around to fixing it at some point. (It isn't core functionality for me at this mo.)
      • JosWoolley's avatar
        JosWoolley
        Iron Contributor

        ecovonrein 

         

        You won't be able to achieve what you want using INDIRECT, though if a macro-enabled workbook is an option then perhaps you can find a workaround using EVALUATE.

         

        For example, with cell A1 containing the defined name pertaining to the LAMBDA, a further definition within Named Manager, Eval, say, as:

         

        =LAMBDA(ζ,EVALUATE($A$1&"("&ζ&")"))

         

        after which you can call, for example:

         

        =Eval(1000)

         

        within the worksheet.

         

  • ecovonrein 

    Since I am not sure I fully understand the use case for INDIRECT, here are some techniques that might be of relevance.  I started with a validation list of function names as text.  That allows me to use validation to select the desired function names.  I then built an array of functions in memory

    FunctionList = VSTACK(SINλ, COSλ, TANλ)
    
    where
    SINλ = LAMBDA(ϑ, SIN(RADIANS(ϑ)));
    COSλ = LAMBDA(ϑ, COS(RADIANS(ϑ)));
    TANλ = LAMBDA(ϑ, TAN(RADIANS(ϑ)));

    The worksheet formula could then be

    = LET(
        Fnλ, XLOOKUP(function, validationList, FunctionList),
        MAP(Fnλ, ϑ, Applyλ)
      )
    
    where
    Applyλ = LAMBDA(Fnλ₁, ϑ₁, Fnλ₁(ϑ₁))

    If you find having formulas on the worksheet distracting or ugly, you could wrap the whole shooting-match within a further Lambda to give

    = Indirectλ(function, ϑ)

    • ecovonrein's avatar
      ecovonrein
      Iron Contributor

      This is essentially the same approach that Sergei takes with SWITCH.  It does not work when the target Lambdas are in a foreign Workbook.

       

      I have wondered whether the challenge of poking numbers out of a time grid might be a suitable matter for Power Query.  (I don't know this thing, really.)  The grid is highly readable to a human so I would certainly expect the Copilot to be able to do it.  Not that I would know where to start 🙂🙂...

      • PeterBartholomew1's avatar
        PeterBartholomew1
        Silver Contributor

        ecovonrein 

        I am more used to using a scratch worksheet in the same book for testing/purposes so I have little experience of the scenario you outline.  At least I now have some idea as to how the sheet local names arise for your Lambda functions.  I tend to use workbook scoped Lambdas and keep the sheet localisation to the parameter names rather than the function, but I appreciate that may not be possible.

  • ecovonrein 

    I think I am beginning to lose the thread here.  What is the intended purpose of the indirect?

    If the top level formula is a Lambda function, one can pass any other function as a Lambda variable and apply it to such parameter string as one chooses within the inner formulae.  That would be cleaner conceptually than identifying the Lambda as a text string and using INDIRECT.

Share

Resources