From 4efb34bd728732101432843ed0bfbeb971272287 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Thu, 21 Dec 2023 14:03:41 +0000 Subject: Add chapters --- .../lesson01_case_expressions/code.gleam | 10 ++++++++++ .../lesson01_case_expressions/text.html | 7 +++++++ .../lesson02_variable_patterns/code.gleam | 16 ++++++++++++++++ .../lesson02_variable_patterns/text.html | 17 +++++++++++++++++ .../lesson03_string_patterns/code.gleam | 14 ++++++++++++++ .../lesson03_string_patterns/text.html | 9 +++++++++ .../lesson04_list_patterns/code.gleam | 17 +++++++++++++++++ .../lesson04_list_patterns/text.html | 15 +++++++++++++++ .../lesson05_list_recursion/code.gleam | 13 +++++++++++++ .../lesson05_list_recursion/text.html | 22 ++++++++++++++++++++++ .../lesson06_multiple_subjects/code.gleam | 17 +++++++++++++++++ .../lesson06_multiple_subjects/text.html | 13 +++++++++++++ .../lesson07_alternative_patterns/code.gleam | 14 ++++++++++++++ .../lesson07_alternative_patterns/text.html | 17 +++++++++++++++++ .../lesson08_pattern_aliases/code.gleam | 15 +++++++++++++++ .../lesson08_pattern_aliases/text.html | 7 +++++++ 16 files changed, 223 insertions(+) create mode 100644 src/content/chapter2_flow_control/lesson01_case_expressions/code.gleam create mode 100644 src/content/chapter2_flow_control/lesson01_case_expressions/text.html create mode 100644 src/content/chapter2_flow_control/lesson02_variable_patterns/code.gleam create mode 100644 src/content/chapter2_flow_control/lesson02_variable_patterns/text.html create mode 100644 src/content/chapter2_flow_control/lesson03_string_patterns/code.gleam create mode 100644 src/content/chapter2_flow_control/lesson03_string_patterns/text.html create mode 100644 src/content/chapter2_flow_control/lesson04_list_patterns/code.gleam create mode 100644 src/content/chapter2_flow_control/lesson04_list_patterns/text.html create mode 100644 src/content/chapter2_flow_control/lesson05_list_recursion/code.gleam create mode 100644 src/content/chapter2_flow_control/lesson05_list_recursion/text.html create mode 100644 src/content/chapter2_flow_control/lesson06_multiple_subjects/code.gleam create mode 100644 src/content/chapter2_flow_control/lesson06_multiple_subjects/text.html create mode 100644 src/content/chapter2_flow_control/lesson07_alternative_patterns/code.gleam create mode 100644 src/content/chapter2_flow_control/lesson07_alternative_patterns/text.html create mode 100644 src/content/chapter2_flow_control/lesson08_pattern_aliases/code.gleam create mode 100644 src/content/chapter2_flow_control/lesson08_pattern_aliases/text.html (limited to 'src/content/chapter2_flow_control') diff --git a/src/content/chapter2_flow_control/lesson01_case_expressions/code.gleam b/src/content/chapter2_flow_control/lesson01_case_expressions/code.gleam new file mode 100644 index 0000000..6e1ed46 --- /dev/null +++ b/src/content/chapter2_flow_control/lesson01_case_expressions/code.gleam @@ -0,0 +1,10 @@ +import gleam/io +import gleam/int + +pub fn main() { + let result = case int.random(5) { + 0 -> "It's zero!" + other -> "It's " <> int.to_string(other) + } + io.debug(result) +} diff --git a/src/content/chapter2_flow_control/lesson01_case_expressions/text.html b/src/content/chapter2_flow_control/lesson01_case_expressions/text.html new file mode 100644 index 0000000..7e9ac11 --- /dev/null +++ b/src/content/chapter2_flow_control/lesson01_case_expressions/text.html @@ -0,0 +1,7 @@ +

+ Patterns in case expressions can also assign variables. +

+

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

diff --git a/src/content/chapter2_flow_control/lesson02_variable_patterns/code.gleam b/src/content/chapter2_flow_control/lesson02_variable_patterns/code.gleam new file mode 100644 index 0000000..7bcc93c --- /dev/null +++ b/src/content/chapter2_flow_control/lesson02_variable_patterns/code.gleam @@ -0,0 +1,16 @@ +import gleam/io +import gleam/int + +pub fn main() { + let x = int.random(5) + io.debug(x) + + let result = case x { + // Match specific values + 0 -> "Zero" + 1 -> "One" + // Match any other value + _ -> "Other" + } + io.debug(result) +} diff --git a/src/content/chapter2_flow_control/lesson02_variable_patterns/text.html b/src/content/chapter2_flow_control/lesson02_variable_patterns/text.html new file mode 100644 index 0000000..8154979 --- /dev/null +++ b/src/content/chapter2_flow_control/lesson02_variable_patterns/text.html @@ -0,0 +1,17 @@ +

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

+

+ It allows the programmer to say "if the data has this shape then run this + code", a process called called pattern matching. +

+

+ Gleam performs exhaustiveness checking 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. +

+

+ Try commenting out patterns or adding new redundant ones, and see what + problems the compiler reports. +

diff --git a/src/content/chapter2_flow_control/lesson03_string_patterns/code.gleam b/src/content/chapter2_flow_control/lesson03_string_patterns/code.gleam new file mode 100644 index 0000000..d1441a0 --- /dev/null +++ b/src/content/chapter2_flow_control/lesson03_string_patterns/code.gleam @@ -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" + } +} diff --git a/src/content/chapter2_flow_control/lesson03_string_patterns/text.html b/src/content/chapter2_flow_control/lesson03_string_patterns/text.html new file mode 100644 index 0000000..0dd3274 --- /dev/null +++ b/src/content/chapter2_flow_control/lesson03_string_patterns/text.html @@ -0,0 +1,9 @@ +

+ When pattern matching on strings the <> operator can be + used to match on strings with a specific prefix. +

+

+ The pattern "hello " <> name matches any string that starts with + "hello " and asigns the rest of the string to the variable + name. +

diff --git a/src/content/chapter2_flow_control/lesson04_list_patterns/code.gleam b/src/content/chapter2_flow_control/lesson04_list_patterns/code.gleam new file mode 100644 index 0000000..e767d20 --- /dev/null +++ b/src/content/chapter2_flow_control/lesson04_list_patterns/code.gleam @@ -0,0 +1,17 @@ +import gleam/io +import gleam/int +import gleam/list + +pub fn main() { + let x = list.repeat(int.random(5), times: int.random(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) +} diff --git a/src/content/chapter2_flow_control/lesson04_list_patterns/text.html b/src/content/chapter2_flow_control/lesson04_list_patterns/text.html new file mode 100644 index 0000000..de55eef --- /dev/null +++ b/src/content/chapter2_flow_control/lesson04_list_patterns/text.html @@ -0,0 +1,15 @@ +

+ Lists and the values they contain can be pattern matched on in case + expressions. +

+

+ List patterns match on specific lengths of lists. The pattern [] + matches an empty list, and the pattern [_] matches a list with + one element. They will not match on lists with other lengths. +

+

+ The spread pattern .. can be used to match the rest of the list. + The pattern [1, ..] matches any list that starts with + 1. The pattern [_, _, ..] matches any list that has + at least two elements. +

diff --git a/src/content/chapter2_flow_control/lesson05_list_recursion/code.gleam b/src/content/chapter2_flow_control/lesson05_list_recursion/code.gleam new file mode 100644 index 0000000..370675a --- /dev/null +++ b/src/content/chapter2_flow_control/lesson05_list_recursion/code.gleam @@ -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 + } +} diff --git a/src/content/chapter2_flow_control/lesson05_list_recursion/text.html b/src/content/chapter2_flow_control/lesson05_list_recursion/text.html new file mode 100644 index 0000000..7f2351d --- /dev/null +++ b/src/content/chapter2_flow_control/lesson05_list_recursion/text.html @@ -0,0 +1,22 @@ +

+ Most commonly functions in the + gleam/list + module are used to iterate across a list, but at times you may prefer + to work with the list directly. +

+

+ Gleam doesn't have a looping syntax, instead iteration is done through + recursion and pattern matching. +

+

+ The [first, ..rest] pattern matches on a list with at least one + element, assigning the first element to the variable first and + the rest of the list to the variable rest. + By using this pattern and a pattern for the empty list [] a + function can run code on each element of a list until the end is reached. +

+

+ This code sums a list by recursing over the list and adding each int to a + total argument, returning it when the end is reached. +

+ diff --git a/src/content/chapter2_flow_control/lesson06_multiple_subjects/code.gleam b/src/content/chapter2_flow_control/lesson06_multiple_subjects/code.gleam new file mode 100644 index 0000000..d7aa34a --- /dev/null +++ b/src/content/chapter2_flow_control/lesson06_multiple_subjects/code.gleam @@ -0,0 +1,17 @@ +import gleam/io +import gleam/int + +pub fn main() { + let x = int.random(2) + let y = int.random(2) + io.debug(x) + io.debug(y) + + let result = case x, y { + 0, 0 -> "Both are zero" + 0, _ -> "First is zero" + _, 0 -> "Second is zero" + _, _ -> "Neither are zero" + } + io.debug(result) +} diff --git a/src/content/chapter2_flow_control/lesson06_multiple_subjects/text.html b/src/content/chapter2_flow_control/lesson06_multiple_subjects/text.html new file mode 100644 index 0000000..26a7ea3 --- /dev/null +++ b/src/content/chapter2_flow_control/lesson06_multiple_subjects/text.html @@ -0,0 +1,13 @@ +

+ Sometimes it is useful to pattern match on multiple values at the same time in + one case experession. +

+

+ To do this you can give multiple subjects and multiple patterns, separated + commas. +

+

+ When matching on multiple subjects there must be the same number of patterns + as there are subjects. Try removing one of the _, sub-patterns to + see the compile time error that is returned. +

diff --git a/src/content/chapter2_flow_control/lesson07_alternative_patterns/code.gleam b/src/content/chapter2_flow_control/lesson07_alternative_patterns/code.gleam new file mode 100644 index 0000000..06a6562 --- /dev/null +++ b/src/content/chapter2_flow_control/lesson07_alternative_patterns/code.gleam @@ -0,0 +1,14 @@ +import gleam/io +import gleam/int + +pub fn main() { + let number = int.random(10) + io.debug(number) + + let result = case number { + 2 | 4 | 6 | 8 -> "This is an even number" + 1 | 3 | 5 | 7 -> "This is an odd number" + _ -> "I'm not sure" + } + io.debug(result) +} diff --git a/src/content/chapter2_flow_control/lesson07_alternative_patterns/text.html b/src/content/chapter2_flow_control/lesson07_alternative_patterns/text.html new file mode 100644 index 0000000..10ad731 --- /dev/null +++ b/src/content/chapter2_flow_control/lesson07_alternative_patterns/text.html @@ -0,0 +1,17 @@ +

+ Alternative patterns can be given for a case clause using the + | operator. If any of the patterns match then the clause matches. +

+

+ When matching on multiple subjects there must be the same number of patterns + as there are subjects. Try removing one of the _, sub-patterns to + see the compile time error that is returned. +

+

+ If a pattern defines a variable then all of the alternative patterns for that + clause must also define a variable with the same name and same type. +

+

+ Currently it is not possible to have nested alternative patterns, so the + pattern [1 | 2 | 3] is not valid. +

diff --git a/src/content/chapter2_flow_control/lesson08_pattern_aliases/code.gleam b/src/content/chapter2_flow_control/lesson08_pattern_aliases/code.gleam new file mode 100644 index 0000000..ee40a26 --- /dev/null +++ b/src/content/chapter2_flow_control/lesson08_pattern_aliases/code.gleam @@ -0,0 +1,15 @@ +import gleam/io + +pub fn main() { + io.debug(get_first_non_empty([[], [1, 2, 3], [4, 5]])) + io.debug(get_first_non_empty([[1, 2], [3, 4, 5], []])) + io.debug(get_first_non_empty([[], [], []])) +} + +fn get_first_non_empty(lists: List(List(t))) -> List(t) { + case lists { + [[_, ..] as first, ..] -> first + [_, ..rest] -> get_first_non_empty(rest) + [] -> [] + } +} diff --git a/src/content/chapter2_flow_control/lesson08_pattern_aliases/text.html b/src/content/chapter2_flow_control/lesson08_pattern_aliases/text.html new file mode 100644 index 0000000..b737eb8 --- /dev/null +++ b/src/content/chapter2_flow_control/lesson08_pattern_aliases/text.html @@ -0,0 +1,7 @@ +

+ The as operator can be used to assign sub patterns to variables. +

+

+ The pattern [_, ..] as it will match any non-empty list and + assign that list to the variable it. +

-- cgit v1.2.3