-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import gleam/io | ||
import gleam/int | ||
|
||
pub fn main() { | ||
let result = case int.random(0, 5) { | ||
0 -> "It's zero!" | ||
other -> "It's " <> int.to_string(other) | ||
} | ||
io.debug(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<p> | ||
Patterns in case expressions can also assign variables. | ||
</p> | ||
<p> | ||
When a variable name is used in a pattern the value that is matched against is | ||
assigned to that name, and can be used in the body of that clause. | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import gleam/io | ||
import gleam/int | ||
|
||
pub fn main() { | ||
let x = int.random(0, 5) | ||
io.debug(x) | ||
|
||
let result = case x { | ||
// Match specific values | ||
0 -> "Zero" | ||
1 -> "One" | ||
// Match any other value | ||
_ -> "Other" | ||
} | ||
io.debug(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<p> | ||
The case expression is the most common kind of flow control in Gleam code. It | ||
is similar to `switch` in some other languages, but more powerful than most. | ||
</p> | ||
<p> | ||
It allows the programmer to say "if the data has this shape then run this | ||
code", a process called called <em>pattern matching</em>. | ||
</p> | ||
<p> | ||
Gleam performs <em>exhaustiveness checking</em> to ensure that the patterns in | ||
a case expression cover all possible values. With this you can have confidence | ||
that your logic is up-to-date for the design of the data you are working with. | ||
</p> | ||
<p> | ||
Try commenting out patterns or adding new redundant ones, and see what | ||
problems the compiler reports. | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import gleam/io | ||
|
||
pub fn main() { | ||
io.debug(get_name("Hello, Joe")) | ||
io.debug(get_name("Hello, Mike")) | ||
io.debug(get_name("System still working?")) | ||
} | ||
|
||
fn get_name(x: String) -> String { | ||
case x { | ||
"Hello, " <> name -> name | ||
_ -> "Unknown" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<p> | ||
When pattern matching on strings the <code><></code> operator can be | ||
used to match on strings with a specific prefix. | ||
</p> | ||
<p> | ||
The pattern <code>"hello " <> name</code> matches any string that starts with | ||
<code>"hello "</code> and asigns the rest of the string to the variable | ||
<code>name</code>. | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import gleam/io | ||
import gleam/int | ||
import gleam/list | ||
|
||
pub fn main() { | ||
let x = list.repeat(int.random(0, 5), times: int.random(0, 3)) | ||
io.debug(x) | ||
|
||
let result = case x { | ||
[] -> "Empty list" | ||
[1] -> "List of just 1" | ||
[4, ..] -> "List starting with 4" | ||
[_, _] -> "List of 2 elements" | ||
_ -> "Some other list" | ||
} | ||
io.debug(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<p> | ||
Lists and the values they contain can be pattern matched on in case | ||
expressions. | ||
</p> | ||
<p> | ||
List patterns match on specific lengths of lists. The pattern <code>[]</code> | ||
matches an empty list, and the pattern <code>[_]</code> matches a list with | ||
one element. They will not match on lists with other lengths. | ||
</p> | ||
<p> | ||
The spread pattern <code>..</code> can be used to match the rest of the list. | ||
The pattern <code>[1, ..]</code> matches any list that starts with | ||
<code>1</code>. The pattern <code>[_, _, ..]</code> matches any list that has | ||
at least two elements. | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import gleam/io | ||
|
||
pub fn main() { | ||
let sum = sum_list([18, 56, 35, 85, 91], 0) | ||
io.debug(sum) | ||
} | ||
|
||
fn sum_list(list: List(Int), total: Int) -> Int { | ||
case list { | ||
[first, ..rest] -> sum_list(rest, total + first) | ||
[] -> total | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<p> | ||
Most commonly functions in the | ||
<a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html"><code>gleam/list</code></a> | ||
module are used to iterate across a list, but at times you may prefer | ||
to work with the list directly. | ||
</p> | ||
<p> | ||
Gleam doesn't have a looping syntax, instead iteration is done through | ||
recursion and pattern matching. | ||
</p> | ||
<p> | ||
The <code>[first, ..rest]</code> pattern matches on a list with at least one | ||
element, assigning the first element to the variable <code>first</code> and | ||
the rest of the list to the variable <code>rest</code>. | ||
By using this pattern and a pattern for the empty list <code>[]</code> a | ||
function can run code on each element of a list until the end is reached. | ||
</p> | ||
<p> | ||
This code sums a list by recursing over the list and adding each int to a | ||
<code>total</code> argument, returning it when the end is reached. | ||
</p> | ||
|