aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/util/runner.gleam
blob: 0ff0a41404322d5d919ac740f792451638554c6f (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
25
26
27
28
29
30
31
32
import gleam/list
import gleam/int
import gleam/io
import gleam/result
import gleam/erlang.{start_arguments}

fn get_day(handler: fn(Int) -> Nil) -> Result(Nil, String) {
  let args = start_arguments()

  use first <- result.then(
    args
    |> list.first()
    |> result.replace_error("Pass the day as first argument!"),
  )

  use day <- result.then(
    first
    |> int.parse()
    |> result.replace_error("The day argument must be a number!"),
  )

  handler(day)

  Ok(Nil)
}

pub fn with_day(handler: fn(Int) -> Nil) -> Nil {
  handler
  |> get_day()
  |> result.map_error(io.println)
  |> result.unwrap(Nil)
}