# THUNKIT  	Allows passing values by "storing" them inside a LAMBDA



## tboulden (Nov 1, 2021)

Link to Wikipedia on thunks


```
=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.

LAMBDA_pairwise_thunks_summarize_pivot.xlsxABCDEFG12=LAMBDA(x,LAMBDA(x))("anything")#CALC!3=LAMBDA(x,LAMBDA(x))("anything")()anything45=LET(
    thunkIt,LAMBDA(x,LAMBDA(x)),
    thunkIt(10)
)#CALC!6789=thunkIt(10)()101011=thunkIt(SEQUENCE(4,4))#CALC!12=thunkIt(SEQUENCE(4,4))()123413567814910111215131415161617=MAP(SEQUENCE(4),
    LAMBDA(num,CHAR(SEQUENCE(num,num,65,1))))#CALC!181920=LET(
    thunks,
        MAP(SEQUENCE(4),
            LAMBDA(num,
                thunkIt(CHAR(SEQUENCE(num,num,65,1))))),
   REDUCE(,thunks,
       LAMBDA(stacked,thunk,
           vStack(IFERROR(stacked(),stacked),thunk())))
)A21AB22CD23ABC24DEF25GHI26ABCD27EFGH28IJKL29MNOPthunkItCell FormulasRangeFormulaB2: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.


----------



## tboulden (Nov 2, 2021)

expandThunks - Retrieve and stack stored thunks in row-major order.

```
=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.

```
=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.xlsxABCDEFGHIJKLMNOPQRSTUVWXYZAA1ABABCABABC2DEFDEF3ABCDABCDEABCDABCDE4EFGHFGHIJEFGHFGHIJ5IJKLKLMNOIJKLKLMNO6PQRSTPQRST7ABCDEFABCDEFGABCDEFABCDEFG8GHIJKLHIJKLMNGHIJKLHIJKLMN9MNOPQROPQRSTUMNOPQROPQRSTU10STUVWXVWXYZ[\STUVWXVWXYZ[\11YZ[\]^]^_`abcYZ[\]^]^_`abc12defghijdefghijexpandThunksCell FormulasRangeFormulaA1: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.


----------



## realflexyourdata (May 10, 2022)

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.


----------

