Skip to content

Instantly share code, notes, and snippets.

@ncalm
Created August 8, 2024 14:37
Show Gist options
  • Save ncalm/b6a20c0e1cb825fd6a636725035dcbbe to your computer and use it in GitHub Desktop.
Save ncalm/b6a20c0e1cb825fd6a636725035dcbbe to your computer and use it in GitHub Desktop.
This gist contains examples of why thunking can be useful
// This takes several seconds to evaluate
huge_array = MAKEARRAY(10000,5000,PRODUCT);
// By putting the array in a thunk, we can choose not to evaluate it
thunked_huge_array = LET(my_thunk, LAMBDA(MAKEARRAY(10000,5000,PRODUCT)),my_thunk);
// We can evaluate it by putting () at the end of the LET function's return value
evaluated_on_LET_return = LET(my_thunk, LAMBDA(MAKEARRAY(10000,5000,PRODUCT)),my_thunk());
// Or just putting () outside the LET function
evaluated_on_LET_result = LET(my_thunk, LAMBDA(MAKEARRAY(10000,5000,PRODUCT)),my_thunk)();
// This will evaluate the array even though it isn't used
LET_is_not_lazily_evaluated = LET(
huge_array, MAKEARRAY(10000,5000,PRODUCT),
"I don't want that array"
);
// IF only evaluates either 'then' OR 'else', not both
deciding_with_IF = LET(
my_thunk, LAMBDA(MAKEARRAY(10000,5000,PRODUCT)),
IF(Sheet1!A1="Do it!", my_thunk(), "Didn't do it")
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment