diff options
Diffstat (limited to 'aoc-2020-gleam/src/util/runner.gleam')
-rw-r--r-- | aoc-2020-gleam/src/util/runner.gleam | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/aoc-2020-gleam/src/util/runner.gleam b/aoc-2020-gleam/src/util/runner.gleam new file mode 100644 index 0000000..0ff0a41 --- /dev/null +++ b/aoc-2020-gleam/src/util/runner.gleam @@ -0,0 +1,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) +} |