In Swift, is there a clever way of using the higher order methods on Array to return the 5 first objects? The obj-c way of doing it was saving an index, and for-loop throug
With Swift 5, according to your needs, you may choose one of the 6 following Playground codes in order to solve your problem.
subscript(_:)
subscriptlet array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
let arraySlice = array[..<5]
//let arraySlice = array[0..<5] // also works
//let arraySlice = array[0...4] // also works
//let arraySlice = array[...4] // also works
let newArray = Array(arraySlice)
print(newArray) // prints: ["A", "B", "C", "D", "E"]
prefix(_:)
methodComplexity: O(1) if the collection conforms to RandomAccessCollection
; otherwise, O(k), where k is the number of elements to select from the beginning of the collection.
let array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
let arraySlice = array.prefix(5)
let newArray = Array(arraySlice)
print(newArray) // prints: ["A", "B", "C", "D", "E"]
Apple states for prefix(_:)
:
If the maximum length exceeds the number of elements in the collection, the result contains all the elements in the collection.
prefix(upTo:)
methodComplexity: O(1)
let array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
let arraySlice = array.prefix(upTo: 5)
let newArray = Array(arraySlice)
print(newArray) // prints: ["A", "B", "C", "D", "E"]
Apple states for prefix(upTo:)
:
Using the
prefix(upTo:)
method is equivalent to using a partial half-open range as the collection's subscript. The subscript notation is preferred overprefix(upTo:)
.
prefix(through:)
methodlet array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
let arraySlice = array.prefix(through: 4)
let newArray = Array(arraySlice)
print(newArray) // prints: ["A", "B", "C", "D", "E"]
removeSubrange(_:)
methodComplexity: O(n), where n is the length of the collection.
var array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
array.removeSubrange(5...)
print(array) // prints: ["A", "B", "C", "D", "E"]
dropLast(_:)
methodComplexity: O(1) if the collection conforms to RandomAccessCollection
; otherwise, O(k), where k is the number of elements to drop.
let array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
let distance = array.distance(from: 5, to: array.endIndex)
let arraySlice = array.dropLast(distance)
let newArray = Array(arraySlice)
print(newArray) // prints: ["A", "B", "C", "D", "E"]
Swift 4
To get the first N elements of a Swift array you can use prefix(_ maxLength: Int)
:
Array(largeArray.prefix(5))
Update for swift 4:
[0,1,2,3,4,5].enumerated().compactMap{ $0 < 10000 ? $1 : nil }
For swift 3:
[0,1,2,3,4,5].enumerated().flatMap{ $0 < 10000 ? $1 : nil }
Plain & Simple
extension Array {
func first(elementCount: Int) -> Array {
let min = Swift.min(elementCount, count)
return Array(self[0..<min])
}
}
let a: [Int] = [0, 0, 1, 1, 2, 2, 3, 3, 4]
let b: [Int] = Array(a.prefix(5))
// result is [0, 0, 1, 1, 2]
Swift 4 with saving array types
extension Array {
func take(_ elementsCount: Int) -> [Element] {
let min = Swift.min(elementsCount, count)
return Array(self[0..<min])
}
}