cond

Takes pairs of predicates and transforms and uses the first transform that a predicate return true for.

For example in the following call:

1 cond!(
2     pred1, trsnaform1,
3     pred2, trsnaform2,
4     .
5     .
6     predN, trsnaformN,
7     default
8 )(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.

template cond(statements...)
cond
(
T
)
()

Parameters

statements

pairs of predicates and transforms followed by a default transform

value

the value to evaluate

Return Value

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

Meta