aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/content/chapter3_data_types/lesson06_results/code.gleam25
-rw-r--r--src/content/chapter3_data_types/lesson06_results/text.html37
-rw-r--r--src/content/chapter3_data_types/lesson07_bit_arrays/code.gleam (renamed from src/content/chapter3_data_types/lesson06_bit_arrays/code.gleam)0
-rw-r--r--src/content/chapter3_data_types/lesson07_bit_arrays/text.html (renamed from src/content/chapter3_data_types/lesson06_bit_arrays/text.html)0
-rw-r--r--src/content/chapter4_standard_library/lesson00_standard_library_package/code.gleam6
-rw-r--r--src/content/chapter4_standard_library/lesson00_standard_library_package/text.html14
6 files changed, 82 insertions, 0 deletions
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 @@
+<p>
+ Gleam doesn't use exceptions, instead computations that can either succeed or
+ fail return a value of the built-in <code>Result(value, error)</code> type. It
+ has two variants:
+</p>
+<ul>
+ <li>
+ <code>Ok</code>, which contains the return value of a successful
+ computation.
+ </li>
+ <li>
+ <code>Error</code>, which contains the reason for a failed computation.
+ </li>
+</ul>
+<p>
+ 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.
+</p>
+<p>
+ 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.
+</p>
+<p>
+ 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!
+</p>
+<p>
+ Result value can be handled by pattern matching with a
+ <code>case</code> expression, but given how frequently results are returned
+ this can become unwieldy. Gleam code commonly uses the
+ <code>gleam/result</code> standard library module and
+ <code>use</code> expressions when working with results, both of which will be
+ covered in later chapters.
+</p>
diff --git a/src/content/chapter3_data_types/lesson06_bit_arrays/code.gleam b/src/content/chapter3_data_types/lesson07_bit_arrays/code.gleam
index dc772ca..dc772ca 100644
--- a/src/content/chapter3_data_types/lesson06_bit_arrays/code.gleam
+++ b/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
index 3214db1..3214db1 100644
--- a/src/content/chapter3_data_types/lesson06_bit_arrays/text.html
+++ b/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 @@
+<p>
+ The Gleam standard library is a regular Gleam package that has been published
+ to the <a href="https://hex.pm">Hex</a> package repository. You could opt to
+ not use if you wish, though almost all Gleam projects depend on it.
+</p>
+<p>
+ All of the modules imported so far in this guide, such as
+ <code>gleam/io</code>, are from the standard library.
+</p>
+<p>
+ All of the documentation for the standard library is available on
+ <a href="https://hexdocs.pm/gleam_stdlib/">HexDocs</a>. We will go over some
+ of the most commonly used modules now.
+</p>