Gleam Operators

Operators perform computations on values. Gleam separates operators by type — integer operators work only on integers, float operators work only on floats. This strict separation prevents silent precision errors that plague many other languages.

Arithmetic Operators for Int

These operators work on Int values only:


Int Arithmetic Operators
────────────────────────────────────────────────
Operator │ Name           │ Example   │ Result
─────────┼────────────────┼───────────┼────────
+        │ Addition       │ 10 + 3    │ 13
-        │ Subtraction    │ 10 - 3    │ 7
*        │ Multiplication │ 10 * 3    │ 30
/        │ Division       │ 10 / 3    │ 3  (truncated)
%        │ Remainder      │ 10 % 3    │ 1

Integer division in Gleam truncates toward zero — the result drops any decimal part. So 10 / 3 gives 3, not 3.333.

let apples = 17
let baskets = 5
let per_basket = apples / baskets   // 3
let leftover = apples % baskets     // 2

Visual: 17 apples into 5 baskets
──────────────────────────────────────────────────
Basket 1: 🍎🍎🍎
Basket 2: 🍎🍎🍎
Basket 3: 🍎🍎🍎
Basket 4: 🍎🍎🍎
Basket 5: 🍎🍎🍎
Leftover: 🍎🍎  ← this is the remainder (%)

Arithmetic Operators for Float

Float operators add a dot after each symbol:


Float Arithmetic Operators
────────────────────────────────────────────────
Operator │ Name           │ Example       │ Result
─────────┼────────────────┼───────────────┼────────
+.       │ Addition       │ 1.5 +. 2.5    │ 4.0
-.       │ Subtraction    │ 5.0 -. 1.5    │ 3.5
*.       │ Multiplication │ 2.0 *. 3.0    │ 6.0
/.       │ Division       │ 7.0 /. 2.0    │ 3.5
let price = 49.99
let tax_rate = 0.18
let tax = price *. tax_rate     // 8.9982
let total = price +. tax        // 58.9882

Why Different Operators?


The Problem in Other Languages
──────────────────────────────────────────────────
Python:  5 / 2 = 2.5    (implicit float conversion)
         5 // 2 = 2     (integer division)

Gleam:   5 / 2 = 2      (Int division, always)
         5.0 /. 2.0 = 2.5 (Float division, always)

The dot in Gleam's float operators makes the type
visible in the code — no guessing required.

Comparison Operators

These operators compare two values and return a Bool:


Comparison Operators (work for Int, Float, String, Bool)
────────────────────────────────────────────────────────
Operator │ Meaning                │ Example        │ Result
─────────┼────────────────────────┼────────────────┼────────
==       │ Equal to               │ 5 == 5         │ True
!=       │ Not equal to           │ 5 != 3         │ True
<        │ Less than              │ 3 < 7          │ True
>        │ Greater than           │ 7 > 3          │ True
<=       │ Less than or equal     │ 5 <= 5         │ True
>=       │ Greater than or equal  │ 6 >= 10        │ False
let temperature = 38
let is_fever = temperature >= 38   // True
let is_below_zero = temperature < 0 // False

Boolean Operators

These combine Bool values together:


Boolean Operators
────────────────────────────────────────────────
Operator │ Name │ Example          │ Result
─────────┼──────┼──────────────────┼────────
&&       │ AND  │ True && False    │ False
||       │ OR   │ True || False    │ True
!        │ NOT  │ !True            │ False
let age = 20
let has_id = True

let can_enter = age >= 18 && has_id   // True — both must be true

let is_student = False
let is_teacher = True
let in_class = is_student || is_teacher  // True — either works

Short-Circuit Evaluation

Gleam evaluates && and || lazily. For &&, if the left side is False, Gleam skips the right side — the result is False regardless. For ||, if the left side is True, Gleam skips the right side.


Short-Circuit Diagram
──────────────────────────────────────────────
False && expensive_check()
         ↑
         Never runs — Gleam already knows the result is False

True || expensive_check()
        ↑
        Never runs — Gleam already knows the result is True

String Operators

Gleam provides one dedicated string operator:


String Operator
────────────────────────────────────────────────
Operator │ Name           │ Example
─────────┼────────────────┼──────────────────────────
<>       │ Concatenation  │ "Gleam" <> " is great"
                            → "Gleam is great"
let first = "Karan"
let last = "Mehta"
let full_name = first <> " " <> last   // "Karan Mehta"

The Pipe Operator

The pipe operator |> passes the result of one expression as the first argument of the next function. It creates a clean left-to-right reading flow.


Without pipe (hard to read, inside-out):
──────────────────────────────────────────────
string.uppercase(string.trim("  gleam  "))

With pipe (reads left to right):
──────────────────────────────────────────────
"  gleam  "
|> string.trim        // "gleam"
|> string.uppercase   // "GLEAM"

The pipe operator is one of Gleam's most loved features. It makes chained operations readable like a recipe:

let result =
  100
  |> int.to_float
  |> float.multiply(1.18)
  |> float.to_string
// result = "118.0"

Operator Precedence

When multiple operators appear in one expression, Gleam evaluates higher-precedence operators first:


Precedence (highest at top)
──────────────────────────────────────────────────
Level │ Operators
──────┼──────────────────────────────
  1   │ Unary  -  !
  2   │ *   /   %   *.   /.
  3   │ +   -   +.   -.
  4   │ <>  (string concat)
  5   │ ==  !=  <  >  <=  >=
  6   │ &&
  7   │ ||
  8   │ |>  (pipe — lowest)
let result = 2 + 3 * 4   // 14 (not 20)
                          // * runs before +

Use parentheses to override the default order:

let result = (2 + 3) * 4   // 20

Practical Example — Shopping Cart

import gleam/io
import gleam/float

pub fn cart_total(quantity: Int, unit_price: Float, discount: Float) -> Float {
  let subtotal = int.to_float(quantity) *. unit_price
  let discount_amount = subtotal *. discount
  let total = subtotal -. discount_amount
  total
}

pub fn main() {
  let total = cart_total(3, 250.0, 0.10)
  io.debug(total)   // 675.0
}

Calculation Flow
──────────────────────────────────────────────────
quantity = 3, price = 250.0, discount = 10%

  3 × 250.0 = 750.0   (subtotal)
750.0 × 0.10 = 75.0   (discount amount)
750.0 - 75.0 = 675.0  (final total)

Gleam's separate Int and Float operators force you to be explicit about which kind of number you are working with — and that clarity makes the math in your programs easy to read and verify.

Leave a Comment

Your email address will not be published. Required fields are marked *