Skip to content

[Swift 6]: Update lasagna master and its concept #836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions concepts/default-parameters/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ func greeting(name: String = "guest", duration: Int = 2) -> String {
}

greeting(name: "Bobo", duration: 7)
// => "Welcome, Bobo. Enjoy your 7 night stay."
// Returns "Welcome, Bobo. Enjoy your 7 night stay."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The strings concept doc doesn't have "Returns" on every example line of code. It might be good to pick one style and use it consistently.

greeting(duration: 3)
// => "Welcome, guest. Enjoy your 3 night stay."
// Returns "Welcome, guest. Enjoy your 3 night stay."
greeting(name: "Wynona")
// => "Welcome, Wynona. Enjoy your 2 night stay."
// Returns "Welcome, Wynona. Enjoy your 2 night stay."
greeting()
// => "Welcome, guest. Enjoy your 2 night stay."
// Returns "Welcome, guest. Enjoy your 2 night stay."
```

[default-parameter-values]: https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID169
[default-parameter-values]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Default-Parameter-Values
15 changes: 9 additions & 6 deletions concepts/default-parameters/introduction.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# Introduction
# Default parameter values

Default parameter values can be supplied for any of a function's parameters by assigning a value to the parameter in the parameter list following the parameter's type annotation. When a default parameter value is specified, the caller of the function can omit that parameter when calling the function and the default value will be used instead.
[Default parameter values][default-parameter-values] can be supplied for any of a function's parameters by assigning a value to the parameter in the parameter list following the parameter's type annotation.
When a default parameter value is specified, the caller of the function can omit that parameter when calling the function and the default value will be used instead.

```swift
func greeting(name: String = "guest", duration: Int = 2) -> String {
"Welcome, \(name). Enjoy your \(duration) night stay."
}

greeting(name: "Bobo", duration: 7)
// => "Welcome, Bobo. Enjoy your 7 night stay."
// Returns "Welcome, Bobo. Enjoy your 7 night stay."
greeting(duration: 3)
// => "Welcome, guest. Enjoy your 3 night stay."
// Returns "Welcome, guest. Enjoy your 3 night stay."
greeting(name: "Wynona")
// => "Welcome, Wynona. Enjoy your 2 night stay."
// Returns "Welcome, Wynona. Enjoy your 2 night stay."
greeting()
// => "Welcome, guest. Enjoy your 2 night stay."
// Returns "Welcome, guest. Enjoy your 2 night stay."
```

[default-parameter-values]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Default-Parameter-Values
4 changes: 2 additions & 2 deletions concepts/default-parameters/links.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"url": "https://docs.swift.org/swift-book/LanguageGuide/Functions.html",
"description": "Swift.org: Functions"
"url": "https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Default-Parameter-Values",
"description": "Swift.org: Default Parameter Values"
}
]
7 changes: 5 additions & 2 deletions concepts/inout-parameters/about.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# In-out parameters

Within the body of a function, parameters are treated as constants, not variables; trying to modify the value of a parameter will raise a compile-time error. If a function wishes to modify the value of a parameter, it must use an [_in-out parameter_][in-out-parameters] to make this mutability explicit.
Within the body of a function, parameters are treated as constants, not variables; trying to modify the value of a parameter will raise a compile-time error.
If a function wishes to modify the value of a parameter, it must use an [_in-out parameter_][in-out-parameters] to make this mutability explicit.

To use in-out parameters, a programmer must be sure of three things:

Expand All @@ -21,6 +22,7 @@ updateVersion(&dbRecord)
// dbRecord is now (3, "Exercism")
```

~~~~exercism/warrning
There are a couple of extra rules one should be aware of regarding in-out parameters.

1. Inside a function with in-out parameters, you are not allowed to reference the variable that was passed in as the in-out parameter.
Expand All @@ -36,5 +38,6 @@ var mutVar = 0
inoutFunc(&mutVar, &mutVar)
// raises a compiler error: "Inout arguments are not allowed to alias each other"
```
~~~~

[in-out-parameters]: https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID545
[in-out-parameters]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/declarations/#In-Out-Parameters
25 changes: 23 additions & 2 deletions concepts/inout-parameters/introduction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Introduction
# In-out parameters

Within the body of a function, parameters are treated as constants, not variables; trying to modify the value of a parameter will raise a compile-time error. If a function wishes to modify the value of a parameter, it must use an _in-out parameter_ to make this mutability explicit.
Within the body of a function, parameters are treated as constants, not variables; trying to modify the value of a parameter will raise a compile-time error.
If a function wishes to modify the value of a parameter, it must use an [_in-out parameter_][in-out-parameters] to make this mutability explicit.

To use in-out parameters, a programmer must be sure of three things:

Expand All @@ -20,3 +21,23 @@ updateVersion(&dbRecord)

// dbRecord is now (3, "Exercism")
```

~~~~exercism/warrning
There are a couple of extra rules one should be aware of regarding in-out parameters.

1. Inside a function with in-out parameters, you are not allowed to reference the variable that was passed in as the in-out parameter.
2. The same variable cannot be passed as multiple in-out parameters in the same function.

```swift
func inoutFunc(_ ioVar1: inout Int, _ ioVar2: inout Int) {
ioVar1 += 1
ioVar2 += 2
}

var mutVar = 0
inoutFunc(&mutVar, &mutVar)
// raises a compiler error: "Inout arguments are not allowed to alias each other"
```
~~~~

[in-out-parameters]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/declarations/#In-Out-Parameters
4 changes: 2 additions & 2 deletions concepts/inout-parameters/links.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"url": "https://docs.swift.org/swift-book/LanguageGuide/Functions.html",
"description": "Swift.org: Functions"
"url": "https://docs.swift.org/swift-book/documentation/the-swift-programming-language/declarations/#In-Out-Parameters",
"description": "Swift.org: In-Out Parameters"
}
]
24 changes: 9 additions & 15 deletions concepts/nested-functions/about.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
# Nested functions

Functions may be defined [inside of other functions][nested-functions]. This is commonly used to create helper functions which are only useful to their enclosing function and so don't need to pollute the outside namespace.
Functions may be defined [inside of other functions][nested-functions].
This is commonly used to create helper functions which are only useful to their enclosing function and so don't need to pollute the outside namespace.

These functions are defined and called just like normal functions, but are not visible outside the enclosing function.

```swift
func makeNumber(_ bits: [Bool]) -> Int {
func makeNumber(_ number: Int) -> Int {
func double(_ x: Int) -> Int { 2 * x }
func add(_ x: Int) -> Int { x + 1 }

var number = 0
for bit in bits {
number = double(number)
if bit {
number = add(number)
}
}
return number
return number.isMultiple(of: 2) ? add(double(number)) : double(number)
}

makeNumber([true, false, true, true])
// => 11
makeNumber([true, true, false, false, false, true, true])
// => 99
makeNumber(3)
// Returns 6
makeNumber(4)
// Returns 9
```

[nested-functions]: https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID178
[nested-functions]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Nested-Functions
19 changes: 14 additions & 5 deletions concepts/nested-functions/introduction.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
# Introduction
# Nested functions

Functions may be defined inside of other functions, which are known as _nested-functions_.
Functions may be defined [inside of other functions][nested-functions].
This is commonly used to create helper functions which are only useful to their enclosing function and so don't need to pollute the outside namespace.

These functions are defined and called just like normal functions, but are not visible outside the enclosing function.

```swift
func double(_ x: Int) -> Int {
func add(_ x: Int, y: Int) -> Int { x + y }
func makeNumber(_ number: Int) -> Int {
func double(_ x: Int) -> Int { 2 * x }
func add(_ x: Int) -> Int { x + 1 }

add(x, x)
return number.isMultiple(of: 2) ? add(double(number)) : double(number)
}

makeNumber(3)
// Returns 6
makeNumber(4)
// Returns 9
```

[nested-functions]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Nested-Functions
4 changes: 2 additions & 2 deletions concepts/nested-functions/links.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"url": "https://docs.swift.org/swift-book/LanguageGuide/Functions.html",
"description": "Swift.org: Functions"
"url": "https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Nested-Functions",
"description": "Swift.org: Nested Functions"
}
]
8 changes: 3 additions & 5 deletions concepts/variadic-parameters/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ geometricMean(1, 2, 3, 4, 5)
// => 2.605171084697352
```

When using variadic parameters, Swift has two limitations:
Note that when using variadic parameters, Swift has a limitation.
If a function has parameters that follow the variadic parameter in the definition, the first parameter following the variadic parameter is _required_ to have an argument label.

1. Swift only allows functions to have one variadic parameter. It has been decided that this restriction will be removed in a future release of Swift, but it is currently still in place.
2. If a function has parameters that follow the variadic parameter in the definition, the first parameter following the variadic parameter is _required_ to have an argument label.

[variadic-parameters]: https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID171
[variadic-parameters]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Variadic-Parameters
14 changes: 11 additions & 3 deletions concepts/variadic-parameters/introduction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Introduction
# Variadic parameters

_Variadic parameters_ in Swift allow zero or more values of the same type to be passed into a single parameter in a function. This is indicated by appending `...` to the type annotation of the parameter.
[_Variadic parameters_][variadic-parameters] in Swift allow zero or more values of the same type to be passed into a single parameter in a function.
This is indicated by appending `...` to the type annotation of the parameter.

These values will be automatically grouped into an array with elements of the same type as the type of the variadic parameter.

Expand All @@ -14,5 +15,12 @@ func geometricMean(_ numbers: Double...) -> Double {
}

geometricMean(1, 2, 3, 4, 5)
// => 2.605171084697352
// Returns 2.605171084697352
```

When using variadic parameters, Swift has two limitations:

1. Swift only allows functions to have one variadic parameter. It has been decided that this restriction will be removed in a future release of Swift, but it is currently still in place.
2. If a function has parameters that follow the variadic parameter in the definition, the first parameter following the variadic parameter is _required_ to have an argument label.

[variadic-parameters]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Variadic-Parameters
4 changes: 2 additions & 2 deletions concepts/variadic-parameters/links.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"url": "https://docs.swift.org/swift-book/LanguageGuide/Functions.html",
"description": "Swift.org: Functions"
"url": "https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Variadic-Parameters",
"description": "Swift.org: Variadic Parameters"
}
]
18 changes: 9 additions & 9 deletions exercises/concept/lasagna-master/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
- Nested functions can be called as normal inside the functions the are defined within.
- Nested functions have access to all of the parameter, constants, and variables of the surrounding function. There is no need to pass them in to the nested function.

[functions]: https://docs.swift.org/swift-book/LanguageGuide/Functions.html
[type annotations]: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID312
[argument labels]: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID166
[multiple-return-values]: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID164
[implicit-returns]: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID607
[default-parameter-values]: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID169
[variadic-parameters]: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID171
[in-out-parameters]: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID173
[nested-functions]: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID178
[functions]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/
[type annotations]: [type annotations]: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID312
[argument labels]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Function-Argument-Labels-and-Parameter-Names
[multiple-return-values]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Functions-with-Multiple-Return-Values
[implicit-returns]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions#Functions-With-an-Implicit-Return
[default-parameter-values]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Default-Parameter-Values
[variadic-parameters]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Variadic-Parameters
[in-out-parameters]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#In-Out-Parameters
[nested-functions]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/functions/#Nested-Functions
Loading