...
Two special comparison operators are useful with the sort() operator: <=>
and cmp
. These operators return -1 if the first operand is less than the second, 0, if they're equal, and 1 if the first operand is greater than the second.The The <=>
operator is used with numbers and cmp
is used with strings.
Code Block | ||||
---|---|---|---|---|
| ||||
x = 5; y = 6; x <=> y // returns -1 x <=> x // returns 0 y <=> x // returns 1 p = "aab"; q = "abb"; ap cmp bq // returns -1 ap cmp ap // returns 0 bq cmp ap // returns 1 |
Like
Like
takes a regular expression as its second argument and returns true if it matches the string given as its first argument. Arguments to these operators can be any valid expression.
...
Code Block | ||||
---|---|---|---|---|
| ||||
not a a && b a || b (a || b ) && not c |
The operators &&
and ||
use short-circuit evaluation semantics.
Code Block |
---|
val = arg || default |
In the example code shown above, the name val
is bound to the value of arg
provided it is considered to be "true" and otherwise binds it to the value of default
. This is similar to, but more general than, the universal operator defaultsTo
except that the latter only applies when the value it is operating on is specifically null
.