Fundamentals 8 min read

Is Gleam the Friendly Functional Language You’ve Been Waiting For?

This article introduces Gleam, a type‑safe functional language that compiles to Erlang and JavaScript, walks through installation, a hello‑world program, package management, pattern matching with algebraic data types, and demonstrates its concurrency‑friendly, null‑free design.

21CTO
21CTO
21CTO
Is Gleam the Friendly Functional Language You’ve Been Waiting For?

Gleam is a type‑safe functional programming language for building scalable concurrent systems. It compiles to Erlang and JavaScript, allowing direct inter‑operation with other BEAM languages such as Erlang and Elixir.

Erlang’s long‑standing focus on concurrency and fault‑tolerance makes it a solid foundation, and Gleam aims to be friendly without assuming prior knowledge.

Hello World

import gleam/io

pub fn main() {
  io.println("hello world!")
}

Installation on macOS can be done via Homebrew, which also installs Erlang automatically.

brew install gleam

Gleam provides a project generator similar to Rails; creating a new “hello” project yields a default hello.gleam file containing the hello‑world code.

Package Management

Two .toml files configure dependencies. In gleam.toml you declare required libraries, for example:

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"

Adding packages such as envoy (environment variables) and argv (command‑line arguments) lets you read an environment variable based on user input:

import argv
import envoy
import gleam/io
import gleam/result

pub fn main() {
  case argv.load().arguments {
    ["get", name] -> get(name)
    _ -> io.println("Usage: get <name>")
  }
}

fn get(name: String) -> Nil {
  let value = envoy.get(name) |> result.unwrap("")
  io.println(format_pair(name, value))
}

fn format_pair(name: String, value: String) -> String {
  name <> "=" <> value
}

The case expression matches patterns, and the underscore acts as a catch‑all. Gleam’s pipe operator ( |>) improves readability of chained function calls.

Algebraic Data Types

Custom types can be defined and pattern‑matched, for example:

pub type Season {
  Spring
  Summer
  Autumn
  Winter
}

fn weather(season: Season) -> String {
  case season {
    Spring -> "Mild"
    Summer -> "Hot"
    Autumn -> "Windy"
    Winter -> "Cold"
  }
}

Another example shows a sum type Travel with associated data:

pub type Travel {
  Walk(hours: Int)
  Cycle(hours: Int)
  Drive(hours: Int, speed: Int)
}

pub fn main() {
  let walking = Walk(1)
  let cycling = Cycle(1)
  let bus_trip = Drive(2, 50)

  let trip = [walking, cycling, bus_trip]
  io.debug(trip)
}

Gleam has no null, no implicit conversions, and no exceptions; errors are handled with the built‑in Result type. Numeric operators are distinct for integers ( +) and floats ( +.).

Overall, Gleam offers a gentle entry point to functional programming while preserving the benefits of immutability and type safety.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptconcurrencyfunctional programmingErlangGleam
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.