Forum Discussion

JackTradeOne's avatar
JackTradeOne
Copper Contributor
Mar 16, 2021
Solved

Default values for LAMBDA parameters?

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".

  • 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.

8 Replies

  • tboulden's avatar
    tboulden
    Iron Contributor
    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.

    • JackTradeOne's avatar
      JackTradeOne
      Copper Contributor
      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?
      • tboulden's avatar
        tboulden
        Iron Contributor
        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.
  • SergeiBaklan's avatar
    SergeiBaklan
    Diamond Contributor

    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.

Resources