Skip to content

Instantly share code, notes, and snippets.

@ncalm
Last active January 8, 2025 13:47
Show Gist options
  • Save ncalm/ef7ed953571eec1475c291948aa2dbc3 to your computer and use it in GitHub Desktop.
Save ncalm/ef7ed953571eec1475c291948aa2dbc3 to your computer and use it in GitHub Desktop.
stacker namespace for Lambda
/*
array is a column of stuff to which we want to apply element function
row_function is some function that produces an array with a fixed number of columns
the column count produced by row_function must be identical regardless of input
stack_function is one of V or H
If you're unsure how these work or why we would use them, please review these videos:
https://youtu.be/04jOeiMypXw
https://youtu.be/wEBLT9QfQRw
*/
// These only necessary if eta-reduced native functions aren't available
V = LAMBDA(one, two, VSTACK(one, two));
H = LAMBDA(one, two, HSTACK(one, two));
STACKER = LAMBDA([stack_function],
LAMBDA(row_function,
LAMBDA(array,
LET(
_stack_function, IF(ISOMITTED(stack_function), V, stack_function),
// eta-reduced syntax:
// _stack_function, IF(ISOMITTED(stack_function), VSTACK, stack_function),
REDUCE(
row_function(TAKE(array,1)),
SEQUENCE(ROWS(array)-1,1,2),
LAMBDA(acc, curr, _stack_function(acc, row_function(INDEX(array,curr))))
)
)
)
)
);
VSTACKER = STACKER(V);
// eta-reduced syntax:
// VSTACKER = STACKER(VSTACK);
HSTACKER = STACKER(H);
// eta-reduced syntax:
// HSTACKER = STACKER(HSTACK);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment