Gleam Standard Library

The Gleam standard library (gleam_stdlib) provides modules for everyday programming tasks. It is included in every project by default. Knowing what each module offers saves you from reinventing functionality that already exists and is well-tested.

Standard Library Overview


Module Map
──────────────────────────────────────────────────
gleam/io         → print, debug output
gleam/string     → text manipulation
gleam/int        → integer math and conversion
gleam/float      → float math and conversion
gleam/bool       → boolean operations
gleam/list       → list processing
gleam/map        → key-value stores
gleam/option     → optional values
gleam/result     → success/failure values
gleam/set        → unique collections
gleam/dict       → newer map API (alias for map)
gleam/bit_array  → binary data
gleam/bytes      → byte sequences
gleam/order      → comparison results
gleam/function   → function utilities
gleam/dynamic    → runtime type checking
gleam/uri        → URL parsing and building
gleam/regex      → regular expressions

gleam/io

import gleam/io

io.println("Hello")          // print with newline
io.print("no newline")       // print without newline
io.debug(any_value)          // print any type (for dev)
io.println_error("oops")     // print to stderr

gleam/int

import gleam/int

int.to_string(42)        // "42"
int.parse("99")          // Ok(99)
int.to_float(5)          // 5.0
int.absolute_value(-7)   // 7
int.power(2, 10)         // Ok(1024)
int.max(10, 20)          // 20
int.min(10, 20)          // 10
int.clamp(150, 0, 100)   // 100 (cap at max)
int.compare(5, 10)       // order.Lt
int.is_even(4)           // True
int.is_odd(7)            // True
int.digits(123, 10)      // Ok([1,2,3])

gleam/float

import gleam/float

float.to_string(3.14)        // "3.14"
float.parse("2.5")           // Ok(2.5)
float.ceiling(4.2)           // 5.0
float.floor(4.9)             // 4.0
float.round(4.5)             // 5.0
float.truncate(4.9)          // 4
float.absolute_value(-3.5)   // 3.5
float.square_root(16.0)      // Ok(4.0)
float.power(2.0, 8.0)        // Ok(256.0)
float.max(1.5, 2.5)          // 2.5
float.clamp(1.5, 0.0, 1.0)   // 1.0

gleam/bool

import gleam/bool

bool.to_string(True)            // "True"
bool.negate(False)              // True
bool.and(True, False)           // False
bool.or(True, False)            // True
bool.guard(True, "yes", fn() { "no" })  // "yes"

gleam/order

The Order type represents comparison results. Used in sorting and comparison functions:

import gleam/order.{Lt, Eq, Gt}
import gleam/int

let result = int.compare(5, 10)   // Lt
let desc = order.negate(result)   // Gt  (flip for descending sort)

gleam/function

import gleam/function

// compose: apply g then f
let double_then_str = function.compose(int.to_string, fn(n) { n * 2 })
double_then_str(5)   // "10"

// identity: returns its argument unchanged
function.identity(42)   // 42

// constant: always returns the same value
let always_zero = function.constant(0)
always_zero("anything")   // 0

gleam/set

import gleam/set

let s = set.from_list([1, 2, 3, 2, 1])
// {1, 2, 3}  — duplicates removed

let s2 = set.insert(s, 4)           // {1, 2, 3, 4}
let has_3 = set.contains(s, 3)      // True
let size = set.size(s)               // 3

let a = set.from_list([1, 2, 3])
let b = set.from_list([2, 3, 4])
set.intersection(a, b)  // {2, 3}
set.union(a, b)          // {1, 2, 3, 4}
set.difference(a, b)     // {1}

gleam/uri

import gleam/uri

let parsed = uri.parse("https://example.com/path?key=val")
// Ok(Uri(scheme: Some("https"), host: Some("example.com"), ...))

let built = uri.Uri(
  scheme: Some("https"),
  host: Some("api.example.com"),
  path: "/users",
  query: Some("page=1"),
  ..uri.empty
)
uri.to_string(built)
// "https://api.example.com/users?page=1"

gleam/regex

import gleam/regex

let pattern = regex.from_string("[0-9]+")

case pattern {
  Ok(re) ->
    regex.check(re, "abc123")    // True
  Error(_) ->
    io.println("invalid regex")
}

Practical Example — Data Validator

import gleam/string
import gleam/int
import gleam/regex

pub fn is_valid_email(email: String) -> Bool {
  string.contains(email, "@") && string.contains(email, ".")
}

pub fn is_valid_age(raw: String) -> Result(Int, String) {
  case int.parse(string.trim(raw)) {
    Error(_) -> Error("Not a number")
    Ok(age) ->
      case age >= 0 && age <= 130 {
        True  -> Ok(age)
        False -> Error("Age out of range")
      }
  }
}

Key Points


Standard Library Essentials
──────────────────────────────────────────────────
1. Included by default — no gleam add needed
2. gleam/io for printing
3. gleam/int and gleam/float for numeric work
4. gleam/string for text
5. gleam/list for collections
6. gleam/option and gleam/result for safe handling
7. gleam/set for unique collections
8. All modules are pure — no hidden side effects

The standard library covers most daily needs. Before reaching for an external package, check whether a standard module already provides what you need. Familiarity with the stdlib reduces dependencies and keeps projects lean.

Leave a Comment

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