-
-
Notifications
You must be signed in to change notification settings - Fork 159
[Swift 6]: Rework for-loops concept #824
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
meatball133
wants to merge
8
commits into
exercism:main
Choose a base branch
from
meatball133:rework-for-loops
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f72e3f0
Update Swift tools version to 6.0 and add swift-numerics dependency
meatball133 fea42a8
Merge branch 'update-exercise-batch-17' into main
meatball133 f200f48
Add BirdWatcher exercise with initial implementation and documentation
meatball133 3701026
Update concept
meatball133 5daa40a
Update config
meatball133 4fce93e
format config file
meatball133 ea84733
Remove for loop concept mention
meatball133 3b51410
Changes based on feedback
meatball133 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"blurb": "For loops can be used to iterate over a sequence of values.", | ||
"authors": [ | ||
"wneumann" | ||
"wneumann", | ||
"meatball133" | ||
], | ||
"contributors": [] | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,84 +1,120 @@ | ||
# About | ||
# For loops | ||
|
||
[For-in loops][for-in-loops] are used to iterate over a sequence of values, taking each element in turn, binding it to a variable or constant name of the developer's choosing, then executes a block of code that may refer to the element. When every element of the sequence has been iterated over, the loop exits and execution begins with the first line following the body of the loop. | ||
Looping is a fundamental concept in programming that allows you to execute a block of code multiple times. | ||
In Swift, there are two types of loops: [`for-in` loops][for-loops] and `while` loops. | ||
In this chapter, you'll learn about `for-in` loops. | ||
|
||
For loops allows you to iterate over a sequence of values, taking each element in turn, binding it to a variable of your choosing. | ||
Swift allows you to iterate over a variety of sequences, such as ranges, arrays, and strings (and more types which will be covered later). | ||
When every element of the sequence has been iterated over, the loop exits. | ||
|
||
For loops are declared by using the `for` keyword, followed by a variable name, the `in` keyword, and a sequence of values to iterate over. | ||
The variable given in the `for-in` loop is inmutable, meaning you can't change its value inside the loop. | ||
Here's an example of a `for-in` loop that iterates over an array of numbers: | ||
|
||
```swift | ||
let numbers = [3, 10, 7, 11] | ||
let word = "Supercalifragilisticexpialidocious" | ||
|
||
for number in numbers { | ||
print("\(number) / 2 = \(number / 2)") | ||
print(number) | ||
} | ||
print("Done with numbers") | ||
|
||
// prints: | ||
// 3 / 2 = 1 | ||
// 10 / 2 = 5 | ||
// 7 / 2 = 3 | ||
// 11 / 2 = 5 | ||
// 3 | ||
// 10 | ||
// 7 | ||
// 11 | ||
// Done with numbers | ||
``` | ||
|
||
~~~~exercism/note | ||
The `number` variable is declared in the `for-in` loop and is only available within the loop's scope. | ||
|
||
for char in word { | ||
if "aeiou".contains(char) { | ||
print(char, terminator: "") | ||
} | ||
} | ||
print(" - those are all the vowels") | ||
|
||
// prints: | ||
// ueaiaiiieiaioiou - those are all the vowels | ||
```swift | ||
let numbers = [3, 10, 7, 11] | ||
|
||
for number in numbers { | ||
number + 1 | ||
} | ||
number + 1 // Error: Use of unresolved identifier 'number' | ||
``` | ||
~~~~ | ||
|
||
## Iterating over a range | ||
|
||
If one needs to mutate the current element of the iteration, it can be declared as a variable in the for-in loop: | ||
You can also iterate over a range of numbers using a `for-in` loop. | ||
This allows you to execute a block of code a specific number of times, for example, the range `1...5` will iterate over the numbers 1, 2, 3, 4, and 5, so the loop will execute 5 times. | ||
Sometimes you might want to iterate over indexes, in a datastructure like an array, then you can use a `0..<array.count` range. | ||
|
||
```swift | ||
for var x in [123, 900, 7] { | ||
while x > 0 { | ||
print(x % 10) | ||
x /= 10 | ||
} | ||
print() | ||
let numbers = [3, 10, 7, 11] | ||
|
||
for i in 0..<numbers.count { | ||
print(numbers[i]) | ||
} | ||
|
||
// prints: | ||
// 3 | ||
// 2 | ||
// 1 | ||
// | ||
// 0 | ||
// 0 | ||
// 9 | ||
// | ||
// 10 | ||
// 7 | ||
// 11 | ||
``` | ||
|
||
If one wants to execute a loop a specified number of times, a for-in loop can be used with a range supplied for the sequence to iterate over: | ||
## Iterating over a string | ||
|
||
You can also iterate over a string using a `for-in` loop. | ||
This allows you to iterate over each character in the string, and note specifically that the type given in the loop is a `Character`. | ||
|
||
```swift | ||
for i in 1...3 { | ||
print("i: \(i)") | ||
let message = "Hello!" | ||
|
||
for character in message { | ||
print(character) | ||
} | ||
|
||
// prints: | ||
// i: 1 | ||
// i: 2 | ||
// i: 3 | ||
// H | ||
// e | ||
// l | ||
// l | ||
// o | ||
// ! | ||
``` | ||
|
||
If the body of the loop doesn't refer to the current element of the sequence, an underscore (`_`) can be supplied for the name: | ||
## Unusued variables | ||
|
||
If you don't need the value of the variable in the loop, you can use an underscore `_` to ignore it. | ||
|
||
```swift | ||
for _ in 1...3 { | ||
print("Perhaps.") | ||
let numbers = [3, 10, 7, 11] | ||
|
||
for _ in numbers { | ||
print("Hello!") | ||
} | ||
``` | ||
|
||
// prints: | ||
// Perhaps. | ||
// Perhaps. | ||
// Perhaps. | ||
## stride | ||
|
||
Swift also provides a `stride` function that allows you to create a sequence over a range with a specific step. | ||
Which can be then iterated over using a `for-in` loop. | ||
`stride` is defined as [`stride(from:to:by:)`][stride-to] or [`stride(from:through:by:)`][stride-through], the first one is exclusive and the second one is inclusive. | ||
|
||
```swift | ||
for i in stride(from: 0, to: 10, by: 2) { | ||
print(i) | ||
} | ||
|
||
// prints: | ||
// 0 | ||
// 2 | ||
// 4 | ||
// 6 | ||
// 8 | ||
``` | ||
|
||
[for-in-loops]: https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID121 | ||
Note that the `to` parameter is exclusive, so the loop will iterate until the number before the `to` parameter, while the `through` parameter is inclusive, so in this case it would also include the `10`. | ||
|
||
[stride-to]: https://developer.apple.com/documentation/swift/stride(from:to:by:) | ||
[stride-through]: https://developer.apple.com/documentation/swift/stride(from:through:by:) | ||
[for-loops]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow/#For-In-Loops |
This file contains hidden or 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 |
---|---|---|
@@ -1,19 +1,108 @@ | ||
# Introduction | ||
# For loops | ||
|
||
For-in loops are used to iterate over a sequence of values, taking each element in turn, binding it to a variable or constant name of the developer's choosing, then executes a block of code that may refer to the element. When every element of the sequence has been iterated over, the loop exits and execution begins with the first line following the body of the loop. | ||
Looping is a fundamental concept in programming that allows you to execute a block of code multiple times. | ||
In Swift, there are two types of loops: [`for-in` loops][for-loops] and `while` loops. | ||
In this chapter, you'll learn about `for-in` loops. | ||
|
||
For loops allows you to iterate over a sequence of values, taking each element in turn, binding it to a variable of your choosing. | ||
Swift allows you to iterate over a variety of sequences, such as ranges, arrays, and strings (and more types which will be covered later). | ||
When every element of the sequence has been iterated over, the loop exits. | ||
|
||
For loops are declared by using the `for` keyword, followed by a variable name, the `in` keyword, and a sequence of values to iterate over. | ||
The variable given in the `for-in` loop is inmutable, meaning you can't change its value inside the loop. | ||
Here's an example of a `for-in` loop that iterates over an array of numbers: | ||
|
||
```swift | ||
let numbers = [3, 10, 7, 11] | ||
|
||
for number in numbers { | ||
print("\(number) / 2 = \(number / 2)") | ||
print(number) | ||
} | ||
print("Done with numbers") | ||
|
||
// prints: | ||
// 3 / 2 = 1 | ||
// 10 / 2 = 5 | ||
// 7 / 2 = 3 | ||
// 11 / 2 = 5 | ||
// 3 | ||
// 10 | ||
// 7 | ||
// 11 | ||
// Done with numbers | ||
``` | ||
|
||
~~~~exercism/note | ||
The `number` variable is declared in the `for-in` loop and is only available within the loop's scope. | ||
|
||
```swift | ||
let numbers = [3, 10, 7, 11] | ||
|
||
for number in numbers { | ||
number + 1 | ||
} | ||
number + 1 // Error: Use of unresolved identifier 'number' | ||
``` | ||
~~~~ | ||
|
||
## Iterating over a range | ||
|
||
You can also iterate over a range of numbers using a `for-in` loop. | ||
This allows you to execute a block of code a specific number of times, for example, the range `1...5` will iterate over the numbers 1, 2, 3, 4, and 5, so the loop will execute 5 times. | ||
Sometimes you might want to iterate over indexes, in a datastructure like an array, then you can use a `0..<array.count` range. | ||
|
||
```swift | ||
let numbers = [3, 10, 7, 11] | ||
|
||
for i in 0..<numbers.count { | ||
print(numbers[i]) | ||
} | ||
|
||
// prints: | ||
// 3 | ||
// 10 | ||
// 7 | ||
// 11 | ||
``` | ||
|
||
## Iterating over a string | ||
|
||
You can also iterate over a string using a `for-in` loop. | ||
This allows you to iterate over each character in the string, and note specifically that the type given in the loop is a `Character`. | ||
|
||
```swift | ||
let message = "Hello!" | ||
|
||
for character in message { | ||
print(character) | ||
} | ||
|
||
// prints: | ||
// H | ||
// e | ||
// l | ||
// l | ||
// o | ||
// ! | ||
``` | ||
|
||
## stride | ||
|
||
Swift also provides a `stride` function that allows you to create a sequence over a range with a specific step. | ||
Which can be then iterated over using a `for-in` loop. | ||
`stride` is defined as [`stride(from:to:by:)`][stride-to] or [`stride(from:through:by:)`][stride-through], the first one is exclusive and the second one is inclusive. | ||
|
||
```swift | ||
for i in stride(from: 0, to: 10, by: 2) { | ||
print(i) | ||
} | ||
|
||
// prints: | ||
// 0 | ||
// 2 | ||
// 4 | ||
// 6 | ||
// 8 | ||
``` | ||
|
||
Note that the `to` parameter is exclusive, so the loop will iterate until the number before the `to` parameter, while the `through` parameter is inclusive, so in this case it would also include the `10`. | ||
|
||
[stride-to]: https://developer.apple.com/documentation/swift/stride(from:to:by:) | ||
[stride-through]: https://developer.apple.com/documentation/swift/stride(from:through:by:) | ||
[for-loops]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow/#For-In-Loops |
This file contains hidden or 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 |
---|---|---|
@@ -1 +1,11 @@ | ||
[] | ||
[ | ||
{ | ||
"url": "https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow/#For-In-Loops", | ||
"description": "Swift Book: For-In Loops" | ||
}, | ||
{ | ||
"url": "https://developer.apple.com/documentation/swift/stride(from:through:by:)", | ||
"description": "Swift docs: stride" | ||
} | ||
] | ||
|
This file contains hidden or 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 hidden or 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,21 @@ | ||
# Hints | ||
|
||
## 1. Determine the total number of birds that you counted so far | ||
|
||
- Refer to the exercise introduction for an example how to use a for loop to iterate over a slice. | ||
- Use a helper variable to store the total count and increase that variable as you go through the slice. | ||
- Think about the correct initial value for that helper variable. | ||
|
||
## 2. Calculate the number of visiting birds in a specific week | ||
|
||
- This task is similar to the first one, you can copy your code as a starting point. | ||
- Think about which indexes in the slice you would need to take into account for week number 1 and 2, respectively. | ||
- Now find a general way to calculate the first and the last index that should be considered. | ||
- With that you can set up the for loop to only iterate over the relevant section of the slice. | ||
|
||
## 3. Fix a counting mistake | ||
|
||
- Again you need to set up a for loop to iterate over the whole bird count slice. | ||
- This time you only need to visit every second entry in the slice. | ||
- Change the step so the counter variable is increased accordingly after each iteration. | ||
- In the body of the for loop you can use the increment operator to change the value of an element in a slice in-place. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.