diff options
author | Louis Pilfold <louis@lpil.uk> | 2024-01-03 19:48:25 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-01-03 19:48:25 +0000 |
commit | 51a1fcbd0e42b25238877d3ad05d1690e8bc1553 (patch) | |
tree | 36e8f8ad1ace4874a9c7515a8d381c5616f52d46 /src/content/chapter4_standard_library/lesson02_result_module/code.gleam | |
parent | f3eaea54080300a85e7acb63a38b0bf5f7c6b531 (diff) | |
download | tour-51a1fcbd0e42b25238877d3ad05d1690e8bc1553.tar.gz tour-51a1fcbd0e42b25238877d3ad05d1690e8bc1553.zip |
More stdlib
Diffstat (limited to 'src/content/chapter4_standard_library/lesson02_result_module/code.gleam')
-rw-r--r-- | src/content/chapter4_standard_library/lesson02_result_module/code.gleam | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/content/chapter4_standard_library/lesson02_result_module/code.gleam b/src/content/chapter4_standard_library/lesson02_result_module/code.gleam new file mode 100644 index 0000000..8772788 --- /dev/null +++ b/src/content/chapter4_standard_library/lesson02_result_module/code.gleam @@ -0,0 +1,24 @@ +import gleam/io +import gleam/int +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 +} |