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 with 0 would 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 — Addition
  • a - b — Subtraction
  • a * b — Multiplication
  • a / b — Division (rounds toward zero)
  • a % b — Remainder
  • a ** 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 ** 8256
  • [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:

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:

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 — Does a equal b?
  • a != b — Does a not equal b?
  • a < b — Is a less than b?
  • a > b — Is a greater than b?
  • a <= b — Is a less than or equal to b?
  • a >= b — Is a greater than or equal to b?

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:

  • [1, 2] == [1, 2, 0]TRUE
  • [1, 2] == [1, 2, 3]FALSE because 0 == 3FALSE
  • [1, 2] != [1, 2, 3]TRUE because 0 != 3TRUE
  • [-1, 2] < [0, 4]TRUE because -1 < 0TRUE and 2 < 4TRUE
  • [-1, 2] < [0, 1]FALSE because 2 < 1FALSE

Boolean operators

Range operator

  • a..b

Vector indexing

  • v[n]

Indexing a vector v by an integer n results in the n-th component of v. The X component has index 0, the Y component has index 1, etc. Indexing with a value n that is not between 0 and v.len - 1 (inclusive) causes an error. (See Errors.)

Filter test

  • a is b

This operator takes a basic type for a and the corresponding filter type for b.