logic_formulas¶
shortfx.fxExcel.logic_formulas
¶
Excel Logic Functions Module.
This module provides Excel-compatible logical and conditional functions for shortfx. Functions include: - Basic Logic: AND, OR, NOT, XOR, TRUE, FALSE - Conditionals: IF, IFS, IFERROR, IFNA - Lambda Functions: LAMBDA, LET, BYCOL, BYROW, REDUCE, SCAN
All functions follow Excel naming conventions and behavior.
Functions¶
AND(*args: Any) -> bool
¶
Return TRUE if all arguments are TRUE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Logical values to evaluate. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if all arguments are True, False otherwise. |
Example
AND(True, True, True) True AND(True, False, True) False
Cost: O(n) where n is the number of arguments
Source code in shortfx/fxExcel/logic_formulas.py
BYCOL(array: List[List[Any]], lambda_func: Callable[[List[Any]], Any]) -> List[Any]
¶
Apply a LAMBDA to each column and return an array of results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
List[List[Any]]
|
List of lists (matrix) where each sublist is a row. |
required |
lambda_func
|
Callable[[List[Any]], Any]
|
Lambda function that takes a list (column) as argument. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
List[Any]: Results of applying lambda_func to each column. |
Example
BYCOL([[1, 2], [3, 4]], lambda col: sum(col)) [4, 6]
Cost: O(r * c) where r is rows and c is columns
Source code in shortfx/fxExcel/logic_formulas.py
BYROW(array: List[List[Any]], lambda_func: Callable[[List[Any]], Any]) -> List[Any]
¶
Apply a LAMBDA to each row and return an array of results.
Excel: BYROW / Spanish: BYROW
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
List[List[Any]]
|
List of lists (matrix) where each sublist is a row. |
required |
lambda_func
|
Callable[[List[Any]], Any]
|
Lambda function that takes a list (row) as argument. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
List[Any]: Results of applying lambda_func to each row. |
Example
BYROW([[1, 2, 3], [4, 5, 6]], lambda row: sum(row)) [6, 15]
Cost: O(r) where r is the number of rows
Source code in shortfx/fxExcel/logic_formulas.py
FALSE() -> bool
¶
Return the logical value FALSE.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
False |
Example
FALSE() False
Cost: O(1)
IF(logical_test: Any, value_if_true: Any, value_if_false: Any = None) -> Any
¶
Perform a logical test and return one value for TRUE and another for FALSE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logical_test
|
Any
|
Condition to evaluate. |
required |
value_if_true
|
Any
|
Value to return if condition is True. |
required |
value_if_false
|
Any
|
Value to return if condition is False (optional). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
value_if_true if test is True, otherwise value_if_false. |
Example
IF(10 > 5, "Yes", "No") 'Yes' IF(3 < 2, "Yes", "No") 'No'
Cost: O(1)
Source code in shortfx/fxExcel/logic_formulas.py
IFERROR(value: Union[Callable, Any], value_if_error: Any) -> Any
¶
Return a value if an expression results in an error, otherwise return the expression result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Union[Callable, Any]
|
Expression to evaluate (callable) or direct value. |
required |
value_if_error
|
Any
|
Value to return if error occurs. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result of expression or value_if_error if error occurs. |
Example
IFERROR(lambda: 10 / 2, "Error") 5.0 IFERROR(lambda: 10 / 0, "Error") 'Error'
Cost: O(1)
Source code in shortfx/fxExcel/logic_formulas.py
IFNA(value: Union[Callable, Any], value_if_na: Any) -> Any
¶
Return a value if the expression results in #N/A or None, otherwise return the expression result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Union[Callable, Any]
|
Expression to evaluate (callable) or direct value. |
required |
value_if_na
|
Any
|
Value to return if result is None or #N/A. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result of expression or value_if_na if result is None. |
Example
IFNA(lambda: None, "N/A") 'N/A' IFNA(lambda: 42, "N/A") 42
Cost: O(1)
Source code in shortfx/fxExcel/logic_formulas.py
IFS(*args: Any) -> Any
¶
Check multiple conditions and return the value corresponding to the first TRUE condition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Pairs of condition and value (cond1, val1, cond2, val2, ..., default). |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Value corresponding to the first True condition, or default value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If number of arguments is even (missing default value). |
Example
IFS(False, "A", True, "B", False, "C", "Default") 'B' IFS(False, "A", False, "B", "Default") 'Default'
Cost: O(n) where n is the number of condition pairs
Source code in shortfx/fxExcel/logic_formulas.py
LAMBDA(*args: Any, expression: Optional[Callable] = None) -> Callable
¶
Create a reusable lambda function with named parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Parameter names for the lambda. |
()
|
expression
|
Optional[Callable]
|
Lambda expression as a callable function. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Lambda function that can be called with specified arguments. |
Example
add = LAMBDA(expression=lambda x, y: x + y) add(5, 3) 8
Cost: O(1)
Source code in shortfx/fxExcel/logic_formulas.py
LET(**kwargs: Any) -> Any
¶
Assign names to calculation results and evaluate a final expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Name-value pairs where the last pair must be 'expression' with the function to evaluate. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result of the final expression using the assigned names. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If 'expression' argument is missing. |
Example
LET(x=5, y=3, expression=lambda x, y: x + y) 8
Cost: O(1)
Source code in shortfx/fxExcel/logic_formulas.py
MAKEARRAY(rows: int, cols: int, lambda_func: Callable[[int, int], Any]) -> List[List[Any]]
¶
Return a calculated array of a specified row and column size by applying a LAMBDA.
Excel: MAKEARRAY / Spanish: ARCHIVOMAKEARRAY
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
int
|
Number of rows in the array. |
required |
cols
|
int
|
Number of columns in the array. |
required |
lambda_func
|
Callable[[int, int], Any]
|
Lambda function that takes (row_index, col_index) and returns a value. Indices are 1-based (Excel convention). |
required |
Returns:
| Type | Description |
|---|---|
List[List[Any]]
|
List[List[Any]]: Generated array with values from lambda_func. |
Example
MAKEARRAY(2, 3, lambda r, c: r * c) [[1, 2, 3], [2, 4, 6]] MAKEARRAY(3, 2, lambda r, c: r + c) [[2, 3], [3, 4], [4, 5]]
Cost: O(r * c) where r is rows and c is columns
Source code in shortfx/fxExcel/logic_formulas.py
MAP(*args) -> List[List[Any]]
¶
Return an array formed by mapping each value in arrays to a new value by applying a LAMBDA.
Excel: MAP / Spanish: MAPEAR
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
One or more arrays (matrices) followed by the lambda function. The last argument must be the lambda function. |
()
|
Returns:
| Type | Description |
|---|---|
List[List[Any]]
|
List[List[Any]]: New array with lambda_func applied to each set of values. |
Example
MAP([[1, 2], [3, 4]], lambda x: x * 2) [[2, 4], [6, 8]] MAP([[1, 2], [3, 4]], [[5, 6], [7, 8]], lambda x, y: x + y) [[6, 8], [10, 12]]
Cost: O(r * c) where r is rows and c is columns
Source code in shortfx/fxExcel/logic_formulas.py
NOT(value: Any) -> bool
¶
Reverse the logical value of its argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Logical value to invert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Inverted logical value. |
Example
NOT(True) False NOT(False) True
Cost: O(1)
Source code in shortfx/fxExcel/logic_formulas.py
OR(*args: Any) -> bool
¶
Return TRUE if any argument is TRUE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Logical values to evaluate. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if any argument is True, False otherwise. |
Example
OR(False, True, False) True OR(False, False, False) False
Cost: O(n) where n is the number of arguments
Source code in shortfx/fxExcel/logic_formulas.py
REDUCE(initial_value: Any, array: List[Any], lambda_func: Callable[[Any, Any], Any]) -> Any
¶
Reduce an array to an accumulated value by applying a LAMBDA to each value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_value
|
Any
|
The starting value for the accumulator. |
required |
array
|
List[Any]
|
Array of values to reduce. |
required |
lambda_func
|
Callable[[Any, Any], Any]
|
Function taking (accumulator, current_value) and returning new accumulator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The final accumulated value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If lambda_func is not callable. |
Example
REDUCE(0, [1, 2, 3, 4, 5], lambda acc, x: acc + x) 15 REDUCE(1, [1, 2, 3, 4], lambda acc, x: acc * x) 24
Cost: O(n) where n is the number of elements
Source code in shortfx/fxExcel/logic_formulas.py
SCAN(initial_value: Any, array: List[Any], lambda_func: Callable[[Any, Any], Any]) -> List[Any]
¶
Scan an array by applying a LAMBDA to each value, returning intermediate results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_value
|
Any
|
The starting value for the accumulator. |
required |
array
|
List[Any]
|
Array of values to scan. |
required |
lambda_func
|
Callable[[Any, Any], Any]
|
Function taking (accumulator, current_value) and returning new accumulator. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
List[Any]: Array of each intermediate accumulated value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If lambda_func is not callable. |
Example
SCAN(0, [1, 2, 3, 4, 5], lambda acc, x: acc + x) [1, 3, 6, 10, 15] SCAN(1, [2, 3, 4], lambda acc, x: acc * x) [2, 6, 24]
Cost: O(n) where n is the number of elements
Source code in shortfx/fxExcel/logic_formulas.py
SWITCH(expression: Any, *args: Any) -> Any
¶
Evaluate an expression and return a value from a list based on matching values.
Excel: SWITCH / Spanish: CAMBIAR
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
Any
|
The value to compare against. |
required |
*args
|
Any
|
Alternating value/result pairs, with optional default at the end. Format: value1, result1, value2, result2, ..., [default] |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result corresponding to the first matching value, or default. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no match found and no default provided. |
Example
SWITCH(2, 1, "one", 2, "two", 3, "three") 'two' SWITCH(5, 1, "one", 2, "two", "other") 'other'
Cost: O(n) where n is the number of value/result pairs
Source code in shortfx/fxExcel/logic_formulas.py
TRUE() -> bool
¶
Return the logical value TRUE.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True |
Example
TRUE() True
Cost: O(1)
XOR(*args: Any) -> bool
¶
Return a logical exclusive OR of all arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Logical values to evaluate. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if an odd number of arguments are True, False otherwise. |
Example
XOR(True, False, False) True XOR(True, True, False) False
Cost: O(n) where n is the number of arguments