Loop through a range of an Array [duplicate] - swift

This question already has answers here:
How to loop through an array from the second element in elegant way using Swift
(5 answers)
Closed 3 years ago.
How can I loop through an array range? Example if I had 5 objects in an array. I want to loop from index [3] to end of the array in this example it would go through and update objects 3-5 and skip objects 1 & 2. This is what I have so far using the stride method(this code isn't working). Is this the correct method? How can I achieve this?
stride(from: markers[index], to: markers.endIndex, by: 1).forEach { i in
// Do something for each array object
}

You can use the range operator to get sequences of indices or slices of the array. Which you use depends on what you are trying to do. For clarity I am going to leave out error checking.
For example:
let letters = ["a", "b", "c", "d", "e"]
letters[3...].forEach { print($0) } // prints d e
// or you can print up to index 3
letters[...3].forEach { print($0) } // prints a b c d
// or print elements 1-3 inclusive
letters[1...3].forEach { print($0) } // prints b c d
// or print elements 1-3 excluding index 3
letters[1..<3].forEach { print($0) } // prints b c d
If you wanted to modify the elements of the array you pass in the indices rather than the elements
var mutableLetters = ["a","b","c","d","e"]
(3..<mutableLetters.count).forEach {
mutableLetters[$0] = mutableLetters[$0].uppercased()
}
notice here we need to specify both limits because the range knows nothing about the array.
It's often more Swifty not to modify things in place so, if this fits your use case you might consider something like this:
let immutableLetters = ["a","b","c","d","e"]
let upperCasedFromThreeOn = immutableLetters[3...].map { $0.uppercased() }
// upperCasedFromThreeOn = ["D","E"]
As a final note, sometimes you need to know both the index and the element. You can use a forEach on the indices as above, but another way is to use enumerated() this creates a tuple of the index and element.
let range = 2...4
immutableLetters.enumerated()
.filter { (index,_) in range.contains(index) }
.forEach { (index, element) in
print("\(index) \(element)")
}
Here I've used a filter after the enumeration so that the indices match the original array.

You can simply iterate over your array slice dropping the first n elements:
let markers = ["a","b","c","d","e"]
for marker in markers.dropFirst(2) {
print(marker) // this will print 'c d e'
}
If you need to change your array you can iterate over a slice of its indices:
let markers = ["a","b","c","d","e"]
for index in markers.indices.dropFirst(2) {
print(markers[index])
}

You can simply loop through the array using Range Operator in Swift like,
var markers = ["M1","M2","M3","M4","M5"]
let count = markers.count
if count > 2 {
for i in 2..<count {
//add your code here..
}
}
In the above code, I've used half-open range operator(..<)

Related

Iterating backwards with for-loop doesn't work

I'm a newbie on Swift and I'm really confused about how to iterate backward with for-loop.
my array:
let arr = ["a", "b", "c"]
Trying for-loop that i googled:
for i in stride(from: arr.count, through: 0, by: -1) {
print(arr[i]) }
Output: Terminated by signal 4
Another attempt that doesn't work:
for i in arr.count...0 {
print(arr[i])
}
what am i doing wrong?
Both of them doesn't work because you start at arr.count, which is always an invalid index for an array. The last valid index is arr.count - 1, so changing the start of the stride/range to that will fix the problem.
If you want to iterate through the indices in reverse, you can just get the indices and reverse it:
for i in arr.indices.reversed() {
let element = arr[i]
}
Alternatively, you can use enumerated().reversed(), but note that reversed() here will first create an extra array to hold the reversed indices and elements, which means that you will be looping through arr an extra time.
for (i, element) in arr.enumerated().reversed() {
}
You missed the point that array indices – in almost all programming languages – are zero based, so the last index is count - 1
for i in stride(from: arr.count - 1, through: 0, by: -1) {
print(arr[i])
}

Swift lazy subscript ignores filter

How does subscripting a lazy filter work?
let ary = [0,1,2,3]
let empty = ary.lazy.filter { $0 > 4 }.map { $0 + 1 }
print(Array(empty)) // []
print(empty[2]) // 3
It looks like it just ignores the filter and does the map anyway. Is this documented somewhere? What other lazy collections have exceptional behavior like this?
It comes down to subscripting a LazyFilterCollection with an integer which in this case ignores the predicate and forwards the subscript operation to the base.
For example, if we're looking for the strictly positive integers in an array :
let array = [-10, 10, 20, 30]
let lazyFilter = array.lazy.filter { $0 > 0 }
print(lazyFilter[3]) // 30
Or, if we're looking for the lowercase characters in a string :
let str = "Hello"
let lazyFilter = str.lazy.filter { $0 > "Z" }
print(lazyFilter[str.startIndex]) //H
In both cases, the subscript is forwarded to the base collection.
The proper way of subscripting a LazyFilterCollection is using a LazyFilterCollection<Base>.Index as described in the documentation :
let start = lazyFilter.startIndex
let index = lazyFilter.index(start, offsetBy: 1)
print(lazyFilter[index])
Which yields 20 for the array example, or l for the string example.
In your case, trying to access the index 3:
let start = empty.startIndex
let index = empty.index(start, offsetBy: 3)
print(empty)
would raise the expected runtime error :
Fatal error: Index out of range
To add to Carpsen90's answer, you run into one of Collection's particularities: it's not recommended, nor safe to access collections by an absolute index, even if the type system allows this. Because the collection you receive might be a subset of another one.
Let's take a simpler example, array slicing:
let array = [0, 1, 2, 3, 4]
let slice = array[2..<3]
print(slice) // [2]
print(slice.first) // Optional(2)
print(slice[0]) // crashes with array index out of bounds
Even if slice is a collection indexable by an integer, it's still unsafe to use absolute integers to access elements of that collection, as the collection might have a different set of indices.

Swift Forin-where perform filter or skip (continue)?

I just gain a knowledge about Swift's forin-where, but it seems very lack of documents. So there's a question in my mind: Does it perform filter then loop or just loop with condition? given the below code
var arr = [1, 2, 3, 4, 5]
for i in arr where i > 3 {
print(i)
}
does the machine do like this:
for i in arr.filter { $0 > 3 }
or like this?
for i in arr {
guard i > 3 else { continue }
print(i)
}
Should I use forin-where or just filter then foreach?
It is the latter (iterate over all elements, execute the body only for
elements satisfying the condition).
The for-statements takes an arbitrary sequence, not only arrays.
Filtering the sequence first would not only be inefficient (memory- and
time-wise), but also impossible for sequences producing “infinitely many” values, like in this example:
for x in 1... where x % 3 == 0 {
print(x)
if x > 10 { break }
}
Here 1... is a “partial range” representing all integers greater than
or equal to one.

safely remove item while iterating backward in Swift 3

When I want to pass through and remove an item or items from an array (when certain conditions are met), I typically iterate backward in the C-style for-loop and remove the item by index, avoiding the problem of index numbers being changed of the next item to be processed, or the changing size of the list affecting how many times the loop is passed through. But the C for-loop has been removed in Swift 3.
Here is my Swift 2.3 code for the initialization of the loop:
for (var i = allowedItems.count - 1; i > -1; i -= 1)
Here is the monstrosity created by the Swift 3 converter:
for (i in ((-1 + 1)...allowedItems.count - 1).reversed())
This version does not compile however. ("Expected ',' separator" at the "in" operator).
I simplify the "-1 + 1" bit to zero:
for (i in (0...allowedItems.count - 1).reversed())
Now the error is "Expected Sequence expression for for-each loop".
What is the safe and hopefully reasonably elegant way of iterating backward in Swift 3, in which an index or counter variable is made available for use in specifying which item should be removed? This type of logic appears a number of places in my code so I want to make sure to find the best solution.
Thanks.
What is the safe and hopefully reasonably elegant way of iterating backward in Swift 3
The built-in way is:
for i in (0 ..< allowedItems.count).reversed()
The elegant way is:
for i in allowedItems.count >>> 0
(where >>> is the custom operator that I define here).
Use stride:
for i in stride(from: allowedItems.count - 1, through: 0, by: -1) {
}
What is the safe and hopefully reasonably elegant way of iterating
backward in Swift 3, in which an index or counter variable is made
available for use in specifying which item should be removed?
This doesn't answer the technical question, but possibly the underlying XY problem: have you considered simply filtering your array based on the criteria "when certain conditions are met"?
func certainConditionsForKeepingAreMet(_ element: YourElementType) -> Bool { /* ... */ }
allowedItems = allowedItems.filter(certainConditionsForKeepingAreMet)
E.g.
var allowedItems = [1, 3 ,6, 2]
func certainConditionsForKeepingAreMet(_ element: Int) -> Bool { return element < 3 }
allowedItems = allowedItems.filter(certainConditionsForKeepingAreMet)
print(allowedItems) // [1, 2]
If you'd like to remove and use the removed elements (on-the-fly), you could simply pipe the elements that are to be removed to some "use this element" function, in the course of checking the conditions for the elements.
func doSomethingWith(_ element: Int) { print("Removed", element) }
func certainConditionsForKeepingAreMet(_ element: Int) -> Bool {
if element >= 3 {
doSomethingWith(element)
return false
}
return true
}
var allowedItems = [1, 3 ,6, 2]
allowedItems = allowedItems.filter(certainConditionsForKeepingAreMet)
/* Removed 3
Removed 6 */
print(allowedItems) // [1, 2]

Adding integers from a dictionary together

Looking to add together integers from a dictionary. For example:
var dictionary = ["one": 1, "two": 2, "three": 3, "four": 4, "five": 5]
I would want to get the sum of 1+2+3+4+5 = 15
I understand it will probably need a loop something like
for (n, i) in dictionary {
*some math function*
}
any help would be appreciated maybe I'm just over thinking this one?
You can use reduce:combine: to get the sum.
With Swift 2.0, reduce:Combine: is added to the protocol extension of SequenceType. So, it is available to all SequenceType like Array, Set or Dictionary.
dictionary.reduce(0) {
sum, item in
return sum + item.1
}
item inside the closure is tuple representing each (key, value) pair. So, item.0 is key where as item.1 is value.The initial value of the sum is 0, and then each time the iteration takes place, sum is added to the value extracted from dictionary.
You could also write it in short as,
dictionary.reduce(0) { return $0 + $1.1 }
While older version of Swift, it has reduce method with Array only. So, we could first get array and apply reduce:combine to get the sum as,
let a = dictionary.values.array.reduce(0) { return $0 + $1 }