diff --git a/src/content/chapter3_data_types/lesson06_results/code.gleam b/src/content/chapter3_data_types/lesson06_results/code.gleam new file mode 100644 index 0000000..ba7d5d8 --- /dev/null +++ b/src/content/chapter3_data_types/lesson06_results/code.gleam @@ -0,0 +1,25 @@ +import gleam/io +import gleam/int + +pub fn main() { + io.debug(buy_pastry(10)) + io.debug(buy_pastry(8)) + io.debug(buy_pastry(5)) + io.debug(buy_pastry(3)) +} + +pub type Error { + NotEnoughMoney(required: Int) + NotLuckyEnough +} + +fn buy_pastry(money: Int) -> Result(Int, Error) { + case money >= 5 { + True -> + case int.random(4) == 0 { + True -> Error(NotLuckyEnough) + False -> Ok(money - 5) + } + False -> Error(NotEnoughMoney(required: 5)) + } +} diff --git a/src/content/chapter3_data_types/lesson06_results/text.html b/src/content/chapter3_data_types/lesson06_results/text.html new file mode 100644 index 0000000..fe0f7ba --- /dev/null +++ b/src/content/chapter3_data_types/lesson06_results/text.html @@ -0,0 +1,37 @@ +

+ Gleam doesn't use exceptions, instead computations that can either succeed or + fail return a value of the built-in Result(value, error) type. It + has two variants: +

+ +

+ The type is generic with two type parameters, one for the success value and + one for the error. With these the result can hold any type for success and + failure. +

+

+ Commonly Gleam programs and library will define custom types with a variant + for each possible problem that can arise, along with any error information + that would be useful to the programmer. +

+

+ This is advantageous over exceptions as you can immediately see what if any + errors a function can return, and the compiler will ensure they are handled. + No nasty surprises with unexpected exceptions! +

+

+ Result value can be handled by pattern matching with a + case expression, but given how frequently results are returned + this can become unwieldy. Gleam code commonly uses the + gleam/result standard library module and + use expressions when working with results, both of which will be + covered in later chapters. +

diff --git a/src/content/chapter3_data_types/lesson06_bit_arrays/code.gleam b/src/content/chapter3_data_types/lesson07_bit_arrays/code.gleam similarity index 100% rename from src/content/chapter3_data_types/lesson06_bit_arrays/code.gleam rename to src/content/chapter3_data_types/lesson07_bit_arrays/code.gleam diff --git a/src/content/chapter3_data_types/lesson06_bit_arrays/text.html b/src/content/chapter3_data_types/lesson07_bit_arrays/text.html similarity index 100% rename from src/content/chapter3_data_types/lesson06_bit_arrays/text.html rename to src/content/chapter3_data_types/lesson07_bit_arrays/text.html diff --git a/src/content/chapter4_standard_library/lesson00_standard_library_package/code.gleam b/src/content/chapter4_standard_library/lesson00_standard_library_package/code.gleam new file mode 100644 index 0000000..a842430 --- /dev/null +++ b/src/content/chapter4_standard_library/lesson00_standard_library_package/code.gleam @@ -0,0 +1,6 @@ +import gleam/io + +pub fn main() { + io.println("Hello, Joe!") + io.println("Hello, Mike!") +} diff --git a/src/content/chapter4_standard_library/lesson00_standard_library_package/text.html b/src/content/chapter4_standard_library/lesson00_standard_library_package/text.html new file mode 100644 index 0000000..f5d7505 --- /dev/null +++ b/src/content/chapter4_standard_library/lesson00_standard_library_package/text.html @@ -0,0 +1,14 @@ +

+ The Gleam standard library is a regular Gleam package that has been published + to the Hex package repository. You could opt to + not use if you wish, though almost all Gleam projects depend on it. +

+

+ All of the modules imported so far in this guide, such as + gleam/io, are from the standard library. +

+

+ All of the documentation for the standard library is available on + HexDocs. We will go over some + of the most commonly used modules now. +