diff options
author | Louis Pilfold <louis@lpil.uk> | 2024-01-18 16:44:27 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-01-18 16:44:27 +0000 |
commit | f92661980deac22b54e79cd44c25caba17910c95 (patch) | |
tree | 165923d00aab8e7a5d5944d388dbb182c6d57b51 /src | |
parent | e35f26eb4a034aad8a531986bf876075ffb02e3c (diff) | |
download | tour-f92661980deac22b54e79cd44c25caba17910c95.tar.gz tour-f92661980deac22b54e79cd44c25caba17910c95.zip |
Last of the content!
Diffstat (limited to 'src')
-rw-r--r-- | src/content/chapter1_functions/lesson01_recursion/code.gleam | 17 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson01_recursion/text.html | 20 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson02_tail_calls/code.gleam | 21 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson02_tail_calls/text.html | 23 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson03_higher_order_functions/code.gleam (renamed from src/content/chapter1_functions/lesson01_higher_order_functions/code.gleam) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson03_higher_order_functions/text.html (renamed from src/content/chapter1_functions/lesson01_higher_order_functions/text.html) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson04_anonymous_functions/code.gleam (renamed from src/content/chapter1_functions/lesson02_anonymous_functions/code.gleam) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson04_anonymous_functions/text.html (renamed from src/content/chapter1_functions/lesson02_anonymous_functions/text.html) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson05_function_captures/code.gleam (renamed from src/content/chapter1_functions/lesson03_function_captures/code.gleam) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson05_function_captures/text.html (renamed from src/content/chapter1_functions/lesson03_function_captures/text.html) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson06_generic_functions/code.gleam (renamed from src/content/chapter1_functions/lesson04_generic_functions/code.gleam) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson06_generic_functions/text.html (renamed from src/content/chapter1_functions/lesson04_generic_functions/text.html) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson07_pipelines/code.gleam (renamed from src/content/chapter1_functions/lesson05_pipelines/code.gleam) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson07_pipelines/text.html (renamed from src/content/chapter1_functions/lesson05_pipelines/text.html) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson08_labelled_arguments/code.gleam (renamed from src/content/chapter1_functions/lesson06_labelled_arguments/code.gleam) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson08_labelled_arguments/text.html (renamed from src/content/chapter1_functions/lesson06_labelled_arguments/text.html) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson09_documentation_comments/code.gleam (renamed from src/content/chapter1_functions/lesson099_documentation_comments/code.gleam) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson09_documentation_comments/text.html (renamed from src/content/chapter1_functions/lesson099_documentation_comments/text.html) | 0 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson10_deprecations/code.gleam | 13 | ||||
-rw-r--r-- | src/content/chapter1_functions/lesson10_deprecations/text.html | 13 | ||||
-rw-r--r-- | src/content/chapter5_advance_features/lesson01_use_sugar/code.gleam (renamed from src/content/chapter5_advance_features/lesson01_use_continued/code.gleam) | 0 | ||||
-rw-r--r-- | src/content/chapter5_advance_features/lesson01_use_sugar/text.html (renamed from src/content/chapter5_advance_features/lesson01_use_continued/text.html) | 0 |
22 files changed, 107 insertions, 0 deletions
diff --git a/src/content/chapter1_functions/lesson01_recursion/code.gleam b/src/content/chapter1_functions/lesson01_recursion/code.gleam new file mode 100644 index 0000000..ee93a6f --- /dev/null +++ b/src/content/chapter1_functions/lesson01_recursion/code.gleam @@ -0,0 +1,17 @@ +import gleam/io + +pub fn main() { + io.debug(factorial(5)) + io.debug(factorial(7)) +} + +// A recursive functions that calculates factorial +pub fn factorial(x: Int) -> Int { + case x { + // Base case + 1 -> 1 + + // Recursive case + _ -> x * factorial(x - 1) + } +} diff --git a/src/content/chapter1_functions/lesson01_recursion/text.html b/src/content/chapter1_functions/lesson01_recursion/text.html new file mode 100644 index 0000000..f1585bb --- /dev/null +++ b/src/content/chapter1_functions/lesson01_recursion/text.html @@ -0,0 +1,20 @@ +<p> + Gleam doesn't have loops, instead iteration is done through recursion, that is + through top-level functions calling themselves with different arguments. +</p> +<p> + A recursive function needs to have at least one <em>base case</em> and at + least one <em>recursive case</em>. A base case returns a value without calling + the function again. A recursive case calls the function again with different + inputs, looping again. +</p> +<p> + The Gleam standard library has functions for various common looping patterns, + some of which will be introduced in later lessons, however for more complex + loops manual recursion is often the clearest way to write it. +</p> +<p> + Recursion can seem daunting or unclear at first if you are more familiar with + languages that have special looping features, but stick with it! With time + it'll become just as familiar and comfortable as any other way of iterating. +</p> diff --git a/src/content/chapter1_functions/lesson02_tail_calls/code.gleam b/src/content/chapter1_functions/lesson02_tail_calls/code.gleam new file mode 100644 index 0000000..d823eec --- /dev/null +++ b/src/content/chapter1_functions/lesson02_tail_calls/code.gleam @@ -0,0 +1,21 @@ +import gleam/io + +pub fn main() { + io.debug(factorial(5)) + io.debug(factorial(7)) +} + +pub fn factorial(x: Int) -> Int { + // The public function calls the private tail recursive function + factorial_loop(x, 1) +} + +fn factorial_loop(x: Int, accumulator: Int) -> Int { + case x { + 1 -> accumulator + + // The last thing this function does is call itself + // In the previous lesson the last thing it did was multiple two ints + _ -> factorial_loop(x - 1, accumulator * x) + } +} diff --git a/src/content/chapter1_functions/lesson02_tail_calls/text.html b/src/content/chapter1_functions/lesson02_tail_calls/text.html new file mode 100644 index 0000000..ec39cda --- /dev/null +++ b/src/content/chapter1_functions/lesson02_tail_calls/text.html @@ -0,0 +1,23 @@ +<p> + When a function is called a new stack frame is created in memory to store the + arguments and local variables of the function. If lots of these frames are + created during recursion then the program would use a large amount of memory, + or even crash the program if some limit is hit. +</p> +<p> + To avoid this problem Gleam supports <em>tail call optimisation</em>, which + allows the compiler to reuse the stack frame for the current function if a + function call is the last thing the function does, removing the memory cost. +</p> + +<p> + Unoptimised recursive functions can often be rewritten into tail call + optimised functions by using an accumulator. An accumulator is a variable that + is passed along in addition to the data, similar to a mutable variable in a + language with <code>while</code> loops. +</p> +<p> + Accumulators should be hidden away from the users of your code, they are + internal implementation details. To do this write a public function that calls + a recursive private function with the initial accumulator value. +</p> diff --git a/src/content/chapter1_functions/lesson01_higher_order_functions/code.gleam b/src/content/chapter1_functions/lesson03_higher_order_functions/code.gleam index e3fb3e7..e3fb3e7 100644 --- a/src/content/chapter1_functions/lesson01_higher_order_functions/code.gleam +++ b/src/content/chapter1_functions/lesson03_higher_order_functions/code.gleam diff --git a/src/content/chapter1_functions/lesson01_higher_order_functions/text.html b/src/content/chapter1_functions/lesson03_higher_order_functions/text.html index 3343e4d..3343e4d 100644 --- a/src/content/chapter1_functions/lesson01_higher_order_functions/text.html +++ b/src/content/chapter1_functions/lesson03_higher_order_functions/text.html diff --git a/src/content/chapter1_functions/lesson02_anonymous_functions/code.gleam b/src/content/chapter1_functions/lesson04_anonymous_functions/code.gleam index 2b037e0..2b037e0 100644 --- a/src/content/chapter1_functions/lesson02_anonymous_functions/code.gleam +++ b/src/content/chapter1_functions/lesson04_anonymous_functions/code.gleam diff --git a/src/content/chapter1_functions/lesson02_anonymous_functions/text.html b/src/content/chapter1_functions/lesson04_anonymous_functions/text.html index f7bea3f..f7bea3f 100644 --- a/src/content/chapter1_functions/lesson02_anonymous_functions/text.html +++ b/src/content/chapter1_functions/lesson04_anonymous_functions/text.html diff --git a/src/content/chapter1_functions/lesson03_function_captures/code.gleam b/src/content/chapter1_functions/lesson05_function_captures/code.gleam index 35f3412..35f3412 100644 --- a/src/content/chapter1_functions/lesson03_function_captures/code.gleam +++ b/src/content/chapter1_functions/lesson05_function_captures/code.gleam diff --git a/src/content/chapter1_functions/lesson03_function_captures/text.html b/src/content/chapter1_functions/lesson05_function_captures/text.html index afa87a3..afa87a3 100644 --- a/src/content/chapter1_functions/lesson03_function_captures/text.html +++ b/src/content/chapter1_functions/lesson05_function_captures/text.html diff --git a/src/content/chapter1_functions/lesson04_generic_functions/code.gleam b/src/content/chapter1_functions/lesson06_generic_functions/code.gleam index e232bf8..e232bf8 100644 --- a/src/content/chapter1_functions/lesson04_generic_functions/code.gleam +++ b/src/content/chapter1_functions/lesson06_generic_functions/code.gleam diff --git a/src/content/chapter1_functions/lesson04_generic_functions/text.html b/src/content/chapter1_functions/lesson06_generic_functions/text.html index 1369c93..1369c93 100644 --- a/src/content/chapter1_functions/lesson04_generic_functions/text.html +++ b/src/content/chapter1_functions/lesson06_generic_functions/text.html diff --git a/src/content/chapter1_functions/lesson05_pipelines/code.gleam b/src/content/chapter1_functions/lesson07_pipelines/code.gleam index ec9b805..ec9b805 100644 --- a/src/content/chapter1_functions/lesson05_pipelines/code.gleam +++ b/src/content/chapter1_functions/lesson07_pipelines/code.gleam diff --git a/src/content/chapter1_functions/lesson05_pipelines/text.html b/src/content/chapter1_functions/lesson07_pipelines/text.html index 783ade9..783ade9 100644 --- a/src/content/chapter1_functions/lesson05_pipelines/text.html +++ b/src/content/chapter1_functions/lesson07_pipelines/text.html diff --git a/src/content/chapter1_functions/lesson06_labelled_arguments/code.gleam b/src/content/chapter1_functions/lesson08_labelled_arguments/code.gleam index 25bb8c1..25bb8c1 100644 --- a/src/content/chapter1_functions/lesson06_labelled_arguments/code.gleam +++ b/src/content/chapter1_functions/lesson08_labelled_arguments/code.gleam diff --git a/src/content/chapter1_functions/lesson06_labelled_arguments/text.html b/src/content/chapter1_functions/lesson08_labelled_arguments/text.html index b1d771c..b1d771c 100644 --- a/src/content/chapter1_functions/lesson06_labelled_arguments/text.html +++ b/src/content/chapter1_functions/lesson08_labelled_arguments/text.html diff --git a/src/content/chapter1_functions/lesson099_documentation_comments/code.gleam b/src/content/chapter1_functions/lesson09_documentation_comments/code.gleam index a84dce6..a84dce6 100644 --- a/src/content/chapter1_functions/lesson099_documentation_comments/code.gleam +++ b/src/content/chapter1_functions/lesson09_documentation_comments/code.gleam diff --git a/src/content/chapter1_functions/lesson099_documentation_comments/text.html b/src/content/chapter1_functions/lesson09_documentation_comments/text.html index c27bac6..c27bac6 100644 --- a/src/content/chapter1_functions/lesson099_documentation_comments/text.html +++ b/src/content/chapter1_functions/lesson09_documentation_comments/text.html diff --git a/src/content/chapter1_functions/lesson10_deprecations/code.gleam b/src/content/chapter1_functions/lesson10_deprecations/code.gleam new file mode 100644 index 0000000..26a8f0b --- /dev/null +++ b/src/content/chapter1_functions/lesson10_deprecations/code.gleam @@ -0,0 +1,13 @@ +pub fn main() { + old_function() + new_function() +} + +@deprecated("Use new_function instead") +fn old_function() { + Nil +} + +fn new_function() { + Nil +} diff --git a/src/content/chapter1_functions/lesson10_deprecations/text.html b/src/content/chapter1_functions/lesson10_deprecations/text.html new file mode 100644 index 0000000..0d83ef7 --- /dev/null +++ b/src/content/chapter1_functions/lesson10_deprecations/text.html @@ -0,0 +1,13 @@ +<p> + Functions and other definitions can be marked as deprecated using the + `@deprecated` attribute. +</p> +<p> + If a deprecated function is reference the compiler will emit a warning, + letting the programmer know they ought to update their code. +</p> +<p> + The deprecation atribute takes a message and this will be displayed to the + user in the warning. In the message explain to the user the new approach or + replacement function, or direct them on documentation on how to upgrade. +</p> diff --git a/src/content/chapter5_advance_features/lesson01_use_continued/code.gleam b/src/content/chapter5_advance_features/lesson01_use_sugar/code.gleam index ac61062..ac61062 100644 --- a/src/content/chapter5_advance_features/lesson01_use_continued/code.gleam +++ b/src/content/chapter5_advance_features/lesson01_use_sugar/code.gleam diff --git a/src/content/chapter5_advance_features/lesson01_use_continued/text.html b/src/content/chapter5_advance_features/lesson01_use_sugar/text.html index e28c843..e28c843 100644 --- a/src/content/chapter5_advance_features/lesson01_use_continued/text.html +++ b/src/content/chapter5_advance_features/lesson01_use_sugar/text.html |