Created
August 15, 2024 14:09
-
-
Save ncalm/7efd4633079ab3f653984de5ca7069e7 to your computer and use it in GitHub Desktop.
These Excel LAMBDA functions partition an array into two thunked parts
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
/* | |
axis is (default) 0 for rows 1 for columns | |
array is a 2D array | |
at is a position at which to partition the array on the axis | |
So this returns two thunks, stacked vertically, the first | |
of which contains the first 5 rows of the array. The second contains the second | |
5 rows of the array. | |
PARTITION(0)(SEQUENCE(10,10), 5) | |
*/ | |
// only partitions on rows, always outputs VSTACK'd | |
PARTITION_1 = LAMBDA(array, n, | |
LET( | |
part1, TAKE(array, n), | |
part2, DROP(array, n), | |
VSTACK(LAMBDA(part1), LAMBDA(part2)) | |
) | |
); | |
// adds partitioning on columns | |
PARTITION_2 = LAMBDA(array, n, [axis], | |
LET( | |
_axis, IF(ISOMITTED(axis), 0, 1), | |
part1, IF(_axis=0, TAKE(array, n), TAKE(array,,n)), | |
part2, IF(_axis=0, DROP(array, n), DROP(array,,n)), | |
VSTACK(LAMBDA(part1), LAMBDA(part2)) | |
) | |
); | |
// Changes to HSTACK output if axis<>0 | |
PARTITION_3 = LAMBDA(array, n, [axis], | |
LET( | |
_axis, IF(ISOMITTED(axis), 0, 1), | |
part1, IF(_axis=0, TAKE(array, n), TAKE(array,,n)), | |
part2, IF(_axis=0, DROP(array, n), DROP(array,,n)), | |
IF( | |
_axis=0, | |
VSTACK(LAMBDA(part1), LAMBDA(part2)), | |
HSTACK(LAMBDA(part1), LAMBDA(part2)) | |
) | |
) | |
); | |
// creates STACK function chooser | |
PARTITION_4 = LAMBDA(array, n, [axis], | |
LET( | |
_axis, IF(ISOMITTED(axis), 0, 1), | |
part1, IF(_axis=0, TAKE(array, n), TAKE(array,,n)), | |
part2, IF(_axis=0, DROP(array, n), DROP(array,,n)), | |
stack_fn, IF(_axis=0, VSTACK, HSTACK), | |
stack_fn(LAMBDA(part1), LAMBDA(part2)) | |
) | |
); | |
// Removes stack_fn definition | |
PARTITION_5 = LAMBDA(array, n, [axis], | |
LET( | |
_axis, IF(ISOMITTED(axis), 0, 1), | |
part1, IF(_axis=0, TAKE(array, n), TAKE(array,,n)), | |
part2, IF(_axis=0, DROP(array, n), DROP(array,,n)), | |
IF(_axis=0, VSTACK, HSTACK)(LAMBDA(part1), LAMBDA(part2)) | |
) | |
); | |
// Removes _axis definition (omited defaults to zero anyway) | |
PARTITION_6 = LAMBDA(array, n, [axis], | |
LET( | |
part1, IF(axis=0, TAKE(array, n), TAKE(array,,n)), | |
part2, IF(axis=0, DROP(array, n), DROP(array,,n)), | |
IF(axis=0, VSTACK, HSTACK)(LAMBDA(part1), LAMBDA(part2)) | |
) | |
); | |
// Moves thunking to part definitions | |
PARTITION_7 = LAMBDA(array, n, [axis], | |
LET( | |
part1, LAMBDA(IF(axis=0, TAKE(array, n), TAKE(array,,n))), | |
part2, LAMBDA(IF(axis=0, DROP(array, n), DROP(array,,n))), | |
IF(axis=0, VSTACK, HSTACK)(part1, part2) | |
) | |
); | |
// Creates part_fn | |
PARTITION_8 = LAMBDA(array, n, [axis], | |
LET( | |
part_fn, LAMBDA(f, LAMBDA(IF(axis=0, f(array, n), f(array,,n)))), | |
part1, part_fn(TAKE), | |
part2, part_fn(DROP), | |
IF(axis=0, VSTACK, HSTACK)(part1, part2) | |
) | |
); | |
// Moves the part_fn calls | |
PARTITION_9 = LAMBDA(array, n, [axis], | |
LET( | |
part_fn, LAMBDA(f, LAMBDA(IF(axis=0, f(array, n), f(array,,n)))), | |
IF(axis=0, VSTACK, HSTACK)(part_fn(TAKE), part_fn(DROP)) | |
) | |
); | |
// curries the axis parameter | |
PARTITION = LAMBDA([axis], | |
LAMBDA(array, n, | |
MAP( | |
IF(axis=0,VSTACK,HSTACK)(TAKE,DROP), | |
LAMBDA(f, LAMBDA(IF(axis=0,f(array,n),f(array,,n)))) | |
) | |
) | |
); | |
// partial application of axis argument to PARTITION | |
PARTITIONROWS = PARTITION(0); | |
PARTITIONCOLS = PARTITION(1); | |
// dethunks a partition produced by PARTITION | |
GETPARTITION = LAMBDA( | |
partition_array, | |
partition_no, | |
INDEX(TOCOL(partition_array),partition_no,1)() | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment