Chapter 1: First steps

Betty's bike shop

  • Float
  • pipes
  • interpolation

Floats have their own operators, like *. and +., so an Int never turns into a Float by accident.

The pipe |> passes the value on its left as the first argument of the call on its right. Read it top to bottom: take 21, convert it to pounds, turn that into a string, print it.

"£#{float.to_string(pounds)}" is string interpolation: any String expression can go inside #{…}. Gleam has no interpolation, so it becomes "£" <> ….

Open in playground
import gleam/float
import gleam/io
import gleam/int

def pence_to_pounds(pence: Int) -> Float {
  int.to_float(pence) *. 0.01
}

def pounds_to_string(pounds: Float) -> String {
  "£#{float.to_string(pounds)}"
}

pub def main() {
  106.0
  |> pounds_to_string()
  |> io.println()

  21
  |> pence_to_pounds()
  |> float.to_string()
  |> echo
}

An exercism exercise from the Gleam track, solved in Bea: beacode/src/e4_bettys_bikeshop.bea