Operators¶
Note
This page is under construction.
Besides the is operator, both arguments to any binary (two-input) operator must be of the same type, or one type must be able to implicitly convert to the other. See Conversions for more details. Some additional rules apply for operators:
- For the binary operators
+,-,|,^,<<,>>>, and>>, the shorter vector is extended and the result is the length of the longer vector. - For the binary operators
*,/,%,**, and&, the longer vector is truncated and the result is the length of the shorter vector. (This is because extending the shorter vector with0would not produce useful output.) - Arguments to a boolean operator
- Integers will implicitly convert to vectors of any length. See [Integer to vector conversion](#integer-to-vector-conversion).
- Cells will implicitly convert to cell filters. See [Cell to cell filter conversion](#cell-to-cell-filter-conversion).
- Vectors, cells, and patterns will implicitly convert to booleans. (Note that integers are already booleans.)
- A vector is truthy if any component is nonzero.
- A cell is truthy if it is not
#0. - A pattern is truthy if any cell is nonzero.
Arithmetic operators¶
Given two values a and b:
a + b— Additiona - b— Subtractiona * b— Multiplicationa / b— Division (rounds toward zero)a % b— Remaindera ** b— Exponentiation-a— Negation+a— No operation
NOTE: In the future, the behavior of / and % with negative numbers may be changed to emulate floored or euclidean division/modulo.
All of these operations can be applied to integers and vectors. Vector operations are applied componentwise.
If an operation is applied to two vectors of different lengths, one vector is cast to the length of the other before the operation is applied. (See Vector to vector conversion.)
- For
+and-, the shorter vector is cast to the length of the longer one - For
*,/,%, and**, the longer vector is cast to the length of the shorter one
If an operation is applied to a vector and an integer, in either order, then the integer is converted to a vector of the same length before the operation is applied. (See Integer to vector conversion.)
Overflow, underflow, division by zero, or exponentiation with a negative power cause an error. (See Errors.)
Examples¶
2 ** 8→256[1, 2] + [10, 20, 30]→[11, 22, 30][1, 2, 3] * [1, 2]→[1, 4]12 / [2, 3, 4, 5]→[6, 4, 3, 2][4] % [2, 0, 1]→[0]4 % [2, 0, 1]causes a division-by-zero error
Bitwise operators¶
Given two values a and b:
a & b— Bitwise ANDa | b— Bitwise ORa ^ b— Bitwise XORa >> b— Bitshift right (arithmetic/signed)a >>> b— Bitshift right (logical/unsigned)a << b— Bitshift left~a— Bitwise NOT
All of these operations can be applied to integers and vectors. Vector operations are applied componentwise.
If an operation is applied to two vectors of different lengths, one vector is cast to the length of the other before the operation is applied. (See Vector to vector conversion.)
- For
|,^,>>,<<<, and<<, the shorter vector is cast to the length of the longer one - For
&, the longer vector is cast to the length of the shorter one
If an operation is applied to a vector and an integer (in either order), then the integer is converted to a vector of the same length. (See Integer to vector conversion.)
Bitshifting by less than 0 or more than 64 causes an error. (See Errors.)
Set operators¶
Given two values a and b:
a & b— Intersectiona | b— Uniona ^ b— Symmetric differencea &~ b— Relative complement of B in A
All of these operations can be applied to any set/filter type or its subtypes.
Comparison operators¶
Given two values a and b:
a == b— Doesaequalb?a != b— Doesanot equalb?a < b— Isaless thanb?a > b— Isagreater thanb?a <= b— Isaless than or equal tob?a >= b— Isagreater than or equal tob?
All of these comparisons can be applied to integers and vectors. == and != can be applied to cell states and patterns. Vector comparisons are applied componentwise.
If a comparison is applied to two vectors of different lengths, the shorter vector is to the length of the longer one before the operation is applied. (See Vector to vector conversion.)
If a comparison is applied to a vector and an integer (in either order), then the integer is converted to a vector of the same length. (See Integer to vector conversion.)
A != comparison between two vectors results in TRUE if any corresponding components of the two vectors are unequal. All other comparisons between two vectors result in TRUE if only if the comparison is TRUE for all corresponding components of the two vectors. Examples:
Boolean operators¶
a and b— Logical ANDa or b— Logical ORa xor b— Logical XORnot a— Logical NOT
Range operator¶
a..b