Share via

Strategies for managing lambda functions

Anonymous
2024-12-16T17:46:16+00:00

It has been a few years since Microsoft gave users the ability to create lambda functions. I have used them to reuse complex and/or specialized functions that I use often. They also make it easy to share these functions with others. Soon after learning to create and use lambda functions, I wanted a way to manage them.

The strategy I adopted had a few simple goals:

  1. The methodology of each function should be easy to document and be accessible to whoever uses it.
  2. The functions should be portable and easy to incorporate into any workbook.

I chose to create a workbook for each new lambda function. The workbook has a definition worksheet that includes:

Prototype Formula

The conventional way the expression would be written using the sample input data

Sample Input

A small sample to use as input for the expression 

Lambda Formula

The prototype formula incorporated into the lambda function with the sample input data

For the Name Manager

Name

The name the lambda function is given in the workbook

Refers To

The lambda formula from above (without the sample input data)

Comments

A brief description of the function 

Notes

Any notes that may be helpful or informative to understand the formula and/or approach used.

Below is a screen shot of a workbook that describes a function for calculating the distance between to points on the earth from the longitude and latitude of each location.

The Name, Refers To, and Comments sections can be pasted directly into the name manager.

To reuse or share a copy of the function with another user, just copy a worksheet from this workbook into the one where the function will be reused. Since there are times when the function definition is not needed, I add a blank worksheet to the back of the workbook. When the blank sheet is copied into another workbook, the name (lambda formula) is copied too. The blank worksheet can be deleted, and the name will remain.

For naming conventions, I have prefixed all the names with “L_” so they are easy to find when editing a workbook.

I am curious how others are managing any collections of lambdas.

Please use reply to contribute any findings or advice to the discussion.

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2024-12-17T13:21:47+00:00

    Congrats from my side, too. Important question/issue as soon as your lambdas become more complicated,

    i.e.,, longer than 1 line of code. For example, in my current project I have 2 or 3 lambdas that are 3-4 times

    larger than your example. It is quite easy to get lost in such formulas leading to syntactic errors caught by

    the lambda engine but not always with detailed enough feedback to enable you to quickly recover from

    them. So just putting the final lambda expression (including a lot of not-so-easy let's) into your document

    is not enough for future users (including yourself) who need to understand the lambda function and perhaps

    want to adapt it to their (or yours) different context. My experience with some of the add-in formula editors

    (even Microsoft's own Advanced Formula Editor) is not quite positive. In particular, they are not helpful for

    the strategy that you propose here, because they don't provide a way to export the structured lambda's.

    In my case, i.e., rather long lambda expressions with a lot of in-formula LET functions, I decided to use MS

    WORD to get the sort of structured expressions to keep track of the numerous parameters and variables in

    the lambda function. This is clearly suboptimal. I guess I will follow your advice and put all that information

    in the Excel workbooks themselves, so that those functions can be easily transferred to other workbooks

    and the power of Excel may be used to swap between structured renderings of lambda's and pure-string

    lambda's, i.e., those that will eventually be saved in the Name Manager.

    Here is an example from my project's catalogue of "large lambda's" in WORD, first the structured version

    and then the pure-string version:

    In WORD, one must apply multiple REPLACE commands to get the pure-string rendering. That could be automated

    by a single VBA script, or, who knows, by a (large) lambda function. The reverse way would be much more complicated,

    probably would still involve some improvements by hand.

    Initially, writing the structured _SYNCH function directly in WORD, one would use the tab stops, but that turns out to be

    quite clumsy. In Excel, it would be easier, using distinct columns to imitate the tabs. The "***" comments can be moved

    to another column to the right. Beware, that the same function can usually be realized in different ways. The challenge

    is, to find a "factoring" of the function such that the LET variables can be defined on a single line, and they build up one

    after the other, so that the final expression (here: SWITCH) uses a minimal number of LET variables.

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2024-12-30T16:49:15+00:00

    Paulus,

    Your first point:

    So just putting the final lambda expression (including a lot of not-so-easy let's) into your document is not enough for future users (including yourself) who need to understand the lambda function and perhaps want to adapt it to their (or yours) different context.

    does a really nice job of stating the reason for why I posted this as a discussion.

    Like you, I too recall trying the Advanced Formula Environment with a similar reaction. I have not investigated if that team has done any further development.

    Using MS Word to document expressions seems like a viable and natural way to approach the problem. I like the way you use the LET function alias the meaningful names of the inputs with more compact variables for the calculations.

    Thanks for sharing your insights.

    0 comments No comments
  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more