Array

extension Array
  • Returns an Array containing the results of mapping transform over self. The transform provides not only each element of the array but also the index of tha item in the array.

    let items: [SomeObject] = existingArray.mapWithIndex { index, response in
        return SomeObject(index: index, description: response.body)
    }
    

    Declaration

    Swift

    public func mapWithIndex<T>(_ fn: (Int, Element) throws -> T) rethrows -> [T]

    Parameters

    f

    The transform that given an element of the array and an index returns an element of type T

    Return Value

    The array created by applying the transform to this array.

  • Rotates an array by the number of places indicated by shift.

    This is useful for infinitely scrolling visuals, but where the data backing those visuals is finite.

    var array = [1, 2, 3, 4, 5]
    
    let x = array.rotated(by: 2)   // x should be [3, 4, 5, 1, 2]
    let y = array.rotated(by: -2)  // y should be [4, 5, 1, 2, 3]
    

    Declaration

    Swift

    public func rotated(by shift: Int) -> Array

    Parameters

    shift

    The number of indices by which the array should be shifted. Positive shifts right, negative shifts left.

    Return Value

    Returns the rotated array.

  • Rotates self in-place.

    Declaration

    Swift

    public mutating func rotate(by shift: Int)
  • Removes and returns the specified element from the array

    Declaration

    Swift

    @discardableResult
    public mutating func remove(_ element: Element) -> Element?

    Parameters

    element

    The element to remove from the array

    Return Value

    The removed element or nil, if the element was not found

  • Returns the element before the specified element.

    let foo = [1,2,3]
    let two = foo.before(3)
    let one = foo.before(2)
    let nope = foo.before(1)
    

    Declaration

    Swift

    public func before(_ element: Element) -> Element?

    Parameters

    element

    The element to index against.

    Return Value

    The element before the index element, or nil if there isn’t one.

  • Returns the element after the specified element.

    let foo = [1,2,3]
    let twp = foo.after(1)
    let three = foo.after(2)
    let nope = foo.after(3)
    

    Declaration

    Swift

    public func after(_ element: Element) -> Element?

    Parameters

    element

    The element to index against.

    Return Value

    The element after the index element, or nil if there isn’t one.

  • Returns an array containing no duplicates.

    Performance analysis:

    Three implementation were explored:

    Set

    public func unique() -> [Element] {
       return Array(Set(self))
    }
    

    Reduce

    public func unique() -> [Element] {
       return reduce([]) { !$0.contains($1) ? $0 + [$1] : $0 }
    }
    

    For-Loop

    public func unique() -> [Element] {
       var array: [Element] = []
       for element in self where !array.contains(element) {
           array.append(element)
       }
       return array
    }
    

    Performance tests were run on each implementation to determine which performed best. Starting with a 20 element array, we ran unique() 10,000 times and measured the execution time. This test was run 5 times. The average execution times are shown below.

    Set : 1.082s

    Reduce : 1.75s

    For-Loop : 0.952s

    The performance tests showed that the For-Loop implementation was the most performant. Reduce performed poorly, and while the Set implementation achieved similar performance to the For-Loop implementation, the order of the elements in the array is not preserved when using a Set.

    * Tests were run using Xcode 7.2 and Swift 2.1.

    Declaration

    Swift

    public func unique() -> [Element]
  • Removes duplicates from self in-place.

    Declaration

    Swift

    public mutating func formUnique()