From 2b7dd3595e35bab1e3da5cb17be2fc5fc62e26ca Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Tue, 10 Dec 2024 00:06:37 -0500 Subject: [PATCH] Introduce a new chapter on label shorthand syntax --- .gitignore | 1 + .../lesson08_labelled_arguments/code.gleam | 11 -------- .../lesson08_labelled_arguments/en.html | 5 ---- .../code.gleam | 28 +++++++++++++++++++ .../lesson09_label_shorthand_syntax/en.html | 5 ++++ .../code.gleam | 0 .../en.html | 0 .../code.gleam | 0 .../en.html | 0 9 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 src/content/chapter1_functions/lesson09_label_shorthand_syntax/code.gleam create mode 100644 src/content/chapter1_functions/lesson09_label_shorthand_syntax/en.html rename src/content/chapter1_functions/{lesson09_documentation_comments => lesson10_documentation_comments}/code.gleam (100%) rename src/content/chapter1_functions/{lesson09_documentation_comments => lesson10_documentation_comments}/en.html (100%) rename src/content/chapter1_functions/{lesson10_deprecations => lesson11_deprecations}/code.gleam (100%) rename src/content/chapter1_functions/{lesson10_deprecations => lesson11_deprecations}/en.html (100%) diff --git a/.gitignore b/.gitignore index 7575437..29d9fda 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ build erl_crash.dump /public /wasm-compiler +.DS_Store diff --git a/src/content/chapter1_functions/lesson08_labelled_arguments/code.gleam b/src/content/chapter1_functions/lesson08_labelled_arguments/code.gleam index becdf7f..25bb8c1 100644 --- a/src/content/chapter1_functions/lesson08_labelled_arguments/code.gleam +++ b/src/content/chapter1_functions/lesson08_labelled_arguments/code.gleam @@ -9,17 +9,6 @@ pub fn main() { // Using the labels in a different order io.debug(calculate(1, multiply: 3, add: 2)) - - // You may have some local variables declared - let multiply = 3 - let add = 2 - - // You can specify the variables by name when calling the function - io.debug(calculate(1, add: add, multiply: multiply)) - - // Or use shorthand syntax, if the variable names match the names of the - // labelled arguments - io.debug(calculate(1, add:, multiply:)) } fn calculate(value: Int, add addend: Int, multiply multiplier: Int) { diff --git a/src/content/chapter1_functions/lesson08_labelled_arguments/en.html b/src/content/chapter1_functions/lesson08_labelled_arguments/en.html index 49d3cf8..edbe226 100644 --- a/src/content/chapter1_functions/lesson08_labelled_arguments/en.html +++ b/src/content/chapter1_functions/lesson08_labelled_arguments/en.html @@ -19,8 +19,3 @@ Labels are optional when calling a function, it is up to the programmer to decide what is clearest in their code.

-

- Additionally, when local variables have the same name as the labelled - argument, the variable name can be omitted when calling the function. This is - known as shorthand syntax for labels. -

diff --git a/src/content/chapter1_functions/lesson09_label_shorthand_syntax/code.gleam b/src/content/chapter1_functions/lesson09_label_shorthand_syntax/code.gleam new file mode 100644 index 0000000..af51b7f --- /dev/null +++ b/src/content/chapter1_functions/lesson09_label_shorthand_syntax/code.gleam @@ -0,0 +1,28 @@ +pub fn main() { + let quantity = 5.0 + let unit_price = 10.0 + let discount = 0.2 + + // Explicitly providing local variable names when calling the function. + calculate_total_cost( + quantity: quantity, + unit_price: unit_price, + discount: discount, + ) + + // However, since our local variable names are identical to the argument + // labels, we can omit the variable names entirely and use shorthand label + // syntax. + calculate_total_cost(quantity:, unit_price:, discount:) +} + +fn calculate_total_cost( + quantity quantity: Float, + unit_price price: Float, + discount discount: Float, +) -> Float { + let subtotal = quantity *. price + let discount = subtotal *. discount + + subtotal -. discount +} diff --git a/src/content/chapter1_functions/lesson09_label_shorthand_syntax/en.html b/src/content/chapter1_functions/lesson09_label_shorthand_syntax/en.html new file mode 100644 index 0000000..3ce95de --- /dev/null +++ b/src/content/chapter1_functions/lesson09_label_shorthand_syntax/en.html @@ -0,0 +1,5 @@ +

+ When local variables have the same names as a function's labelled arguments, + the variable names can be omitted when calling the function. This is known as + shorthand syntax for labels. +

diff --git a/src/content/chapter1_functions/lesson09_documentation_comments/code.gleam b/src/content/chapter1_functions/lesson10_documentation_comments/code.gleam similarity index 100% rename from src/content/chapter1_functions/lesson09_documentation_comments/code.gleam rename to src/content/chapter1_functions/lesson10_documentation_comments/code.gleam diff --git a/src/content/chapter1_functions/lesson09_documentation_comments/en.html b/src/content/chapter1_functions/lesson10_documentation_comments/en.html similarity index 100% rename from src/content/chapter1_functions/lesson09_documentation_comments/en.html rename to src/content/chapter1_functions/lesson10_documentation_comments/en.html diff --git a/src/content/chapter1_functions/lesson10_deprecations/code.gleam b/src/content/chapter1_functions/lesson11_deprecations/code.gleam similarity index 100% rename from src/content/chapter1_functions/lesson10_deprecations/code.gleam rename to src/content/chapter1_functions/lesson11_deprecations/code.gleam diff --git a/src/content/chapter1_functions/lesson10_deprecations/en.html b/src/content/chapter1_functions/lesson11_deprecations/en.html similarity index 100% rename from src/content/chapter1_functions/lesson10_deprecations/en.html rename to src/content/chapter1_functions/lesson11_deprecations/en.html