Create a cond
function that takes 2 arguments:
- An matrix in the format:
[
[firstCondition, firstCallback],
[secondCondition, secondCallback],
...
]
- Any
value
.
So that the functions works as:
- If
firstCondition(value)
is true, returnfirstCallback(value)
. - If
secondCondition(value)
is true, returnsecondCallback(value)
. - ...
- If
nthCondition(value)
is true, returnnthCallback(value)
. - Throw if no condition is true.
Example:
const x = 10
cond([
[greaterThan10, half],
[equals10, double],
[lowerThan10, triple]
], x)
// returns double(10)