brainstate.transform.ifelse#
- brainstate.transform.ifelse(conditions, branches, *operands, check_cond=True)#
Represent multi-way
if/elif/elsecontrol flow.- Parameters:
conditions (Sequence[bool] or Array) – Sequence of mutually exclusive boolean predicates. When
check_condisTrue, exactly one entry must evaluate toTrue.branches (Sequence[Callable]) – Sequence of branch callables evaluated lazily. Must have the same length as
conditions, contain at least two callables, and each branch receives*operandswhen selected.*operands (Any) – Operands forwarded to the selected branch as positional arguments.
check_cond (
bool) – Whether to verify that exactly one condition evaluates toTrue.
- Returns:
Value produced by the branch corresponding to the active condition.
- Return type:
Any
Notes
When
check_condisTrue, exactly one condition must evaluate toTrue. A common pattern is to make the final conditionTrueto encode a default branch.Examples
>>> import brainstate >>> >>> def describe(a): ... return brainstate.transform.ifelse( ... conditions=[a > 5, a > 0, True], ... branches=[ ... lambda: "greater than five", ... lambda: "positive", ... lambda: "non-positive", ... ], ... ) >>> >>> describe(7) >>> describe(-1)