SOLVED

Default values for LAMBDA parameters?

Copper Contributor

Is it possible to somehow include default values for LAMBDA functions with multiple parameters? For example, if my function is 

=LAMBDA(
height,
width,
height * width
);

I would like to be able have "height" default to, say, "2" so I can call it with "4" and have it return "8".

8 Replies
best response confirmed by JackTradeOne (Copper Contributor)
Solution

@JackTradeOne 

If only as

=LAMBDA(height,width,
IF(height, height,2)* width
)(,4)

Lambda allows optional parameters if you have more than one of them. Missed parameters are returned as zero.

Alternatively, you could approach it like so:
Area:=LAMBDA(dim_1,LAMBDA(dim_2,dim_1*dim_2))
Then partially apply the 2, you get:
=Area(2) => =LAMBDA(dim_2,2*dim_2)

And so =Area(2)(4) => LAMBDA(dim_2,2*dim_2)(4) => 2*4 = 8

Depending on your context, having this set up for partial application may be more flexible.

I'm not sure I understand your concept of "partially apply". The desired end result is a function (say, "Area") which I can call once as Area(,4) and get "8" in return. Does you approach achieve that?
Apologies, partial application is a concept from functional programming which LAMBDAs lend themselves to. Not to go to far afield, you can google it if you like.

So we have Area defined as above in Name Manager; let's define Area2:=Area(2). Now when you do Area2(4), you'll get 8. If you needed to hardcode some other value, say 4, you could define Area4:=Area(4), and Area4(2)=8.

@tboulden 

First, thanks for the explanation of partial application. Now that you describe it, I remember running into this concept before, though under a different name. I will definitely revisit that.

In terms of the specific answer - IIUC, I would need to define, in addition to your Area2, another Area function to handle situations where both parameters are present, correct? What I'm trying to achieve is one function which can accept either Area(2,4) or Area(?,4) and, in either case, return 8.

Sergei's suggestion will serve you well for those specifications then, I was proffering the suggestion in case your use case might be more open-ended.
Got it! Thanks again.
@JackTradeOne you can use the function =ISOMITTED() to change your lambda according to this need.
1 best response

Accepted Solutions
best response confirmed by JackTradeOne (Copper Contributor)
Solution

@JackTradeOne 

If only as

=LAMBDA(height,width,
IF(height, height,2)* width
)(,4)

Lambda allows optional parameters if you have more than one of them. Missed parameters are returned as zero.

View solution in original post