THUNKIT

=THUNKIT(anything)

anything
numbers, text, arrays, LAMBDAs, etc.

Allows passing values by "storing" them inside a LAMBDA

tboulden

Board Regular
Joined
Jan 14, 2021
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Link to Wikipedia on thunks

Excel Formula:
=LAMBDA(x,LAMBDA(x))

Some LAMBDA helpers have limitations on what can be returned due to constraints based on the shape of the starting array. For example, if some calculation creates a series of 2-d arrays, Excel will throw an error if we try to pass those back through SCAN/BYROW/BYCOL. Thunking a value by wrapping it in a LAMBDA with no parameter "stores" a complex value in a single cell in an array, which can be passed around in calculations; we trade error-handling constructions for thunking.

I'll add some helper functions for use with thunks soon.

Cell Formulas
RangeFormula
B2:B3,B20,B17,B11:B12,B9,B5B2=FORMULATEXT(C2)
C2C2=LAMBDA(x,LAMBDA(x))("anything")
C3C3=LAMBDA(x,LAMBDA(x))("anything")()
C5C5=LET( thunkIt,LAMBDA(x,LAMBDA(x)), thunkIt(10) )
C9C9=thunkIt(10)()
C11C11=thunkIt(SEQUENCE(4,4))
C12:F15C12=thunkIt(SEQUENCE(4,4))()
C17C17=MAP(SEQUENCE(4), LAMBDA(num,CHAR(SEQUENCE(num,num,65,1))))
C20:F29C20=LET( thunks, MAP(SEQUENCE(4), LAMBDA(num, thunkIt(CHAR(SEQUENCE(num,num,65,1))))), REDUCE(,thunks, LAMBDA(stacked,thunk, vStack(IFERROR(stacked(),stacked),thunk()))) )
Dynamic array formulas.
 
Upvote 0
expandThunks - Retrieve and stack stored thunks in row-major order.
Excel Formula:
=LAMBDA(array_of_thunks,
    IF(
        AND(ROWS(array_of_thunks)=1,COLUMNS(array_of_thunks)=1),
            INDEX(array_of_thunks,1,1)(),
        LET(
            expand_rows,
                BYROW(array_of_thunks,
                    LAMBDA(row_,
                        thunkIt(
                            REDUCE(
                                ,
                                row_,
                                LAMBDA(stacked,thunk,
                                    hStack(IFERROR(stacked(),stacked),thunk())
                                )
                            )
                        )
                    )
                ),
            IF(
                ROWS(array_of_thunks)=1,
                    INDEX(expand_rows,1,1)(),
                LET(
                    stack_expanded_rows,
                        REDUCE(
                            ,
                            IF(COLUMNS(array_of_thunks)=1,array_of_thunks,expand_rows),
                            LAMBDA(stacked,thunk,
                                vStack(IFERROR(stacked(),stacked),thunk())
                            )
                        ),
                    stack_expanded_rows
                )
            )
        )
    )
)

alignExpandThunks - Adjust dimensions of thunked arrays and ensure INDEX(thunk,1,1)() lines up with others in same row and column.
Excel Formula:
=LAMBDA(array_of_thunks,
    LET(
        getDimCt,
            LAMBDA(dim,
               LAMBDA(thunk,
                   CHOOSE(dim,ROWS(thunk()),COLUMNS(thunk())))),
        row_cts,MAP(array_of_thunks,getDimCt(1)),
        col_cts,MAP(array_of_thunks,getDimCt(2)),
        max_row_ct_by_row,BYROW(row_cts,LAMBDA(row_,MAX(row_))),
        max_col_ct_by_col,BYCOL(col_cts,LAMBDA(col_,MAX(col_))),
        row_idxs,
          pairWise(max_row_ct_by_row,max_col_ct_by_col,
            LAMBDA(x,y,thunkIt(QUOTIENT(SEQUENCE(x,y)-1,y)+1))),
        col_idxs,
          pairWise(max_row_ct_by_row,max_col_ct_by_col,
            LAMBDA(x,y,thunkIt(MOD(SEQUENCE(x,y)-1,y)+1))),
        aligned,
          MAP(array_of_thunks,row_idxs,col_idxs,
            LAMBDA(thunk,row_idx,col_idx,
              thunkIt(
                IFERROR(
                  INDEX(thunk(),row_idx(),col_idx()),
                  "")))),
        expandThunks(aligned)
    )
)
LAMBDA_pairwise_thunks_summarize_pivot.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
1ABABCABABC
2DEFDEF
3ABCDABCDEABCDABCDE
4EFGHFGHIJEFGHFGHIJ
5IJKLKLMNOIJKLKLMNO
6PQRSTPQRST
7ABCDEFABCDEFGABCDEFABCDEFG
8GHIJKLHIJKLMNGHIJKLHIJKLMN
9MNOPQROPQRSTUMNOPQROPQRSTU
10STUVWXVWXYZ[\STUVWXVWXYZ[\
11YZ[\]^]^_`abcYZ[\]^]^_`abc
12defghijdefghij
expandThunks
Cell Formulas
RangeFormula
A1:M12A1=expandThunks(MAP(SEQUENCE(3,2), LAMBDA(num, thunkIt(CHAR(SEQUENCE(num,num+1,65,1))))))
O1:AA12O1=alignExpandThunks(MAP(SEQUENCE(3,2), LAMBDA(num, thunkIt(CHAR(SEQUENCE(num,num+1,65,1))))))
Dynamic array formulas.
 
I keep coming back to this page every time I'm doubting if I understand thunking in Lambda. I think it might be (finally) starting to sink in! Thanks for writing this up. It's an invaluable resource.
 
Hi tboulden,

I was wondering if this "thunkit" method can be further extrapolated to a multi-layer (more than 2 levels) thunking like LAMBDA(x,LAMBDA(x, LAMBDA(x))) and even LAMBDA(x,LAMBDA(x, LAMBDA(x, LAMBDA(x)))) etc.?

Of course we hardly encounter such complex calculations in practice. I just wanted to push it a little further and see how for example a 3-layer array thunking plays out.

Appreciate if you could shed some light and give an example if possible.

Many thanks.

Vince
 
Hi tboulden,

I was wondering if this "thunkit" method can be further extrapolated to a multi-layer (more than 2 levels) thunking like LAMBDA(x,LAMBDA(x, LAMBDA(x))) and even LAMBDA(x,LAMBDA(x, LAMBDA(x, LAMBDA(x)))) etc.?

Of course we hardly encounter such complex calculations in practice. I just wanted to push it a little further and see how for example a 3-layer array thunking plays out.

Appreciate if you could shed some light and give an example if possible.

Many thanks.

Vince
Perhaps, but I think for it to be truly useful, it will depend on the problem being addressed. What experimenting have you done? The recycling of x as the parameter means that essentially all values passed in are lost except the last one, so not sure what the specific value is in this example.
 

Forum statistics

Threads
1,224,257
Messages
6,177,473
Members
452,780
Latest member
atlam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top