aboutsummaryrefslogtreecommitdiff
path: root/src/content/chapter4_standard_library/lesson02_result_module/code.gleam
blob: ec7039b7c1e4e644889170b3f80194230b6d94b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import gleam/int
import gleam/io
import gleam/result

pub fn main() {
  io.println("=== map ===")
  io.debug(result.map(Ok(1), fn(x) { x * 2 }))
  io.debug(result.map(Error(1), fn(x) { x * 2 }))

  io.println("=== try ===")
  io.debug(result.try(Ok("1"), int.parse))
  io.debug(result.try(Ok("no"), int.parse))
  io.debug(result.try(Error(Nil), int.parse))

  io.println("=== unwrap ===")
  io.debug(result.unwrap(Ok("1234"), "default"))
  io.debug(result.unwrap(Error(Nil), "default"))

  io.println("=== pipeline ===")
  int.parse("-1234")
  |> result.map(int.absolute_value)
  |> result.try(int.remainder(_, 42))
  |> io.debug
}