Operators
Stone uses operators as the primary syntax for arithmetic, comparison, logical, and array operations.
Scalar Arithmetic Operators
For scalar (single number) operations:
| Operator | Description |
|---|---|
a + b | Addition |
a - b | Subtraction |
a * b | Multiplication |
a / b | Division |
a % b | Modulo (remainder) |
a ^ b | Power (exponentiation) |
Note: These operators work on scalars only. Using them on arrays will produce an error—use element-wise operators instead.
Element-wise Array Operators
For arrays, use dot-prefixed operators for element-by-element operations:
| Operator | Description |
|---|---|
a .+ b | Element-wise addition |
a .- b | Element-wise subtraction |
a .* b | Element-wise multiplication |
a ./ b | Element-wise division |
a .^ b | Element-wise power |
Arrays must have matching shapes. These operators also work with scalar-array combinations (broadcasting).
a = [1, 2, 3]
b = [4, 5, 6]
c = a .+ b // [5, 7, 9]
d = a .* b // [4, 10, 18]
e = a .^ 2 // [1, 4, 9]
Matrix Operators
| Operator | Description |
|---|---|
A @ B | Matrix multiplication |
A ^ n | Matrix power (A must be square) |
import eye, transpose from linalg
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
C = A @ B // matrix multiplication
D = transpose(A) // [[1, 3], [2, 4]]
E = A ^ 2 // A @ A
Comparison Operators
| Operator | Description |
|---|---|
a > b | Greater than |
a < b | Less than |
a >= b | Greater or equal |
a <= b | Less or equal |
a == b | Equality |
a != b | Inequality |
Logical Operators
| Operator | Description |
|---|---|
a && b | Logical AND |
a || b | Logical OR |
!a | Logical NOT |
Pipe Operator
The pipe operator |> passes the left operand as the first argument to the function on the right:
| Operator | Description |
|---|---|
a |> f | Pipe: equivalent to f(a) |
a |> f(b, c) | Pipe with args: equivalent to f(a, b, c) |
The pipe operator enables a fluent, left-to-right coding style for function composition:
// Without pipe - nested calls read inside-out
result = h(g(f(x), a))
// With pipe - reads left-to-right
result = x |> f |> g(a) |> h
Pipes are left-associative and have the lowest precedence, so a + b |> f is parsed as (a + b) |> f.
// Transform and process data
data = [1, 2, 3, 4, 5]
result = data |> map(double) |> filter(is_even) |> sum
// Chain string operations
name = " hello world " |> trim |> upper |> split(" ")
Unary Operators
| Operator | Description |
|---|---|
-a | Negation |
!a | Logical NOT |
Precedence
Operators follow this precedence (highest to lowest):
- Postfix: member access (
.), function calls, indexing ([]) - Unary:
!,-(negation) - Power:
^,.^(right-associative) - Multiplicative:
*,/,%,.*,./ - Matrix multiplication:
@ - Additive:
+,-,.+,.- - Comparison:
>,<,>=,<= - Equality:
==,!= - Logical AND:
&& - Logical OR:
|| - Pipe:
|>
Use parentheses to override precedence when needed.
Examples
// Scalar arithmetic
result = 10 + 5 * 2 // 20 (multiplication first)
result = (10 + 5) * 2 // 30 (parentheses override)
power = 2 ^ 10 // 1024
// Comparison
is_valid = x > 0 && x < 100
// Logical
should_process = is_ready && !is_paused
// Modulo
remainder = 17 % 5 // 2
is_even = n % 2 == 0
// Array operations
xs = [1, 2, 3, 4]
ys = [5, 6, 7, 8]
sums = xs .+ ys // [6, 8, 10, 12]
products = xs .* ys // [5, 12, 21, 32]
squares = xs .^ 2 // [1, 4, 9, 16]
// Matrix operations
import transpose from linalg
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
C = A @ B // matrix product
D = transpose(A) // transpose