r.branch(test, true_action[, test2, test2_action, ...], false_action) → any
test.branch(true_action[, test2, test2_action, ...], false_action) → any
Perform a branching conditional equivalent to if-then-else
.
The branch
command takes 2n+1 arguments: pairs of conditional expressions and commands to be executed if the conditionals return any value but False
or None
(i.e., “truthy” values), with a final “else” command to be evaluated if all of the conditionals are False
or None
.
You may call branch
infix style on the first test. (See the second example for an illustration.)
r.branch(test1, val1, test2, val2, elseval)
is the equivalent of the Python statement
if test1:
return val1
elif test2:
return val2
else:
return elseval
Example: Test the value of x.
x = 10
r.branch((x > 5), 'big', 'small').run(conn)
> "big"
Example: As above, infix-style.
x = 10
r.expr(x > 5).branch('big', 'small').run(conn)
> "big"
Example: Categorize heroes by victory counts.
r.table('marvel').map(
r.branch(
r.row['victories'] > 100,
r.row['name'] + ' is a superhero',
r.row['victories'] > 10,
r.row['name'] + ' is a hero',
r.row['name'] + ' is very nice'
)
).run(conn)
If the documents in the table marvel
are:
[
{ "name": "Iron Man", "victories": 214 },
{ "name": "Jubilee", "victories": 49 },
{ "name": "Slava", "victories": 5 }
]
The results will be:
[
"Iron Man is a superhero",
"Jubilee is a hero",
"Slava is very nice"
]
Couldn't find what you were looking for?
Contribute: edit this page or open an issue