Last active
October 1, 2023 09:41
-
-
Save lhem/9e22b7c6bf71197a976c84637fda119d to your computer and use it in GitHub Desktop.
fast stacking arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
An extension of the BYROW function that efficiently stacks arrays of results | |
Background: | |
An example of a fold that generates integers from 1 to 2^n is, | |
=REDUCE(1,{0,1,2},LAMBDA(j,i,VSTACK(j,j+2^i))) | |
A tree fold replaces the integer arrays with list functions j -> a(j), | |
=REDUCE(LAMBDA(j,j),{0,1,2},LAMBDA(a,i,LAMBDA(j,VSTACK(a(j),a(j+2^i)))))(1) | |
| | |
/ \ | |
/ \ / \ | |
/\ /\ /\ /\ | |
12 34 56 78 | |
from this we may generalise to arbitrary array and function parameters, | |
*/ | |
FSTACK | |
=LAMBDA(array, function, | |
LET( | |
n, ROWS(array), | |
k, CEILING(LOG(n, 2), 1), | |
REDUCE( | |
LAMBDA(r, function(CHOOSEROWS(array, r))), | |
SEQUENCE(k, , 0), | |
LAMBDA(a, i, | |
LAMBDA(j, | |
IF( | |
j + 2 ^ i <= n, | |
VSTACK(a(j), a(j + 2 ^ i)), | |
a(j) | |
) | |
) | |
) | |
)(1) | |
) | |
); | |
// e.g. returns a list of all ticket names and numbers given tblTicket as below: | |
listTickets | |
=FSTACK( | |
tblTicket, | |
LAMBDA(r, | |
IF( | |
{1, 0}, | |
INDEX(r, 1), | |
SEQUENCE(INDEX(r, 2), 1, TEXTBEFORE(INDEX(r, 3), "-")) | |
) | |
) | |
); | |
tblTicket= | |
{ | |
"Jeff",10,"1001-1010"; | |
"Yury",5,"2333-2337"; | |
"Eric",6,"2422-2427"; | |
"Marsha",2,"2999-3000"; | |
"Jackie",1,"3001-3001"; | |
"Matt",12,"3002-3013" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment