On this page
Operators
Halyard provides a rich set of operators for arithmetic, comparison, logical operations, and more.
Arithmetic Operators
Basic mathematical operations:
| Operator | Description | Example |
|---|---|---|
+ | Addition | 1 + 2 |
- | Subtraction | 5 - 3 |
* | Multiplication | 4 * 2 |
/ | Division | 10.0 / 3.0 |
// | Integer division | 10 // 3 |
% | Modulo | 10 % 3 |
^ | Power | 2 ^ 8 |
let addition = 5 + 3
let subtraction = 10 - 4
let multiply = 6 * 7
let divide = 15.0 / 3.0
let intDiv = 10 // 3
let modulo = 17 % 5
let power = 2 ^ 10
let negative = -42 -- -42 (negation)
power
Compiled
Arithmetic operators require both operands to implement the Numeric trait. The built-in Numeric types are Int, Float, and Decimal. Passing a Bool, String, or other non-numeric type to +, -, *, /, %, or ^ is a compile-time error.
/, //, and % are further constrained beyond Numeric:
/requiresFractional—FloatandDecimal, but notInt. Dividing twoIntvalues does not compile; convert them first withMath.toFloat.//and%requireIntegral—Intonly, notFloatorDecimal. Integer division or modulo on aFloatorDecimaldoes not compile.
This means 1 / 2 is a compile-time error, not a silent Float result — the ambiguity between fractional and truncating division must be resolved explicitly at the call site.
Integer vs Float Division
/ and // serve different purposes and accept different operand types:
-- `/` requires Fractional (Float, Decimal — not Int); `//` requires
-- Integral (Int only). Mixing them up is a compile-time error.
let floatDiv = 10.0 / 3.0
let intDiv = 10 // 3
(floatDiv, intDiv)
Compiled
Numeric Type Promotion
When mixing Int and Float, Int is promoted to Float:
let x = 1 + 2.5
let y = 3 * 1.5
x + y
Compiled
The Int operand is converted to a real Float and the operation is the Float one. This also holds for /: 1 / 2.0 and 1.0 / 2 are 0.5, because the mixed operands meet at Float; only / on two Ints is rejected. Promotion goes one way only — a Float never narrows to an Int.
Decimal does not mix with Int or Float values. A number literal beside a Decimal operand is built as a Decimal (19.99d * 3 is 59.97d), but an Int or Float value beside one is a compile-time type error; convert an Int with Decimal.fromInt. See Numeric Type Promotion in the types guide for the whole rule.
Comparison Operators
Compare values and return Bool:
| Operator | Description | Example |
|---|---|---|
== | Equal | a == b |
!= | Not equal | a != b |
< | Less than | a < b |
<= | Less than or equal | a <= b |
> | Greater than | a > b |
>= | Greater than or equal | a >= b |
let eq = 5 == 5
let neq = 5 != 6
let lt = 3 < 5
let gt = 5 > 3
let lte = 3 <= 3
let gte = 5 >= 4
(eq, neq, lt, gt, lte, gte)
Compiled
The ordering operators <, <=, >, >= require both operands to implement the Ord trait. All primitive types (Int, Float, Decimal, String, Bool, Char) implement Ord. Custom types do not unless an explicit impl is provided.
== and != work on all scalar types (Int, Float, Decimal, Bool, String, Char, Symbol, Unit), Enum variants, and structural collections (Tuple, List, Record). They do not work on functions, DataFrames, Matrices, or open record types.
Logical Operators
Boolean logic operations:
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | a && b |
|| | Logical OR | a || b |
not | Logical NOT | not a |
-- And (both must be True)
let and1 = True && True
let and2 = True && False
-- Or (at least one must be True)
let or1 = True || False
let or2 = False || False
-- Not (negation)
let not1 = not True -- False
let not2 = not False -- True
let not3 = not (1 > 2)
-- True
let not4 = not not True -- True (double negation)
(and1, or1, not1, not3)
Compiled
Short-circuit Evaluation
Logical operators use short-circuit evaluation:
-- False && ... returns False immediately without evaluating the right side
let result = False && True
result -- False
Compiled
Bitwise Operators
Bitwise operations on Int values (two's-complement 64-bit integers):
| Operator | Description | Example |
|---|---|---|
~ | Bitwise NOT | ~n |
~n flips every bit of n. The result satisfies ~~n == n (double inversion is identity) and ~n == -(n + 1) for all Int values.
let n = 42
let inverted = ~n -- -43 (two's-complement bitwise NOT)
let restored = ~inverted -- 42 (double inversion is identity)
restored
Compiled
~ accepts only Int. Applying it to Float, Decimal, or Bool is a compile error.
String Operators
String manipulation:
"Hello, " ++ "World!"
Compiled
List Operators
Working with lists:
| Operator | Description | Example |
|---|---|---|
:: | Cons (prepend) | 1 :: [2, 3] |
++ | Concatenation | [1, 2] ++ [3, 4] |
Both sit at one precedence level and associate to the right, below + and -,
so xs ++ y :: ys means xs ++ (y :: ys) and 1 + 2 :: [3] means
(1 + 2) :: [3]. ++ concatenates strings as well as lists.
-- Cons (prepend element)
let consed1 = 1 :: [2, 3]
let consed2 : [Int] = 1 :: (2 :: [])
-- Concatenation
let joined = [1, 2] ++ [3, 4]
joined
Compiled
Pipe Operators
Chain function calls elegantly:
| Operator | Description | Example |
|---|---|---|
|> | Pipe forward | x |> f |
<| | Pipe backward | f <| x |
fn double : Int -> Int
fn double x =
x * 2
fn addOne : Int -> Int
fn addOne x =
x + 1
-- Forward pipe
5 |> double -- Same as: double 5 = 10
Compiled
import List
-- Pipeline example
[1, 2, 3, 4, 5]
|> List.filter (|x| x > 2)
|> List.map (|x| x * 2)
-- [6, 8, 10]
Compiled
Function Composition
Compose functions into new functions:
| Operator | Description | Example |
|---|---|---|
>> | Compose (left to right) | f >> g |
<< | Compose (right to left) | g << f |
-- Forward composition (f first, then g)
let addOne = |x : Int| x + 1
let double = |x : Int| x * 2
let addThenDouble = addOne >> double
addThenDouble 5 -- 12 (5 + 1 = 6, 6 * 2 = 12)
Compiled
-- Backward composition (g first, then f)
let addOne = |x : Int| x + 1
let double = |x : Int| x * 2
let doubleThenAdd = addOne << double
doubleThenAdd 5 -- 11 (5 * 2 = 10, 10 + 1 = 11)
Compiled
Result Propagation Operator
The ? postfix operator works on Result values:
| Operator | Description |
|---|---|
? | Unwrap Ok value; short-circuit with Err if the result is an error |
-- ? unwraps Ok values and short-circuits on Err
let addOne = |x: Int| Ok (x + 1)
let result = 5 |> addOne
result?
Compiled
If the expression is Ok v, ? evaluates to v. If it is Err e, the expression evaluates to Err e (the error is preserved). Applying ? to a non-Result type is a compile error.
Operators with DataFrame Expressions
Arithmetic (+, -, *, /, %, ^), comparison (==, !=, <, <=, >, >=), boolean (&&, ||), and unary (-, not) operators work directly on Expr values from the DataFrame.Expr module. When either operand is an Expr, scalars are automatically coerced to literal expressions:
-- Operators work directly on column references
import DataFrame
import Result
import DataFrame.Expr as Expr
let df =
DataFrame.fromRecords
[ { price = 10.0, quantity = 2, active = True }
, { price = 20.0, quantity = 3, active = False }
]
-- Arithmetic: @col * scalar -> Expr
-- Comparison: @col >= scalar -> Expr (boolean)
-- Boolean: Expr && Expr -> Expr
let expr = @price * 1.1
df
|> DataFrame.applyExprs [(@adjusted, expr)]
|> Result.andThen (DataFrame.column @adjusted)
|> Result.withDefault []
Compiled
See the DataFrame Expressions guide for full details.
Best Practices
- Use parentheses when precedence is unclear
- Prefer pipes (
|>) for data transformation chains - Use composition (
>>) to build reusable function pipelines - Use
!=for inequality (not/=)
Next Steps
Learn about functions to create reusable code.