How can I traverse an array with stride? [duplicate] - swift

This question already has an answer here:
How can I do a Swift for-in loop with a step?
(1 answer)
Closed 3 years ago.
I want to traverse an array with a stride. For example, I have an array [0,1,2,3,4,5,6,7,8,9].And I want to traverse this array with a stride of 3. The oc code likes below:
for (int i = 0; i < array.count; i += 3) {
}
How can I do that with swift.

You can use a for loop with stride to traverse the array
let testarray = [0,1,2,3,4,5,6,7,8,9]
for i in stride(from: 0, to: testarray.count, by: 3) {
print(testarray[i])
}

Related

Swift loop from upper value to lower value [duplicate]

This question already has answers here:
How to iterate for loop in reverse order in swift?
(16 answers)
Closed 4 years ago.
I have a Swift for loop defined like this:
for i in 20...1 {
array.append(i)
}
But I get a crash with message
Thread 1: Fatal error: Can't form Range with upperBound < lowerBound
What is the fix?
You need to reverse the range:
for i in (1...20).reversed() {
array.append(i)
}
You cannot loop in reverse order like that, if you want you can try this :
for i in stride(from: 20, through: 1, by: -1) {
array.append(i)
}

How to create an array by multiplying another array in a functional way? [duplicate]

This question already has answers here:
Repeating array in Swift
(4 answers)
Closed 5 years ago.
I'd like to create an array that contains elements from another array multiplied by some Int value.
Example:
the following code
let arr = [1,2,3]
let multiplier = 3
print(function(arr, multiplier))
should return
[1,2,3,1,2,3,1,2,3]
I know how to make it using nested for loops, but I'm looking for some nifty functional way. I was thinking about map() function, but it iterates over each element of a given array, which is not my use case I suppose.
Main idea:
Create array of arrays,
flatMap to one-dimensional array.
Example:
let arr = [1, 2, 3]
let multiplayer = 3
print(Array(repeating: arr, count: multiplayer).flatMap({ $0 }))

how to rewrite the following for loop in swift 3 syntax? [duplicate]

This question already has answers here:
Fix warning "C-style for Statement is deprecated" in Swift 3
(4 answers)
Decrement index in a loop after Swift C-style loops deprecated
(5 answers)
Closed 6 years ago.
for (int i = n-2; i >= 0; --i)
{
....
}
the automatic translation of the above to swift 3 syntax is this
for i in n-2 ... 0
{
}
this doesn't work, because n could be 1, in the c syntax, this is valid, the loop won't be triggered,
but in the swift 3 syntax, this will cause runtime error.
for i in stride(from: n-2, through: 0, by: -1) {
}
for i in (0 ... n-2).reversed() {
}

Swift C-style loop [duplicate]

This question already has answers here:
How can I do a Swift for-in loop with a step?
(1 answer)
Express for loops in swift with dynamic range
(2 answers)
Closed 6 years ago.
for (var i = 1; i < 1024; i *= 2) {
print(i)
}
How can this be done with for in loop?
The given solution is for += operator not *= operator. Please provide a solution for *= thanks.
In Swift 3 you can do
for f in sequence(first: 1, next: { $0 < (1024 / 2) ? $0 * 2 : nil }) {
print(f)
}
The concept of the sequence function is described in the documentation.
Printing an infinite list is easy, the code would just be
for f in sequence(first: 1, next: {$0 * 2}) {
print(f)
}
Since we want the program to stop at some point, we us the ternary operator ? to terminate the list once we reach the maximum value.
Since the last value we want to print is 512, the last value we have to double is 256. For 512 which does not satisfy the condition < (1024 / 2) we have nil and thereby stop.

How to create an array with incremented values in Swift? [duplicate]

This question already has answers here:
Is there a way to instantly generate an array filled with a range of values in Swift?
(4 answers)
Closed 7 years ago.
I know that I can create an array with repeated values in Swift with:
var myArray = [Int](count: 5, repeatedValue: 0)
But is there a way to create an array with incremented values such as [0, 1, 2, 3, 4] other than to do a loop such as
var myArray = [Int]()
for i in 0 ... 4 {
myArray.append(i)
}
I know that code is pretty straightforward, readable, and bulletproof, but it feels like I should be able pass some function in some way to the array as it's created to provided the incremented values. It might not be worth the cost in readability or computationally more efficient, but I'm curious nonetheless.
Use the ... notation / operator:
let arr1 = 0...4
That gets you a Range, which you can easily turn into a "regular" Array:
let arr2 = Array(0...4)