YAMLScript Operators
YAMLScript has a number of operators that you can use in your code.
Review YeS Expressions to see how YAMLScript supports infix operator expressions, whereas Clojure requires the operator to precede the operands.
When YAMLScript operators are used with infix notation, they often become polymorphic and do things according to the types of the operands.
!yamlscript/v0
s =: ('foo' + 'bar') # => 'foobar'
s =: (+ 'foo' 'bar') # ERROR - Clojure + only works on numbers
You can see why by looking at the ys -c
output:
$ ys -c -e "s =: ('foo' + 'bar')" -e "s =: (+ 'foo' 'bar')"
(def s (add+ "foo" "bar"))
(def s (+ "foo" "bar"))
See how the infix +
operator compiles to the add+
function? The add+
function works on numbers, strings, sequences, and mappings!
If you absolutely need the Clojure
+
function for performance reasons, you can simply use the prefix form:(+ a b)
.
Arithmetic Operators
+
- Addition - Infix works on strings, sequences, and mappings or else casts arguments to numbers.-
- Subtraction - Works on numbers.*
- Multiplication - Infix works on numbers orstr * num
,num * str
,seq * num
,num * seq
./
- Division - Works on numbers. Infix returns a double in the cases where Clojure would return a ratio.%
- Remainder - Works on numbers. Compiles torem
in Clojure.%%
- Modulus - Works on numbers. Compiles tomod
in Clojure.**
- Exponentiation - Works on numbers and has right associativity.
Comparison Operators
==
- Equal To - Works on any comparable values.!=
- Not Equal To - Works on any comparable values.>
- Greater Than - Works on numbers. Supportsa > b > c
.>=
- Greater Than or Equal To - Works on numbers.<
- Less Than - Works on numbers.<=
- Less Than or Equal To - Works on numbers.
These operators have the respective named functions: eq
, ne
, gt
, ge
, lt
, le
for use in places where a function makes more sense than an operator.
Conditional Operators
In Clojure false
and nil
are treated as "false" and everything else is treated as "true".
YAMLScript adds the concept of "truey" and "falsey" values. Empty strings, empty collections, 0
, false
, and nil
are "falsey" and everything else is "truey".
This concept applies to some operators.
&&
- Logical And||
- Logical Or&&&
- Truey And|||
- Truey Or