From 51a1fcbd0e42b25238877d3ad05d1690e8bc1553 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Wed, 3 Jan 2024 19:48:25 +0000 Subject: More stdlib --- .../lesson14_list_functions/code.gleam | 15 -------- .../lesson14_list_functions/text.html | 25 ------------- .../lesson01_list_module/code.gleam | 19 ++++++++++ .../lesson01_list_module/text.html | 42 ++++++++++++++++++++++ .../lesson02_result_module/code.gleam | 24 +++++++++++++ .../lesson02_result_module/text.html | 40 +++++++++++++++++++++ .../lesson03_dict_module/code.gleam | 14 ++++++++ .../lesson03_dict_module/text.html | 40 +++++++++++++++++++++ .../lesson04_option_module/code.gleam | 14 ++++++++ .../lesson04_option_module/text.html | 16 +++++++++ 10 files changed, 209 insertions(+), 40 deletions(-) delete mode 100644 src/content/chapter0_basics/lesson14_list_functions/code.gleam delete mode 100644 src/content/chapter0_basics/lesson14_list_functions/text.html create mode 100644 src/content/chapter4_standard_library/lesson01_list_module/code.gleam create mode 100644 src/content/chapter4_standard_library/lesson01_list_module/text.html create mode 100644 src/content/chapter4_standard_library/lesson02_result_module/code.gleam create mode 100644 src/content/chapter4_standard_library/lesson02_result_module/text.html create mode 100644 src/content/chapter4_standard_library/lesson03_dict_module/code.gleam create mode 100644 src/content/chapter4_standard_library/lesson03_dict_module/text.html create mode 100644 src/content/chapter4_standard_library/lesson04_option_module/code.gleam create mode 100644 src/content/chapter4_standard_library/lesson04_option_module/text.html (limited to 'src') 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/chapter0_basics/lesson14_list_functions/text.html b/src/content/chapter0_basics/lesson14_list_functions/text.html deleted file mode 100644 index e143654..0000000 --- a/src/content/chapter0_basics/lesson14_list_functions/text.html +++ /dev/null @@ -1,25 +0,0 @@ -

- The gleam/list - standard library module contains functions for working with lists. A Gleam - program will likely make heavy use of this module. -

- -

- map - makes a new list by running a function on each element in a list. -

-

- filter - makes a new list containing only the elements for which a function returns - true. -

-

- fold - 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. -

-

- It's worth getting familiar with all the functions in this module when writing - Gleam code. -

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/chapter4_standard_library/lesson01_list_module/text.html b/src/content/chapter4_standard_library/lesson01_list_module/text.html new file mode 100644 index 0000000..a0bfb5b --- /dev/null +++ b/src/content/chapter4_standard_library/lesson01_list_module/text.html @@ -0,0 +1,42 @@ +

+ The + gleam/list + standard library module contains functions for working with lists. A Gleam + program will likely make heavy use of this module, the various functions + serving as differnt types of loops over lists. +

+ +

+ map + makes a new list by running a function on each element in a list. +

+

+ filter + makes a new list containing only the elements for which a function returns + true. +

+

+ fold + 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. +

+

+ find + returns the first element in a list for which a function returns + True. +

+

+ It's worth getting familiar with all the functions in this module when writing + Gleam code, you'll be using them a lot! +

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 @@ +

+ The + gleam/result + 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. +

+ +

+ map + 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. +

+ +

+ try + 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. +

+ +

+ unwrap + extracts the success value from a result, or returning a default value if the + result is an error. +

+ +

+ Result functions are often used with pipelines to chain together multiple + calls to result returning functions. +

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 @@ +

+ The + gleam/dict + standard library module defines Gleam's Dict 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. +

+ +

+ new + and + new + can be used to create new dicts. +

+ +

+ insert + and + delete + are used to add and remove items from a dict. +

+

+ Like lists, dicts are immutable. Inserting or deleting an item from a dict + will return a new dict with the item added or removed. +

+

+ 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. +

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 @@ +

+ Values in Gleam are not nullable, so the + gleam/option + standard library module defines Gleam's Oction type, which can be + used to represent a value that is either present or absent. +

+ +

+ 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. +

-- cgit v1.2.3