VOOZH about

URL: https://thenewstack.io/introduction-to-gleam-a-new-functional-programming-language/

⇱ Introduction to Gleam, a New Functional Programming Language - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-06-22 04:00:47
Introduction to Gleam, a New Functional Programming Language
tutorial,
JavaScript / Programming Languages / Software Development

Introduction to Gleam, a New Functional Programming Language

Gleam is a type safe functional programming language for building scalable concurrent systems. Is it as friendly as it claims? We find out.
Jun 22nd, 2024 4:00am by David Eastman
👁 Featued image for: Introduction to Gleam, a New Functional Programming Language
Image via Unsplash+. 
When my colleague read my Virgil post, he immediately suggested I look at Gleam. It is cool and new — version 1 was released in March this year — and comes out solidly on the functional side of programming life. Gleam is a type-safe functional programming language for building scalable concurrent systems. It compiles to Erlang and JavaScript, so has straightforward interoperability with other “BEAM” languages such as Erlang and Elixir. (BEAM is the virtual machine that executes user code in the Erlang Runtime System. I believe its short for Bogdan’s Erlang Abstract Machine. Don’t ask.) Erlang was an early telecoms industry language, very much focusing on concurrency and fault tolerance. Its ways of doing things is still respected and accounts for Elixir’s popularity. In this post, I won’t assume you are familiar with these; and actually, Gleam is particularly friendly, so it doesn’t make too many assumptions either. Let’s start with hello world:
import gleam/io 

pub fn main() { 
 io.println("hello world!") 
}
This is pretty similar to the same thing in Zig. There is a very pleasant language tour that makes use of Gleam’s compiling to JavaScript to give dynamic checking. You can also use it as a playground. Installing Gleam also means installing Erlang. For my Mac, I just used Homebrew:
> brew install gleam
Homebrew installs Erlang, automatically. Gleam comes with a template (or project) generator, much like Rails. So to make a new hello project, I just typed: 👁 Image
Rather than saving time for the moment, the “hello world” style one-liner is already there as the default code in hello.gleam: 👁 Image
If I run the whole project: 👁 Image
Note that the two packages were only compiled on the first run.

Package Management

There are two .toml files (apparently Tom’s Own Markup Language. Don’t ask), which act as configuration. As they should be simple, we can have a quick peek. In the gleam.toml:
[dependencies] 
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
Note that they have a version constraint — mentioning the maximum version in order to reduce incompatibility. The actual version downloaded and used is mentioned in the manifest.toml. We can learn a little Gleam and work with the package manager if we follow a simple example. We’ll add a couple of packages, and write some code to print out environment variables. I’ll use the same hello project template, but with the new code inserted. First, we’ll add the new packages to allow environment reading (envoy) and the reading of command line arguments (argv) — which you might expect to be built-in but might reflect system differences. 👁 Image
So let’s replace the code in hello.gleam with the code to print out environment variables on demand:
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 
}
Added to the public main entry point, we have two functions. They use exactly the same format as we saw in Virgil. It turns out that type annotations are optional, but considered good practice. Now, we get a bit functional. The argv load does what you expect, and pulls in a list of hopefully exactly two strings — with the first string equal to “get”. This is used in a case statement. As a quick aside, the Gleam case is a little more flexible than in most non-functional languages. Here we see a lists’ contents being compared:
let result = case x { 
 [] -> "Empty list" 
 [1] -> "List of just 1" 
 [4, ..] -> "List starting with 4" 
 [_, _] -> "List of 2 elements" 
 _ -> "Some other list" 
}
So, patterns can be compared in case statements. That underline _ represents a default, and the possible cases are exhaustively checked. Going back to our environment variable reading code, if the pattern isn’t a list of two strings, then the helper text is spat out. Otherwise, it calls the get function. We see the pipe function, which just helps to make long functional calls a little more readable from left to right.
let value = envoy.get(name) |> result.unwrap("")
This is the same as:
let value = result.unwrap(envoy.get(name),"")
Because Gleam doesn’t throw exceptions, it uses the built-in Result type, and unwrap fetches the good path value. The final oddity is:
name <> "=" <> value
…which is just string concatenation. And here I run it, with the required arguments the second time: 👁 Image
Gleam has no null, no implicit conversions, and no exceptions. So if it compiles, you are good. Also, there is no numerical operator overloading, so the code for adding integers is different to that for adding floats:
io.debug(1 + 1) //ints 
io.debug(1.0 +. 1.5) //floats
Equality works for any type. The general concept of immutability is best experienced by using a functional language for a bit, so I won’t gloss over it. It does help cut out a whole subset of bugs.

Algebraic Data Types

Finally, we saw Algebraic Data Types (ADTs) used in Virgil, so I’m keen to see how the equivalent works in Gleam. In fact, we’ve already seen the use of the case statement. We get custom types, which we pattern match over. So we are part of the way there:
pub type Season { 
 Spring 
 Summer
 Autumn 
 Winter 
} 

fn weather(season: Season) -> String { 
 case season { 
 Spring -> "Mild" 
 Summer -> "Hot" 
 Autumn -> "Windy" 
 Winter -> "Cold" 
 } 
}
Types can hold data in records, which is how we get close to my Virgil example:
import gleam/io

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)
}

// [Walk(hours: 1), Cycle(hours: 1), Drive(hours: 2, speed: 50)]
I don’t think I can associate a method inside a type, but I can access the record values to get a similar result as we got in Virgil. I’ll leave this as an exercise for a more fluent user! For someone like me who doesn’t work with functional code much, Gleam is very approachable and doesn’t immediately confront me with terminology like “currying” and other functional shocks. But it should be a good way to get you to appreciate the immutable advantages of programming if you are not already an advocate.
TRENDING STORIES
David has been a London-based professional software developer with Oracle Corp. and British Telecom, and a consultant helping teams work in a more agile fashion. He wrote a book on UI design and has been writing technical articles ever since....
Read more from David Eastman
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.