From f92661980deac22b54e79cd44c25caba17910c95 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Thu, 18 Jan 2024 16:44:27 +0000 Subject: Last of the content! --- .../lesson01_higher_order_functions/code.gleam | 18 ---------------- .../lesson01_higher_order_functions/text.html | 12 ----------- .../lesson01_recursion/code.gleam | 17 +++++++++++++++ .../lesson01_recursion/text.html | 20 +++++++++++++++++ .../lesson02_anonymous_functions/code.gleam | 14 ------------ .../lesson02_anonymous_functions/text.html | 7 ------ .../lesson02_tail_calls/code.gleam | 21 ++++++++++++++++++ .../lesson02_tail_calls/text.html | 23 ++++++++++++++++++++ .../lesson03_function_captures/code.gleam | 14 ------------ .../lesson03_function_captures/text.html | 11 ---------- .../lesson03_higher_order_functions/code.gleam | 18 ++++++++++++++++ .../lesson03_higher_order_functions/text.html | 12 +++++++++++ .../lesson04_anonymous_functions/code.gleam | 14 ++++++++++++ .../lesson04_anonymous_functions/text.html | 7 ++++++ .../lesson04_generic_functions/code.gleam | 19 ---------------- .../lesson04_generic_functions/text.html | 25 ---------------------- .../lesson05_function_captures/code.gleam | 14 ++++++++++++ .../lesson05_function_captures/text.html | 11 ++++++++++ .../lesson05_pipelines/code.gleam | 19 ---------------- .../lesson05_pipelines/text.html | 25 ---------------------- .../lesson06_generic_functions/code.gleam | 19 ++++++++++++++++ .../lesson06_generic_functions/text.html | 25 ++++++++++++++++++++++ .../lesson06_labelled_arguments/code.gleam | 16 -------------- .../lesson06_labelled_arguments/text.html | 23 -------------------- .../lesson07_pipelines/code.gleam | 19 ++++++++++++++++ .../lesson07_pipelines/text.html | 25 ++++++++++++++++++++++ .../lesson08_labelled_arguments/code.gleam | 16 ++++++++++++++ .../lesson08_labelled_arguments/text.html | 23 ++++++++++++++++++++ .../lesson099_documentation_comments/code.gleam | 19 ---------------- .../lesson099_documentation_comments/text.html | 16 -------------- .../lesson09_documentation_comments/code.gleam | 19 ++++++++++++++++ .../lesson09_documentation_comments/text.html | 16 ++++++++++++++ .../lesson10_deprecations/code.gleam | 13 +++++++++++ .../lesson10_deprecations/text.html | 13 +++++++++++ 34 files changed, 345 insertions(+), 238 deletions(-) delete mode 100644 src/content/chapter1_functions/lesson01_higher_order_functions/code.gleam delete mode 100644 src/content/chapter1_functions/lesson01_higher_order_functions/text.html create mode 100644 src/content/chapter1_functions/lesson01_recursion/code.gleam create mode 100644 src/content/chapter1_functions/lesson01_recursion/text.html delete mode 100644 src/content/chapter1_functions/lesson02_anonymous_functions/code.gleam delete mode 100644 src/content/chapter1_functions/lesson02_anonymous_functions/text.html create mode 100644 src/content/chapter1_functions/lesson02_tail_calls/code.gleam create mode 100644 src/content/chapter1_functions/lesson02_tail_calls/text.html delete mode 100644 src/content/chapter1_functions/lesson03_function_captures/code.gleam delete mode 100644 src/content/chapter1_functions/lesson03_function_captures/text.html create mode 100644 src/content/chapter1_functions/lesson03_higher_order_functions/code.gleam create mode 100644 src/content/chapter1_functions/lesson03_higher_order_functions/text.html create mode 100644 src/content/chapter1_functions/lesson04_anonymous_functions/code.gleam create mode 100644 src/content/chapter1_functions/lesson04_anonymous_functions/text.html delete mode 100644 src/content/chapter1_functions/lesson04_generic_functions/code.gleam delete mode 100644 src/content/chapter1_functions/lesson04_generic_functions/text.html create mode 100644 src/content/chapter1_functions/lesson05_function_captures/code.gleam create mode 100644 src/content/chapter1_functions/lesson05_function_captures/text.html delete mode 100644 src/content/chapter1_functions/lesson05_pipelines/code.gleam delete mode 100644 src/content/chapter1_functions/lesson05_pipelines/text.html create mode 100644 src/content/chapter1_functions/lesson06_generic_functions/code.gleam create mode 100644 src/content/chapter1_functions/lesson06_generic_functions/text.html delete mode 100644 src/content/chapter1_functions/lesson06_labelled_arguments/code.gleam delete mode 100644 src/content/chapter1_functions/lesson06_labelled_arguments/text.html create mode 100644 src/content/chapter1_functions/lesson07_pipelines/code.gleam create mode 100644 src/content/chapter1_functions/lesson07_pipelines/text.html create mode 100644 src/content/chapter1_functions/lesson08_labelled_arguments/code.gleam create mode 100644 src/content/chapter1_functions/lesson08_labelled_arguments/text.html delete mode 100644 src/content/chapter1_functions/lesson099_documentation_comments/code.gleam delete mode 100644 src/content/chapter1_functions/lesson099_documentation_comments/text.html create mode 100644 src/content/chapter1_functions/lesson09_documentation_comments/code.gleam create mode 100644 src/content/chapter1_functions/lesson09_documentation_comments/text.html create mode 100644 src/content/chapter1_functions/lesson10_deprecations/code.gleam create mode 100644 src/content/chapter1_functions/lesson10_deprecations/text.html (limited to 'src/content/chapter1_functions') diff --git a/src/content/chapter1_functions/lesson01_higher_order_functions/code.gleam b/src/content/chapter1_functions/lesson01_higher_order_functions/code.gleam deleted file mode 100644 index e3fb3e7..0000000 --- a/src/content/chapter1_functions/lesson01_higher_order_functions/code.gleam +++ /dev/null @@ -1,18 +0,0 @@ -import gleam/io - -pub fn main() { - // Call a function with another function - io.debug(twice(1, add_one)) - - // Functions can be assigned to variables - let function = add_one - io.debug(function(100)) -} - -fn twice(argument: Int, function: fn(Int) -> Int) -> Int { - function(function(argument)) -} - -fn add_one(argument: Int) -> Int { - argument + 1 -} diff --git a/src/content/chapter1_functions/lesson01_higher_order_functions/text.html b/src/content/chapter1_functions/lesson01_higher_order_functions/text.html deleted file mode 100644 index 3343e4d..0000000 --- a/src/content/chapter1_functions/lesson01_higher_order_functions/text.html +++ /dev/null @@ -1,12 +0,0 @@ -

- In Gleam functions are values. They can be assigned to variables, passed to - other functions, and anything else you can do with values. -

-

- Here the function add_one is being passed as an argument to the - twice function. -

-

- Notice the fn keyword is also used to describe the type of the - function that twice takes as its second argument. -

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

+ Gleam doesn't have loops, instead iteration is done through recursion, that is + through top-level functions calling themselves with different arguments. +

+

+ A recursive function needs to have at least one base case and at + least one recursive case. A base case returns a value without calling + the function again. A recursive case calls the function again with different + inputs, looping again. +

+

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

+

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

diff --git a/src/content/chapter1_functions/lesson02_anonymous_functions/code.gleam b/src/content/chapter1_functions/lesson02_anonymous_functions/code.gleam deleted file mode 100644 index 2b037e0..0000000 --- a/src/content/chapter1_functions/lesson02_anonymous_functions/code.gleam +++ /dev/null @@ -1,14 +0,0 @@ -import gleam/io - -pub fn main() { - // Assign an anonymous function to a variable - let add_one = fn(a) { a + 1 } - io.debug(twice(1, add_one)) - - // Pass an anonymous function as an argument - io.debug(twice(1, fn(a) { a * 2 })) -} - -fn twice(argument: Int, function: fn(Int) -> Int) -> Int { - function(function(argument)) -} diff --git a/src/content/chapter1_functions/lesson02_anonymous_functions/text.html b/src/content/chapter1_functions/lesson02_anonymous_functions/text.html deleted file mode 100644 index f7bea3f..0000000 --- a/src/content/chapter1_functions/lesson02_anonymous_functions/text.html +++ /dev/null @@ -1,7 +0,0 @@ -

- As well as module-level named functions, Gleam has anonymous function - literals. -

-

- Anonymous functions can be used interchangeably with named functions. -

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

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

+

+ To avoid this problem Gleam supports tail call optimisation, 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. +

+ +

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

+

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

diff --git a/src/content/chapter1_functions/lesson03_function_captures/code.gleam b/src/content/chapter1_functions/lesson03_function_captures/code.gleam deleted file mode 100644 index 35f3412..0000000 --- a/src/content/chapter1_functions/lesson03_function_captures/code.gleam +++ /dev/null @@ -1,14 +0,0 @@ -import gleam/io - -pub fn main() { - // These two statements are equivalent - let add_one_v1 = fn(x) { add(1, x) } - let add_one_v2 = add(1, _) - - io.debug(add_one_v1(10)) - io.debug(add_one_v2(10)) -} - -fn add(a: Int, b: Int) -> Int { - a + b -} diff --git a/src/content/chapter1_functions/lesson03_function_captures/text.html b/src/content/chapter1_functions/lesson03_function_captures/text.html deleted file mode 100644 index afa87a3..0000000 --- a/src/content/chapter1_functions/lesson03_function_captures/text.html +++ /dev/null @@ -1,11 +0,0 @@ -

- Gleam has a shorthand syntax for creating anonymous functions that take one - argument and immediately call another function with that argument: the - function capture syntax. -

-

- The anonymous function fn(a) { some_function(..., a, ...) } can - be written as some_function(..., _, ...), with any number of - other arguments passed to the inner function. The underscore _ is - a placeholder for the final argument. -

diff --git a/src/content/chapter1_functions/lesson03_higher_order_functions/code.gleam b/src/content/chapter1_functions/lesson03_higher_order_functions/code.gleam new file mode 100644 index 0000000..e3fb3e7 --- /dev/null +++ b/src/content/chapter1_functions/lesson03_higher_order_functions/code.gleam @@ -0,0 +1,18 @@ +import gleam/io + +pub fn main() { + // Call a function with another function + io.debug(twice(1, add_one)) + + // Functions can be assigned to variables + let function = add_one + io.debug(function(100)) +} + +fn twice(argument: Int, function: fn(Int) -> Int) -> Int { + function(function(argument)) +} + +fn add_one(argument: Int) -> Int { + argument + 1 +} diff --git a/src/content/chapter1_functions/lesson03_higher_order_functions/text.html b/src/content/chapter1_functions/lesson03_higher_order_functions/text.html new file mode 100644 index 0000000..3343e4d --- /dev/null +++ b/src/content/chapter1_functions/lesson03_higher_order_functions/text.html @@ -0,0 +1,12 @@ +

+ In Gleam functions are values. They can be assigned to variables, passed to + other functions, and anything else you can do with values. +

+

+ Here the function add_one is being passed as an argument to the + twice function. +

+

+ Notice the fn keyword is also used to describe the type of the + function that twice takes as its second argument. +

diff --git a/src/content/chapter1_functions/lesson04_anonymous_functions/code.gleam b/src/content/chapter1_functions/lesson04_anonymous_functions/code.gleam new file mode 100644 index 0000000..2b037e0 --- /dev/null +++ b/src/content/chapter1_functions/lesson04_anonymous_functions/code.gleam @@ -0,0 +1,14 @@ +import gleam/io + +pub fn main() { + // Assign an anonymous function to a variable + let add_one = fn(a) { a + 1 } + io.debug(twice(1, add_one)) + + // Pass an anonymous function as an argument + io.debug(twice(1, fn(a) { a * 2 })) +} + +fn twice(argument: Int, function: fn(Int) -> Int) -> Int { + function(function(argument)) +} diff --git a/src/content/chapter1_functions/lesson04_anonymous_functions/text.html b/src/content/chapter1_functions/lesson04_anonymous_functions/text.html new file mode 100644 index 0000000..f7bea3f --- /dev/null +++ b/src/content/chapter1_functions/lesson04_anonymous_functions/text.html @@ -0,0 +1,7 @@ +

+ As well as module-level named functions, Gleam has anonymous function + literals. +

+

+ Anonymous functions can be used interchangeably with named functions. +

diff --git a/src/content/chapter1_functions/lesson04_generic_functions/code.gleam b/src/content/chapter1_functions/lesson04_generic_functions/code.gleam deleted file mode 100644 index e232bf8..0000000 --- a/src/content/chapter1_functions/lesson04_generic_functions/code.gleam +++ /dev/null @@ -1,19 +0,0 @@ -import gleam/io - -pub fn main() { - let add_one = fn(x) { x + 1 } - let exclaim = fn(x) { x <> "!" } - - // Invalid, Int and String are not the same type - // twice(10, exclaim) - - // Here the type variable is replaced by the type Int - io.debug(twice(10, add_one)) - - // Here the type variable is replaced by the type String - io.debug(twice("Hello", exclaim)) -} - -fn twice(argument: value, function: fn(value) -> value) -> value { - function(function(argument)) -} diff --git a/src/content/chapter1_functions/lesson04_generic_functions/text.html b/src/content/chapter1_functions/lesson04_generic_functions/text.html deleted file mode 100644 index 1369c93..0000000 --- a/src/content/chapter1_functions/lesson04_generic_functions/text.html +++ /dev/null @@ -1,25 +0,0 @@ -

- Up until now each function has accepted precisely one type for each of its - arguments. -

-

- The twice function for example only worked with functions that - would take and return ints. This is overly restrictive, it should be possible - to use this function with any type, so long as the function and the initial - value are compatible. -

-

- To enable this Gleam support generics, also known as parametric - polymorphism. -

-

- This works by instead of specifying a concrete type, a type variable is used - which stands in for whatever specific type is being used when the function is - called. These type variable are written with a lowercase name. -

-

- Type variables are not like an any type, they get replaced with a - specific type each time the function is called. Try uncommenting - twice(10, exclaim) to see the compiler error from trying to use a - type variable as an int and a string at the same time. -

diff --git a/src/content/chapter1_functions/lesson05_function_captures/code.gleam b/src/content/chapter1_functions/lesson05_function_captures/code.gleam new file mode 100644 index 0000000..35f3412 --- /dev/null +++ b/src/content/chapter1_functions/lesson05_function_captures/code.gleam @@ -0,0 +1,14 @@ +import gleam/io + +pub fn main() { + // These two statements are equivalent + let add_one_v1 = fn(x) { add(1, x) } + let add_one_v2 = add(1, _) + + io.debug(add_one_v1(10)) + io.debug(add_one_v2(10)) +} + +fn add(a: Int, b: Int) -> Int { + a + b +} diff --git a/src/content/chapter1_functions/lesson05_function_captures/text.html b/src/content/chapter1_functions/lesson05_function_captures/text.html new file mode 100644 index 0000000..afa87a3 --- /dev/null +++ b/src/content/chapter1_functions/lesson05_function_captures/text.html @@ -0,0 +1,11 @@ +

+ Gleam has a shorthand syntax for creating anonymous functions that take one + argument and immediately call another function with that argument: the + function capture syntax. +

+

+ The anonymous function fn(a) { some_function(..., a, ...) } can + be written as some_function(..., _, ...), with any number of + other arguments passed to the inner function. The underscore _ is + a placeholder for the final argument. +

diff --git a/src/content/chapter1_functions/lesson05_pipelines/code.gleam b/src/content/chapter1_functions/lesson05_pipelines/code.gleam deleted file mode 100644 index ec9b805..0000000 --- a/src/content/chapter1_functions/lesson05_pipelines/code.gleam +++ /dev/null @@ -1,19 +0,0 @@ -import gleam/io -import gleam/string - -pub fn main() { - // Without the pipe operator - io.debug(string.drop_left(string.drop_right("Hello, Joe!", 1), 7)) - - // With the pipe operator - "Hello, Mike!" - |> string.drop_right(1) - |> string.drop_left(7) - |> io.debug - - // Changing order with function capturing - "1" - |> string.append("2") - |> string.append("3", _) - |> io.debug -} diff --git a/src/content/chapter1_functions/lesson05_pipelines/text.html b/src/content/chapter1_functions/lesson05_pipelines/text.html deleted file mode 100644 index 783ade9..0000000 --- a/src/content/chapter1_functions/lesson05_pipelines/text.html +++ /dev/null @@ -1,25 +0,0 @@ -

- It's common to want to call a series of functions, passing the result of one - to the next. With the regular function call syntax this can be a little - difficult to read as you have to read the code from the inside out. -

-

- Gleam's pipe operator |> helps with this problem by allowing you - to write code top-to-bottom. -

-

- The pipe operator takes the result of the expression on its left and passes it - as an argument to the function on its right. -

-

- It will first check to see if the left-hand value could be used as the first - argument to the call. For example, a |> b(1, 2) would become - b(a, 1, 2). If not, it falls back to calling the result of the - right-hand side as a function, e.g., b(1, 2)(a) -

-

- Gleam code is typically written with the "subject" of the function as the - first argument, to make it easier to pipe. If you wish to pipe to a different - position then a function capture can be used to insert the argument to the - desired position. -

diff --git a/src/content/chapter1_functions/lesson06_generic_functions/code.gleam b/src/content/chapter1_functions/lesson06_generic_functions/code.gleam new file mode 100644 index 0000000..e232bf8 --- /dev/null +++ b/src/content/chapter1_functions/lesson06_generic_functions/code.gleam @@ -0,0 +1,19 @@ +import gleam/io + +pub fn main() { + let add_one = fn(x) { x + 1 } + let exclaim = fn(x) { x <> "!" } + + // Invalid, Int and String are not the same type + // twice(10, exclaim) + + // Here the type variable is replaced by the type Int + io.debug(twice(10, add_one)) + + // Here the type variable is replaced by the type String + io.debug(twice("Hello", exclaim)) +} + +fn twice(argument: value, function: fn(value) -> value) -> value { + function(function(argument)) +} diff --git a/src/content/chapter1_functions/lesson06_generic_functions/text.html b/src/content/chapter1_functions/lesson06_generic_functions/text.html new file mode 100644 index 0000000..1369c93 --- /dev/null +++ b/src/content/chapter1_functions/lesson06_generic_functions/text.html @@ -0,0 +1,25 @@ +

+ Up until now each function has accepted precisely one type for each of its + arguments. +

+

+ The twice function for example only worked with functions that + would take and return ints. This is overly restrictive, it should be possible + to use this function with any type, so long as the function and the initial + value are compatible. +

+

+ To enable this Gleam support generics, also known as parametric + polymorphism. +

+

+ This works by instead of specifying a concrete type, a type variable is used + which stands in for whatever specific type is being used when the function is + called. These type variable are written with a lowercase name. +

+

+ Type variables are not like an any type, they get replaced with a + specific type each time the function is called. Try uncommenting + twice(10, exclaim) to see the compiler error from trying to use a + type variable as an int and a string at the same time. +

diff --git a/src/content/chapter1_functions/lesson06_labelled_arguments/code.gleam b/src/content/chapter1_functions/lesson06_labelled_arguments/code.gleam deleted file mode 100644 index 25bb8c1..0000000 --- a/src/content/chapter1_functions/lesson06_labelled_arguments/code.gleam +++ /dev/null @@ -1,16 +0,0 @@ -import gleam/io - -pub fn main() { - // Without using labels - io.debug(calculate(1, 2, 3)) - - // Using the labels - io.debug(calculate(1, add: 2, multiply: 3)) - - // Using the labels in a different order - io.debug(calculate(1, multiply: 3, add: 2)) -} - -fn calculate(value: Int, add addend: Int, multiply multiplier: Int) { - value * multiplier + addend -} diff --git a/src/content/chapter1_functions/lesson06_labelled_arguments/text.html b/src/content/chapter1_functions/lesson06_labelled_arguments/text.html deleted file mode 100644 index b1d771c..0000000 --- a/src/content/chapter1_functions/lesson06_labelled_arguments/text.html +++ /dev/null @@ -1,23 +0,0 @@ -

- When functions take several arguments it can be difficult to remember what the - arguments are, and what order they are expected in. -

-

- To help with this Gleam supports labelled arguments, where function arguments - are given an external label in addition to their internal name. These labels - are written before the argument name in the function definition. -

-

- When labelled arguments are used the order of the arguments does not matter, - but all unlabelled arguments must come before labelled arguments. -

-

- There is no performance cost to using labelled arguments, it does not allocate - a dictionary or perform any other runtime work. -

-

- Labels are optional when calling a function, it is up to the programmer to - decide what is clearest in their code. -

- - diff --git a/src/content/chapter1_functions/lesson07_pipelines/code.gleam b/src/content/chapter1_functions/lesson07_pipelines/code.gleam new file mode 100644 index 0000000..ec9b805 --- /dev/null +++ b/src/content/chapter1_functions/lesson07_pipelines/code.gleam @@ -0,0 +1,19 @@ +import gleam/io +import gleam/string + +pub fn main() { + // Without the pipe operator + io.debug(string.drop_left(string.drop_right("Hello, Joe!", 1), 7)) + + // With the pipe operator + "Hello, Mike!" + |> string.drop_right(1) + |> string.drop_left(7) + |> io.debug + + // Changing order with function capturing + "1" + |> string.append("2") + |> string.append("3", _) + |> io.debug +} diff --git a/src/content/chapter1_functions/lesson07_pipelines/text.html b/src/content/chapter1_functions/lesson07_pipelines/text.html new file mode 100644 index 0000000..783ade9 --- /dev/null +++ b/src/content/chapter1_functions/lesson07_pipelines/text.html @@ -0,0 +1,25 @@ +

+ It's common to want to call a series of functions, passing the result of one + to the next. With the regular function call syntax this can be a little + difficult to read as you have to read the code from the inside out. +

+

+ Gleam's pipe operator |> helps with this problem by allowing you + to write code top-to-bottom. +

+

+ The pipe operator takes the result of the expression on its left and passes it + as an argument to the function on its right. +

+

+ It will first check to see if the left-hand value could be used as the first + argument to the call. For example, a |> b(1, 2) would become + b(a, 1, 2). If not, it falls back to calling the result of the + right-hand side as a function, e.g., b(1, 2)(a) +

+

+ Gleam code is typically written with the "subject" of the function as the + first argument, to make it easier to pipe. If you wish to pipe to a different + position then a function capture can be used to insert the argument to the + desired position. +

diff --git a/src/content/chapter1_functions/lesson08_labelled_arguments/code.gleam b/src/content/chapter1_functions/lesson08_labelled_arguments/code.gleam new file mode 100644 index 0000000..25bb8c1 --- /dev/null +++ b/src/content/chapter1_functions/lesson08_labelled_arguments/code.gleam @@ -0,0 +1,16 @@ +import gleam/io + +pub fn main() { + // Without using labels + io.debug(calculate(1, 2, 3)) + + // Using the labels + io.debug(calculate(1, add: 2, multiply: 3)) + + // Using the labels in a different order + io.debug(calculate(1, multiply: 3, add: 2)) +} + +fn calculate(value: Int, add addend: Int, multiply multiplier: Int) { + value * multiplier + addend +} diff --git a/src/content/chapter1_functions/lesson08_labelled_arguments/text.html b/src/content/chapter1_functions/lesson08_labelled_arguments/text.html new file mode 100644 index 0000000..b1d771c --- /dev/null +++ b/src/content/chapter1_functions/lesson08_labelled_arguments/text.html @@ -0,0 +1,23 @@ +

+ When functions take several arguments it can be difficult to remember what the + arguments are, and what order they are expected in. +

+

+ To help with this Gleam supports labelled arguments, where function arguments + are given an external label in addition to their internal name. These labels + are written before the argument name in the function definition. +

+

+ When labelled arguments are used the order of the arguments does not matter, + but all unlabelled arguments must come before labelled arguments. +

+

+ There is no performance cost to using labelled arguments, it does not allocate + a dictionary or perform any other runtime work. +

+

+ Labels are optional when calling a function, it is up to the programmer to + decide what is clearest in their code. +

+ + diff --git a/src/content/chapter1_functions/lesson099_documentation_comments/code.gleam b/src/content/chapter1_functions/lesson099_documentation_comments/code.gleam deleted file mode 100644 index a84dce6..0000000 --- a/src/content/chapter1_functions/lesson099_documentation_comments/code.gleam +++ /dev/null @@ -1,19 +0,0 @@ -//// A module containing some unusual functions and types. - -/// A type where the value can never be constructed. -/// Can you work out why? -pub type Never { - Never(Never) -} - -/// Call a function twice with an initial value. -/// -pub fn twice(argument: value, function: fn(value) -> value) -> value { - function(function(argument)) -} - -/// Call a function three times with an initial value. -/// -pub fn thrice(argument: value, function: fn(value) -> value) -> value { - function(function(function(argument))) -} diff --git a/src/content/chapter1_functions/lesson099_documentation_comments/text.html b/src/content/chapter1_functions/lesson099_documentation_comments/text.html deleted file mode 100644 index c27bac6..0000000 --- a/src/content/chapter1_functions/lesson099_documentation_comments/text.html +++ /dev/null @@ -1,16 +0,0 @@ -

- Documentation and comments are important tools for making your code easier to - work with and understand. -

-

- As well as regular // comments Gleam has /// and - //// comments which are used for attaching documentation to code. -

-

- /// is used for documenting types and functions, and should be - placed immediately before the type or function it is documenting. -

-

- //// is used for documenting modules, and should be placed - at the top of the module. -

diff --git a/src/content/chapter1_functions/lesson09_documentation_comments/code.gleam b/src/content/chapter1_functions/lesson09_documentation_comments/code.gleam new file mode 100644 index 0000000..a84dce6 --- /dev/null +++ b/src/content/chapter1_functions/lesson09_documentation_comments/code.gleam @@ -0,0 +1,19 @@ +//// A module containing some unusual functions and types. + +/// A type where the value can never be constructed. +/// Can you work out why? +pub type Never { + Never(Never) +} + +/// Call a function twice with an initial value. +/// +pub fn twice(argument: value, function: fn(value) -> value) -> value { + function(function(argument)) +} + +/// Call a function three times with an initial value. +/// +pub fn thrice(argument: value, function: fn(value) -> value) -> value { + function(function(function(argument))) +} diff --git a/src/content/chapter1_functions/lesson09_documentation_comments/text.html b/src/content/chapter1_functions/lesson09_documentation_comments/text.html new file mode 100644 index 0000000..c27bac6 --- /dev/null +++ b/src/content/chapter1_functions/lesson09_documentation_comments/text.html @@ -0,0 +1,16 @@ +

+ Documentation and comments are important tools for making your code easier to + work with and understand. +

+

+ As well as regular // comments Gleam has /// and + //// comments which are used for attaching documentation to code. +

+

+ /// is used for documenting types and functions, and should be + placed immediately before the type or function it is documenting. +

+

+ //// is used for documenting modules, and should be placed + at the top of the module. +

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

+ Functions and other definitions can be marked as deprecated using the + `@deprecated` attribute. +

+

+ If a deprecated function is reference the compiler will emit a warning, + letting the programmer know they ought to update their code. +

+

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

-- cgit v1.2.3