-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a new chapter on label shorthand syntax
- Loading branch information
1 parent
1495c29
commit 2b7dd35
Showing
9 changed files
with
34 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ build | |
erl_crash.dump | ||
/public | ||
/wasm-compiler | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/content/chapter1_functions/lesson09_label_shorthand_syntax/code.gleam
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
5 changes: 5 additions & 0 deletions
5
src/content/chapter1_functions/lesson09_label_shorthand_syntax/en.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<p> | ||
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. | ||
</p> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.