Skip to content

Instantly share code, notes, and snippets.

@ncalm
Created April 15, 2024 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncalm/b91c72d0865a99f0cf8a47cb8ff94a6e to your computer and use it in GitHub Desktop.
Save ncalm/b91c72d0865a99f0cf8a47cb8ff94a6e to your computer and use it in GitHub Desktop.
Excel LAMBDA examples of function chaining
IFOMITTED = LAMBDA(arg, then, IF(ISOMITTED(arg), then, arg));
// Functions for common mathematical operators
MULTIPLY = LAMBDA(x, y, x*y);
ADD = LAMBDA(x, y, x+y);
SUBTRACT = LAMBDA(x, y, x-y);
DIVIDE = LAMBDA(x, y, x/y);
// Apply a series of functions to an array
PIPE =LAMBDA(array, functions, operator, [init],
REDUCE(IFOMITTED(init, array), functions, LAMBDA(a, b, operator(a , b(array))))
);
// Currently arrays of functions are composed with HSTACK or VSTACK
pipe_usage_example
=PIPE(
SEQUENCE(10),
HSTACK(LAMBDA(x, MAX(x)), LAMBDA(x, SUM(x))),
MULTIPLY,
1
);
/* Typed arrays would simplify this dramatically
Additionally, allowing passage of mathematical operators
as functions (3rd arg) would be huge! */
/*
//This doesn't work!
dream_pipe_usage_example =PIPE(SEQUENCE(10), {MAX, SUM}, *, 1);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment