Skip to content

Commit

Permalink
More content
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Dec 26, 2023
1 parent 9f22423 commit 02027bc
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/content/chapter3_data_types/lesson06_results/code.gleam
Original file line number Diff line number Diff line change
@@ -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))
}
}
37 changes: 37 additions & 0 deletions src/content/chapter3_data_types/lesson06_results/text.html
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import gleam/io

pub fn main() {
io.println("Hello, Joe!")
io.println("Hello, Mike!")
}
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 02027bc

Please sign in to comment.