Created
December 18, 2024 13:34
-
-
Save snth/5ca415b03f3d1559e0f3e24436d08088 to your computer and use it in GitHub Desktop.
Short Circuiting AND⚡, OR⚡, and IFS⚡ LAMBDA functions for Excel
This file contains hidden or 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
/* | |
AND⚡ (Short-circuiting AND) | |
A performance-optimized version of AND that short-circuits evaluation using CHOOSE, | |
stopping as soon as it encounters FALSE. | |
Inputs: | |
- logical1: First logical test (required) | |
- logical2-logical9: Additional logical tests (optional) | |
Return: | |
TRUE if all provided arguments are TRUE | |
FALSE if any argument is FALSE (stops evaluating remaining arguments) | |
Key difference from AND(): | |
Built-in AND(FALSE, NA()) returns #N/A because it evaluates all arguments | |
AND⚡(FALSE, NA()) returns FALSE because it stops at the first FALSE | |
Examples: | |
=AND⚡(A1>0, B1>0) ' TRUE if both cells are positive | |
=AND⚡(FALSE, NA()) ' Returns FALSE, avoiding the #N/A error | |
=AND⚡(TRUE, 1=1, 5>3) ' Returns TRUE | |
=AND⚡(A1:A10>0) ' Not for arrays - use regular AND() | |
Performance note: | |
Unlike built-in AND(), this function uses CHOOSE for short-circuiting, | |
avoiding evaluation of subsequent arguments once FALSE is encountered. | |
*/ | |
AND⚡ = LAMBDA(logical1, [logical2], [logical3], [logical4], [logical5], [logical6], [logical7], [logical8], [logical9], | |
LET( | |
_cond, LAMBDA(i, | |
CHOOSE(i, logical1, logical2, logical3, logical4, logical5, logical6, logical7, logical8, logical9) | |
), | |
_and, LAMBDA(i, _rec, | |
IF(NOT(_cond(i)), FALSE, IF(ISOMITTED(_cond(i+1)), TRUE, _rec(i+1, _rec))) | |
), | |
_and(1, _and) | |
)); | |
This file contains hidden or 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
/* | |
IFS⚡ (Short-circuiting IFS) | |
A performance-optimized version of IFS that short-circuits evaluation using CHOOSE, | |
stopping once a TRUE condition is found. | |
Inputs: | |
- logical_test1: First condition to test (required) | |
- value_if_true1: Value to return if logical_test1 is TRUE (required) | |
- logical_test2-9, value_if_true2-9: Additional condition/value pairs (optional) | |
Return: | |
The first value_if_true where its corresponding logical_test is TRUE | |
#N/A if no conditions are TRUE | |
Examples: | |
=IFS⚡(A1>90,"A", A1>80,"B", A1>70,"C", TRUE,"F") ' Grade calculator | |
=IFS⚡(ISTEXT(A1),A1, TRUE,0) ' Text or zero | |
=IFS⚡(A1="Error",NA(), 1/A1>0.5,TRUE, TRUE,FALSE) ' Safe division check | |
Performance note: | |
Unlike built-in IFS(), this function uses CHOOSE for short-circuiting, | |
avoiding evaluation of subsequent test/value pairs once a TRUE condition is found. | |
*/ | |
IFS⚡ = LAMBDA(logical_test1, value_if_true1, [logical_test2], [value_if_true2], [logical_test3], [value_if_true3], [logical_test4], [value_if_true4], [logical_test5], [value_if_true5], [logical_test6], [value_if_true6], [logical_test7], [value_if_true7], [logical_test8], [value_if_true8], [logical_test9], [value_if_true9], | |
LET( | |
_cond, LAMBDA(i, | |
CHOOSE(i, logical_test1, logical_test2, logical_test3, logical_test4, logical_test5, logical_test6, logical_test7, logical_test8, logical_test9) | |
), | |
_val, LAMBDA(i, | |
CHOOSE(i, value_if_true1, value_if_true2, value_if_true3, value_if_true4, value_if_true5, value_if_true6, value_if_true7, value_if_true8, value_if_true9) | |
), | |
_ifs, LAMBDA(i, _rec, | |
IF(_cond(i), _val(i), IF(ISOMITTED(_cond(i+1)), NA(), _rec(i+1, _rec))) | |
), | |
_ifs(1, _ifs) | |
)); |
This file contains hidden or 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
/* | |
OR⚡ (Short-circuiting OR) | |
A performance-optimized version of OR that short-circuits evaluation using CHOOSE, | |
stopping as soon as it encounters TRUE. | |
Inputs: | |
- logical1: First logical test (required) | |
- logical2-logical9: Additional logical tests (optional) | |
Return: | |
TRUE if any provided argument is TRUE (stops evaluating remaining arguments) | |
FALSE if all arguments are FALSE | |
Examples: | |
=OR⚡(A1="Yes", A1="Y") ' TRUE if cell contains either value | |
=OR⚡(TRUE, NA()) ' Returns TRUE, avoiding the #N/A error | |
=OR⚡(FALSE, FALSE, 5>3) ' Returns TRUE | |
=OR⚡(A1:A10=0) ' Not for arrays - use regular OR() | |
Performance note: | |
Unlike built-in OR(), this function uses CHOOSE for short-circuiting, | |
avoiding evaluation of subsequent arguments once TRUE is encountered. | |
*/ | |
OR⚡ = LAMBDA(logical1, [logical2], [logical3], [logical4], [logical5], [logical6], [logical7], [logical8], [logical9], | |
LET( | |
_cond, LAMBDA(i, | |
CHOOSE(i, logical1, logical2, logical3, logical4, logical5, logical6, logical7, logical8, logical9) | |
), | |
_or, LAMBDA(i, _rec, | |
IF(_cond(i), TRUE, IF(ISOMITTED(_cond(i+1)), FALSE, _rec(i+1, _rec))) | |
), | |
_or(1, _or) | |
)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment