Gleam
2019fragletfunctionalfunctionalconcurrent.gleam
docker run --rm --platform="linux/amd64" 100hellos/gleam:latest
MCP + fragletc
MCP
This language supports code execution via MCP and the fragletc CLI.
Install fragletc โA type-safe language for the Erlang virtual machine!
Gleam is a friendly language for building type-safe, scalable systems! โจ
What makes Gleam special? ๐
- Type-safe - Catch bugs at compile time, not runtime
- Functional - Immutable data and functions as first-class citizens
- Concurrent - Built on the battle-tested Erlang/OTP platform
- Fast compilation - No more waiting around for builds
- Friendly errors - Error messages that actually help you
- JavaScript target - Run on the web or Node.js too!
Hello World!
import gleam/io
pub fn main() {
io.println("Hello World!")
}Fun Facts! ๐
- Born in 2020 - One of the newest languages on the block
- Erlang heritage - Inherits 30+ years of fault-tolerance wisdom
- Actor model - Handle millions of concurrent processes effortlessly
- Pattern matching - Destructure data with elegant syntax
- No null - Say goodbye to null pointer exceptions forever!
- Hot code swapping - Update running systems without downtime
- BEAM VM - The same virtual machine that powers WhatsApp and Discord
Why Erlang/OTP? ๐
The Erlang virtual machine (BEAM) is legendary for:
- 9 nines uptime (99.9999999% availability)
- Fault isolation - One crash doesn't bring down the whole system
- Massive concurrency - Handle millions of lightweight processes
- Distribution - Built for multi-node systems from day one
Gleam brings modern syntax and compile-time safety to this proven platform!
Language Features ๐
- Pipe operator
|>for readable data transformations - Result types instead of exceptions
- Immutable everything - No accidental mutations
- Generic types with full type inference
- Zero-cost abstractions - Pay only for what you use
- Comprehensive standard library - Batteries included!
Perfect for web services, IoT systems, distributed applications, and anywhere you need reliability! ๐ฏ
Hello World
// BEGIN_FRAGLET
import gleam/io
pub fn main() {
io.println("Hello World!")
}
// END_FRAGLETCoding Guide
Language Version
Gleam (latest available in Alpine)
Execution Model
- Compiled, then executed via Erlang/BEAM VM
- Execution via
gleam run(requires project structure)
Key Characteristics
- Functional programming language
- Type-safe with compile-time type checking
- Immutable data structures
- Pattern matching
- No null values - uses
ResultandOptiontypes - Case-sensitive
- Pipe operator
|>for data transformations - Functions are first-class citizens
- Variables start with lowercase:
x,name - Types start with uppercase:
Int,String,Result
Fragment Authoring
Write a complete Gleam module body. Your fragment must define pub fn main() as the entry point. You can define additional functions, types, and use @external for Erlang FFI.
Available Libraries
The gleam_stdlib package is included (gleam/io, gleam/string, gleam/list, gleam/result, gleam/option, etc.). Erlang standard library functions are accessible via @external FFI.
Common Patterns
- Print:
io.println("message") - String concatenation:
"Hello, " <> name - Pattern matching:
case x { 1 -> "one" } - Pipe operator:
list |> list.map(fn(x) { x * 2 }) - Result handling:
case result { Ok(value) -> ... Error(e) -> ... } - Stdin:
@external(erlang, "io", "get_line") fn get_line(prompt: String) -> String - Args: Use
@external(erlang, "init", "get_plain_arguments")with aCharlisttype andlist_to_binaryconversion - Option handling:
case option { Some(value) -> ... None -> ... } - Function definitions:
pub fn greet(name) { ... } - Anonymous functions:
fn(x) { x * 2 } - List operations:
list.map(fn(x) { x * 2 }),list.filter(...),list.length(...)
Examples
// Simple output
pub fn main() {
io.println("Hello, World!")
}// Function with parameters
pub fn main() {
greet("Alice")
}
fn greet(name: String) {
io.println("Hello, " <> name <> "!")
}// Pattern matching with custom type
import gleam/int
pub type Op {
Add
Multiply
}
pub fn main() {
let result = calculate(Add, 5, 10)
io.println("Result: " <> int.to_string(result))
}
fn calculate(op: Op, a: Int, b: Int) -> Int {
case op {
Add -> a + b
Multiply -> a * b
}
}// List processing with pipe operator
import gleam/list
import gleam/int
pub fn main() {
let numbers = [1, 2, 3, 4, 5]
let sum = numbers
|> list.map(fn(x) { x * x })
|> list.fold(0, fn(acc, x) { acc + x })
io.println("Sum of squares: " <> int.to_string(sum))
}// Recursion
import gleam/int
pub fn main() {
let result = factorial(5)
io.println("Factorial of 5: " <> int.to_string(result))
}
fn factorial(n: Int) -> Int {
case n {
0 -> 1
n -> n * factorial(n - 1)
}
}// Multiple functions
import gleam/int
pub fn main() {
io.println("Double 5: " <> int.to_string(double(5)))
io.println("Triple 5: " <> int.to_string(triple(5)))
}
fn double(x: Int) -> Int {
x * 2
}
fn triple(x: Int) -> Int {
x * 3
}// Using Result type
import gleam/result
import gleam/int
pub fn main() {
case divide(10, 2) {
Ok(value) -> io.println("Result: " <> int.to_string(value))
Error(_) -> io.println("Division by zero!")
}
}
fn divide(a: Int, b: Int) -> Result(Int, String) {
case b {
0 -> Error("Cannot divide by zero")
_ -> Ok(a / b)
}
}// List comprehensions and filtering
pub fn main() {
let numbers = [1, 2, 3, 4, 5, 6]
let evens = list.filter(numbers, fn(x) { x % 2 == 0 })
io.println("Even numbers: " <> debug.to_string(evens))
}Caveats
- Module name must match filename (without extension)
- Functions must be
pub fnto be callable from command line - Gleam uses
<>for string concatenation - Variables are immutable (single-assignment)
- Pattern matching is exhaustive - all cases must be covered
- Type annotations are optional but recommended for clarity
- The main function must be
pub fn main()and returnNil - Gleam requires a project structure (gleam.toml) to compile
- Import statements must be at the top of the file (outside the injection range)
Fraglet Scripts
Test
#!/usr/bin/env -S fragletc --vein=gleam
import gleam/io
pub fn main() {
io.println("Hello World!")
}