Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Gleam: A Basic Introduction

Gleam is a statically-typed functional programming language, which recently had its v1 released, designed for building scalable and maintainable software. It is inspired by languages like Elm and Rust and compiles down to Erlang, making it well-suited for building concurrent and distributed systems. In this tutorial, we’ll cover the basics of Gleam to help you get started with this expressive and powerful language.

Preparing the environment

Before diving into Gleam, you need to set up the development environment. The language compiles to Erlang and Javascript, and while the second doesn’t require anything but a browser and the gleam compiler to run, the first will need the compiler, Erlang and Rebar3. Let’s focus on that:

  1. First, grab a pre-compiled version of Gleam here: https://github.com/gleam-lang/gleam/releases
  2. Then, install Erlang from the Erlang-Solutions website.
  3. Finally, go to rebar3 “getting started” page and follow their instructions. This tool will be necessary in certain cases, such as Web Servers and HTTP clients.

With everything in place, install a plugin to support syntax highlighting in your editor of choice.

Your First Gleam Program

Let’s create a simple “Hello, World!” program to get started. Open your favorite text editor and create a file named hello.gleam with the following content:

// hello.gleam
import gleam/io

pub fn main() {
  io.println("Hello, Gleam!")
}

Save the file and run it using the Gleam compiler:

gleam run hello.gleam

You should see the output:

Hello, Gleam!

Congratulations! You’ve just written and executed your first Gleam program.

Variables and Types

Gleam is a statically-typed language, meaning you must declare the type of a variable before using it. Here’s an example demonstrating variable declaration and basic types:

// variables.gleam
import gleam/io

pub fn main() {
  let message: String = "Hello, Gleam!"
  let number: Int = 42
  let is_true: Bool = True
  let ints = [1, 2, 3]

  io.debug(ints)
  io.debug(message)
  io.debug(number)
  io.debug(is_true)
}

Run the program to see the output:

gleam run variables.gleam

As you may have noticed, this time we used io.debug instead of io.println . That is because the later only works with strings, while the first will print any type.

Arithmetic Operations

Gleam supports standard arithmetic operations for numerical values. Here’s a concise example demonstrating addition:

import gleam/io
import gleam/int

pub fn main() {
  // Int arithmetic
  io.debug(1 + 1)
  io.debug(5 - 1)
  io.debug(5 / 2)
  io.debug(3 * 3)
  io.debug(5 % 2)

  // Int comparisons
  io.debug(2 > 1)
  io.debug(2 = 1)
  io.debug(2 

In this program, the + operator adds the numbers 3 and 5. Run the program:

gleam run arithmetic.gleam

This should output:

2
4
2
9
1
True
False
True
False
True
False
77
10

Functions

Functions are a fundamental part of Gleam. Here’s an example of defining and calling a function:

// functions.gleam
import gleam/io

pub fn main() {
  let result = add(3, 5)
  io.debug(result) //8
}

fn add(x: Int, y: Int) -> Int {
  x + y
}

Run the program and observe the output:

gleam run functions.gleam

Case expressions

In Gleam, case expressions offer a concise and expressive way to handle multiple conditions. Here’s a simple example demonstrating how to use case expressions for pattern matching:

import gleam/io
import gleam/int

pub fn main() {
  let x = int.random(5)
  io.debug(x)

  let result = case x {
    // Match specific values
    0 -> "Zero"
    1 -> "One"

    // Match any other value
    _ -> "Other"
  }
  io.debug(result)
}

This code generates a random integer between 0 and 4 (inclusive) and then uses a case expression to match and print a corresponding message based on the generated value.

Conclusion

In conclusion, Gleam emerges as a compelling language for developers seeking a balance between the expressiveness of functional programming and the performance of concurrent and distributed systems. Drawing inspiration from Elm and Rust, Gleam’s statically-typed nature ensures robustness and maintainability in software development, while being simple and easy to learn over an afternoon.

If you wish to learn more, Gleam has a language tour available on their website!


Get articles on your E-mail

The post Gleam: A Basic Introduction appeared first on Peq42.



This post first appeared on Peq42 - Indie Game Dev, please read the originial post: here

Share the post

Gleam: A Basic Introduction

×

Subscribe to Peq42 - Indie Game Dev

Get updates delivered right to your inbox!

Thank you for your subscription

×