braincore.transform.ifelse

Contents

braincore.transform.ifelse#

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

If-else control flows looks like native Pythonic programming.

Examples

>>> import braincore as bc
>>> def f(a):
>>>    return bc.transform.ifelse(conditions=[a > 10, a > 5, a > 2, a > 0],
>>>                               branches=[lambda: 1,
>>>                                         lambda: 2,
>>>                                         lambda: 3,
>>>                                         lambda: 4,
>>>                                         lambda: 5])
>>> f(1)
4
>>> f(0)
5
Parameters:
  • conditions (bool, sequence of bool, Array) – The boolean conditions.

  • branches (Any) – The branches, at least has two elements. Elements can be functions, arrays, or numbers. The number of branches and conditions has the relationship of len(branches) == len(conditions) + 1. Each branch should receive one arguement for operands.

  • *operands (optional, Any) – The operands for each branch.

  • check_cond (bool) – Whether to check the conditions. Default is True.

Returns:

res – The results of the control flow.

Return type:

Any