aboutsummaryrefslogtreecommitdiff
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
parentf3eaea54080300a85e7acb63a38b0bf5f7c6b531 (diff)
downloadtour-51a1fcbd0e42b25238877d3ad05d1690e8bc1553.tar.gz
tour-51a1fcbd0e42b25238877d3ad05d1690e8bc1553.zip
More stdlib
-rw-r--r--src/content/chapter0_basics/lesson14_list_functions/code.gleam15
-rw-r--r--src/content/chapter4_standard_library/lesson01_list_module/code.gleam19
-rw-r--r--src/content/chapter4_standard_library/lesson01_list_module/text.html (renamed from src/content/chapter0_basics/lesson14_list_functions/text.html)29
-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
-rw-r--r--src/content/chapter4_standard_library/lesson03_dict_module/code.gleam14
-rw-r--r--src/content/chapter4_standard_library/lesson03_dict_module/text.html40
-rw-r--r--src/content/chapter4_standard_library/lesson04_option_module/code.gleam14
-rw-r--r--src/content/chapter4_standard_library/lesson04_option_module/text.html16
9 files changed, 190 insertions, 21 deletions
diff --git a/src/content/chapter0_basics/lesson14_list_functions/code.gleam b/src/content/chapter0_basics/lesson14_list_functions/code.gleam
deleted file mode 100644
index 92d1cc6..0000000
--- a/src/content/chapter0_basics/lesson14_list_functions/code.gleam
+++ /dev/null
@@ -1,15 +0,0 @@
-import gleam/io
-import gleam/list
-
-pub fn main() {
- let ints = [0, 1, 2, 3, 4, 5]
-
- let doubled = list.map(ints, fn(x) { x * 2 })
- io.debug(doubled)
-
- let even = list.filter(ints, fn(x) { x % 2 == 0 })
- io.debug(even)
-
- let total = list.fold(ints, 0, fn(count, e) { count + e })
- io.debug(total)
-}
diff --git a/src/content/chapter4_standard_library/lesson01_list_module/code.gleam b/src/content/chapter4_standard_library/lesson01_list_module/code.gleam
new file mode 100644
index 0000000..29f2448
--- /dev/null
+++ b/src/content/chapter4_standard_library/lesson01_list_module/code.gleam
@@ -0,0 +1,19 @@
+import gleam/io
+import gleam/list
+
+pub fn main() {
+ let ints = [0, 1, 2, 3, 4, 5]
+
+ io.println("=== map ===")
+ io.debug(list.map(ints, fn(x) { x * 2 }))
+
+ io.println("=== filter ===")
+ io.debug(list.filter(ints, fn(x) { x % 2 == 0 }))
+
+ io.println("=== fold ===")
+ io.debug(list.fold(ints, 0, fn(count, e) { count + e }))
+
+ io.println("=== find ===")
+ io.debug(list.find(ints, fn(x) { x > 3 }))
+ io.debug(list.find(ints, fn(x) { x > 13 }))
+}
diff --git a/src/content/chapter0_basics/lesson14_list_functions/text.html b/src/content/chapter4_standard_library/lesson01_list_module/text.html
index e143654..a0bfb5b 100644
--- a/src/content/chapter0_basics/lesson14_list_functions/text.html
+++ b/src/content/chapter4_standard_library/lesson01_list_module/text.html
@@ -1,25 +1,42 @@
<p>
- The <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html"><code>gleam/list</code></a>
+ The
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html"
+ ><code>gleam/list</code></a
+ >
standard library module contains functions for working with lists. A Gleam
- program will likely make heavy use of this module.
+ program will likely make heavy use of this module, the various functions
+ serving as differnt types of loops over lists.
</p>
<p>
- <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html#map"><code>map</code></a>
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html#map"
+ ><code>map</code></a
+ >
makes a new list by running a function on each element in a list.
</p>
<p>
- <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html#filter"><code>filter</code></a>
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html#filter"
+ ><code>filter</code></a
+ >
makes a new list containing only the elements for which a function returns
true.
</p>
<p>
- <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html#fold"><code>fold</code></a>
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html#fold"
+ ><code>fold</code></a
+ >
combines all the elements in a list into a single value by running a function
left-to-right on each element, passing the result of the previous call to the
next call.
</p>
<p>
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html#find"
+ ><code>find</code></a
+ >
+ returns the first element in a list for which a function returns
+ <code>True</code>.
+</p>
+<p>
It's worth getting familiar with all the functions in this module when writing
- Gleam code.
+ Gleam code, you'll be using them a lot!
</p>
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>
diff --git a/src/content/chapter4_standard_library/lesson03_dict_module/code.gleam b/src/content/chapter4_standard_library/lesson03_dict_module/code.gleam
new file mode 100644
index 0000000..02c72c0
--- /dev/null
+++ b/src/content/chapter4_standard_library/lesson03_dict_module/code.gleam
@@ -0,0 +1,14 @@
+import gleam/io
+import gleam/dict
+
+pub fn main() {
+ let scores = dict.from_list([#("Lucy", 13), #("Drew", 15)])
+ io.debug(scores)
+
+ let scores =
+ scores
+ |> dict.insert("Bushra", 16)
+ |> dict.insert("Darius", 14)
+ |> dict.delete("Drew")
+ io.debug(scores)
+}
diff --git a/src/content/chapter4_standard_library/lesson03_dict_module/text.html b/src/content/chapter4_standard_library/lesson03_dict_module/text.html
new file mode 100644
index 0000000..5e60ba2
--- /dev/null
+++ b/src/content/chapter4_standard_library/lesson03_dict_module/text.html
@@ -0,0 +1,40 @@
+<p>
+ The
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/dict.html"
+ ><code>gleam/dict</code></a
+ >
+ standard library module defines Gleam's <code>Dict</code> type and functions
+ for working with it. A dict is a collection of keys and values which other
+ languages may call a hashmap or table.
+</p>
+
+<p>
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/dict.html#new"
+ ><code>new</code></a
+ >
+ and
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/dict.html#from_list"
+ ><code>new</code></a
+ >
+ can be used to create new dicts.
+</p>
+
+<p>
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/dict.html#insert"
+ ><code>insert</code></a
+ >
+ and
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/dict.html#delete"
+ ><code>delete</code></a
+ >
+ are used to add and remove items from a dict.
+</p>
+<p>
+ Like lists, dicts are immutable. Inserting or deleting an item from a dict
+ will return a new dict with the item added or removed.
+</p>
+<p>
+ Dicts are unordered! If it appears that the items in a dict are in a certain
+ order this is incidental and should not be relied upon. Any ordering may
+ change without warning in future versions or on different runtimes.
+</p>
diff --git a/src/content/chapter4_standard_library/lesson04_option_module/code.gleam b/src/content/chapter4_standard_library/lesson04_option_module/code.gleam
new file mode 100644
index 0000000..eb60001
--- /dev/null
+++ b/src/content/chapter4_standard_library/lesson04_option_module/code.gleam
@@ -0,0 +1,14 @@
+import gleam/io
+import gleam/option.{type Option, None, Some}
+
+pub type Person {
+ Person(name: String, pet: Option(String))
+}
+
+pub fn main() {
+ let person_with_pet = Person("Al", Some("Nubi"))
+ let person_without_pet = Person("Maria", None)
+
+ io.debug(person_with_pet)
+ io.debug(person_without_pet)
+}
diff --git a/src/content/chapter4_standard_library/lesson04_option_module/text.html b/src/content/chapter4_standard_library/lesson04_option_module/text.html
new file mode 100644
index 0000000..881ea94
--- /dev/null
+++ b/src/content/chapter4_standard_library/lesson04_option_module/text.html
@@ -0,0 +1,16 @@
+<p>
+ Values in Gleam are not nullable, so the
+ <a href="https://hexdocs.pm/gleam_stdlib/gleam/option.html"
+ ><code>gleam/option</code></a
+ >
+ standard library module defines Gleam's <code>Oction</code> type, which can be
+ used to represent a value that is either present or absent.
+</p>
+
+<p>
+ The option type is very similar to the result type, but it does not have an
+ error value. Some languages have functions return an option when there is no
+ extra error detail to give, but Gleam always uses result. This makes all
+ failible functions consistent and removes any boilerplate that would be
+ required when mixing functions that use each type.
+</p>