Tracks
/
Gleam
Gleam
/
Syllabus
/
Anonymous Functions
An

Anonymous Functions in Gleam

2 exercises

About Anonymous Functions

Gleam has first class functions, meaning functions are normal values that can be assigned to variables, passed as arguments, and returned from other functions.

A named function defined in a module can be referenced by its name.

pub fn main() {
  // Assign the function to a variable
  let f = add_one
  
  // Invoke the function
  f(1) // -> 2
  f(2) // -> 3
}

fn add_one(x) {
  x + 1
}

Gleam also has anonymous functions, which are defined within other functions.

// Define the function
let f = fn(x) { x + 1 }

// Invoke the function
f(1) // -> 2
f(2) // -> 3

The type of a function is written using a similar syntax to anonymous functions. The type of a function that takes an Int and a Float and returns an Int is written like this:

fn(Int, Float) -> Int

Anonymous functions can reference variables that were in scope when they were defined, making them closures.

let secret_number = 42

// This function always returns 42
fn() { secret_number }

The function capture syntax provides a convenient shorthand for creating anonymous functions that pass a single argument to a function. These two expressions are equivalent:

// Anonymous function syntax
let f = fn(x) { my_function(1, 2, x) }

// Function capture syntax
let f = my_function(1, 2, _)
Edit via GitHub The link opens in a new window or tab

Learn Anonymous Functions

Practicing is locked

Unlock 1 more exercise to practice Anonymous Functions