aboutsummaryrefslogtreecommitdiff
path: root/src/content/chapter4_standard_library/lesson02_result_module
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2024-01-03 19:48:25 +0000
committerLouis Pilfold <louis@lpil.uk>2024-01-03 19:48:25 +0000
commit51a1fcbd0e42b25238877d3ad05d1690e8bc1553 (patch)
tree36e8f8ad1ace4874a9c7515a8d381c5616f52d46 /src/content/chapter4_standard_library/lesson02_result_module
parentf3eaea54080300a85e7acb63a38b0bf5f7c6b531 (diff)
downloadtour-51a1fcbd0e42b25238877d3ad05d1690e8bc1553.tar.gz
tour-51a1fcbd0e42b25238877d3ad05d1690e8bc1553.zip
More stdlib
Diffstat (limited to 'src/content/chapter4_standard_library/lesson02_result_module')
-rw-r--r--src/content/chapter4_standard_library/lesson02_result_module/code.gleam24
-rw-r--r--src/content/chapter4_standard_library/lesson02_result_module/text.html40
2 files changed, 64 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
+}
diff --git a/src/content/chapter4_standard_library/lesson02_result_module/text.html b/src/content/chapter4_standard_library/lesson02_result_module/text.html
new file mode 100644
index 0000000..4901afd
--- /dev/null
+++ b/src/content/chapter4_standard_library/lesson02_result_module/text.html
@@ -0,0 +1,40 @@
+<p>
+ The
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/result.html"
+ ><code>gleam/result</code></a
+ >
+ standard library module contains functions for working with results. Gleam
+ programs will make heavy use of this module to avoid excessive nested case
+ expressions when calling multiple functions that can fail.
+</p>
+
+<p>
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/result.html#map"
+ ><code>map</code></a
+ >
+ updates a value held within the Ok of a result by calling a given function on
+ it. If the result is an error then the function is not called.
+</p>
+
+<p>
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/result.html#try"
+ ><code>try</code></a
+ >
+ runs a result returning function on the value held within an Ok of a result.
+ If the result is an error then the function is not called. This is useful for
+ chaining together multiple function calls that can fail, one after the other,
+ stopping at the first error.
+</p>
+
+<p>
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/result.html#unwrap"
+ ><code>unwrap</code></a
+ >
+ extracts the success value from a result, or returning a default value if the
+ result is an error.
+</p>
+
+<p>
+ Result functions are often used with pipelines to chain together multiple
+ calls to result returning functions.
+</p>