brainstate.transform.ifelse

Contents

brainstate.transform.ifelse#

brainstate.transform.ifelse(conditions, branches, *operands, check_cond=True)#

Represent multi-way if/elif/else control flow.

Parameters:
  • conditions (Sequence[bool] or Array) – Sequence of mutually exclusive boolean predicates. When check_cond is True, exactly one entry must evaluate to True.

  • 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 *operands when selected.

  • *operands (Any) – Operands forwarded to the selected branch as positional arguments.

  • check_cond (bool) – Whether to verify that exactly one condition evaluates to True.

Returns:

Value produced by the branch corresponding to the active condition.

Return type:

Any

Notes

When check_cond is True, exactly one condition must evaluate to True. A common pattern is to make the final condition True to 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)