From 2b7dd3595e35bab1e3da5cb17be2fc5fc62e26ca Mon Sep 17 00:00:00 2001
From: "Joseph T. Lyons"
- 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