Forum Discussion
tboulden
Feb 13, 2021Iron Contributor
Recursive Lambda: Unpacking LET function
RecentlyTwifoo posted the FIFO Inventory challenge, and PeterBartholomew1 ventured "shouldn't take lori_m more than a few minutes to write a recursive Python script to reduce any LET function back to...
TTodorov
Apr 06, 2024Copper Contributor
rachel
In fact there is no need to split with |.
My table have 2 columns:
In sample table, when I meet "For all" in second column, need to replace it with another list.
SergeiBaklan
Apr 13, 2024Diamond Contributor
As variant for the sample table
=LET(
splitter, LAMBDA(pair,
LET(
a, TEXTSPLIT(INDEX(pair,1,1),,", "),
b, TEXTSPLIT(INDEX(pair,1,2),, ", "),
LAMBDA(HSTACK(
TOCOL( IF(SEQUENCE(,ROWS(b) ), a ) ),
TOCOL( TRANSPOSE( IF(SEQUENCE(,ROWS(a) ), b ) ) )
) )
)
),
unpacker,
LAMBDA(arrayOfThunks,
LET(
n, ROWS(arrayOfThunks),
first, INDEX(arrayOfThunks, 1, 1)(),
rest, INDEX(arrayOfThunks, SEQUENCE(n - 1, , 2), 1),
IF(
n = 1,
first,
REDUCE(first, rest, LAMBDA(a,v, VSTACK(a, v())))
)
)
),
one, LAMBDA(v, INDEX(v,1,1)),
two, LAMBDA(v, INDEX(v,1,2)),
getAll, LAMBDA(v,
TEXTJOIN(", ",,
FILTER(
ObjetsPositions4[Object],
ObjetsPositions4[Position] = VALUE(v)
)
)
),
rawSplit, unpacker( BYROW(SampleTable5, splitter )),
sortPositions, SORT(ObjetsPositions4, {1,2} ),
setNine, LAMBDA(pair,
LAMBDA(
HSTACK(
IF(one(pair)="99",
INDEX(
CHOOSECOLS(sortPositions,2),
XMATCH( VALUE(two(pair)),
CHOOSECOLS(sortPositions,1),
0,
-1
)
),
one(pair)
),
two(pair)
)
)
),
getNine, unpacker( BYROW(rawSplit, setNine )),
setAll, LAMBDA(pair,
LAMBDA(
HSTACK( one(pair),
IF(two(pair)="For all",
getAll(one(pair)),
two(pair)
)
)
)
),
withAll, unpacker( BYROW(getNine, setAll ) ),
VSTACK(
{"Position","Object"},
unpacker( BYROW(withAll, splitter ) )
)
)