Template cond
Takes pairs of predicates and transforms and uses the first transform that a predicate return true for.
template cond(statements...)
;
For example in the following call:
cond!(
pred1, trsnaform1,
pred2, trsnaform2,
.
.
predN, trsnaformN,
default
)(value);
predN
can either be a unary function or an expression. transformN
can be a unary,
or binary function, or an expression. The unary function cannot be a string since
narrow strings will be treated as values. default
is treated as a transform.
All transforms must return a common type, or void. If there's a common return then a default transform must be provided. If all the transforms return void then a default is not needed.
Contained Functions
Name | Description |
---|---|
cond |
Parameters
Name | Description |
---|---|
statements | pairs of predicates and transforms followed by a default transform |
value | the value to evaluate |
Returns
Whatever is returned by the result that wins
Benchmarks
A sample cond
if/else chain was used with a mixture of expressions and unary functions and
iterated over. A couple of hand written if/else chains were compared. The first used lambdas
to evaluate their conditions, the second just used inline code.
Benchmarking cond against hand written if/elses
cond: 2 hnsecs
hand written 1: 0 hnsecs
hand written 2: 0 hnsecs