Array

struct Array<Element>
  • SwifterSwift: Element at the given index if it exists.

    [1, 2, 3, 4, 5].item(at: 2) -> 3
    [1.2, 2.3, 4.5, 3.4, 4.5].item(at: 3) -> 3.4
    ["h", "e", "l", "l", "o"].item(at: 10) -> nil
    

    Declaration

    Swift

    public func item(at index: Int) -> Element?

    Parameters

    index

    index of element.

    Return Value

    optional element (if exists).

  • SwifterSwift: Return array with all duplicate elements removed.

    [1, 1, 2, 2, 3, 3, 3, 4, 5].duplicatesRemoved() -> [1, 2, 3, 4, 5])
    ["h", "e", "l", "l", "o"].duplicatesRemoved() -> ["h", "e", "l", "o"])
    

    Declaration

    Swift

    public func duplicatesRemoved() -> [Element]

    Return Value

    an array of unique elements.

  • SwifterSwift: All indexes of specified item.

    [1, 2, 2, 3, 4, 2, 5].indexes(of 2) -> [1, 2, 5]
    [1.2, 2.3, 4.5, 3.4, 4.5].indexes(of 2.3) -> [1]
    ["h", "e", "l", "l", "o"].indexes(of "l") -> [2, 3]
    

    Declaration

    Swift

    public func indexes(of item: Element) -> [Int]

    Parameters

    item

    item to check.

    Return Value

    an array with all indexes of the given item.

  • SwifterSwift: Remove last element from array and return it.

    [1, 2, 3, 4, 5].pop() // returns 5 and remove it from the array.
    [].pop() // returns nil since the array is empty.
    

    Declaration

    Swift

    @discardableResult public mutating func pop() -> Element?

    Return Value

    last element in array (if applicable).

  • SwifterSwift: Insert an element to the end of array.

    [1, 2, 3, 4].push(5) -> [1, 2, 3, 4, 5]
    ["h", "e", "l", "l"].push("o") -> ["h", "e", "l", "l", "o"]
    

    Declaration

    Swift

    public mutating func push(_ newElement: Element)

    Parameters

    newElement

    element to insert.

  • SwifterSwift: Swap values at index positions.

    [1, 2, 3, 4, 5].swap(from: 3, to: 0) -> [4, 2, 3, 1, 5]
    ["h", "e", "l", "l", "o"].swap(from: 1, to: 0) -> ["e", "h", "l", "l", "o"]
    

    Declaration

    Swift

    public mutating func swap(from index: Int, to otherIndex: Int)

    Parameters

    index

    index of first element.

    otherIndex

    index of other element.

  • SwifterSwift: Sum of all elements in array.

    [1, 2, 3, 4, 5].sum() -> 15
    

    Declaration

    Swift

    public func sum() -> Element

    Return Value

    sum of the array’s elements.

  • SwifterSwift: Average of all elements in array.

    [1.2, 2.3, 4.5, 3.4, 4.5].average() = 3.18
    

    Declaration

    Swift

    public func average() -> Element

    Return Value

    average of the array’s elements.

  • SwifterSwift: Insert an element at the beginning of array.

    [2, 3, 4, 5].prepend(1) -> [1, 2, 3, 4, 5]
    ["e", "l", "l", "o"].prepend("h") -> ["h", "e", "l", "l", "o"]
    

    Declaration

    Swift

    public mutating func prepend(_ newElement: Element)

    Parameters

    newElement

    element to insert.

  • SwifterSwift: Safely Swap values at index positions.

    [1, 2, 3, 4, 5].safeSwap(from: 3, to: 0) -> [4, 2, 3, 1, 5]
    ["h", "e", "l", "l", "o"].safeSwap(from: 1, to: 0) -> ["e", "h", "l", "l", "o"]
    

    Declaration

    Swift

    public mutating func safeSwap(from index: Index, to otherIndex: Index)

    Parameters

    index

    index of first element.

    otherIndex

    index of other element.

  • SwifterSwift: Get first index where condition is met.

    [1, 7, 1, 2, 4, 1, 6].firstIndex { $0 % 2 == 0 } -> 3
    

    Declaration

    Swift

    public func firstIndex(where condition: (Element) throws -> Bool) rethrows -> Index?

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    first index where the specified condition evaluates to true. (optional)

  • SwifterSwift: Get last index where condition is met.

    [1, 7, 1, 2, 4, 1, 8].lastIndex { $0 % 2 == 0 } -> 6
    

    Declaration

    Swift

    public func lastIndex(where condition: (Element) throws -> Bool) rethrows -> Index?

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    last index where the specified condition evaluates to true. (optional)

  • SwifterSwift: Get all indices where condition is met.

    [1, 7, 1, 2, 4, 1, 8].indices(where: { $0 == 1 }) -> [0, 2, 5]
    

    Declaration

    Swift

    public func indices(where condition: (Element) throws -> Bool) rethrows -> [Index]?

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    all indices where the specified condition evaluates to true. (optional)

  • SwifterSwift: Get last element that satisfies a conditon.

    [2, 2, 4, 7].last(where: {$0 % 2 == 0}) -> 4
    

    Declaration

    Swift

    public func last(where condition: (Element) throws -> Bool) rethrows -> Element?

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    the last element in the array matching the specified condition. (optional)

  • SwifterSwift: Filter elements based on a rejection condition.

    [2, 2, 4, 7].reject(where: {$0 % 2 == 0}) -> [7]
    

    Declaration

    Swift

    public func reject(where condition: (Element) throws -> Bool) rethrows -> [Element]

    Parameters

    condition

    to evaluate the exclusion of an element from the array.

    Return Value

    the array with rejected values filtered from it.

  • SwifterSwift: Get element count based on condition.

    [2, 2, 4, 7].count(where: {$0 % 2 == 0}) -> 3
    

    Declaration

    Swift

    public func count(where condition: (Element) throws -> Bool) rethrows -> Int

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    number of times the condition evaluated to true.

  • SwifterSwift: Iterate over a collection in reverse order. (right to left)

    [0, 2, 4, 7].forEachReversed({ print($0)}) -> //Order of print: 7,4,2,0
    

    Declaration

    Swift

    public func forEachReversed(_ body: (Element) throws -> Void) rethrows

    Parameters

    body

    a closure that takes an element of the array as a parameter.

  • SwifterSwift: Calls given closure with each element where condition is true.

    [0, 2, 4, 7].forEach(where: {$0 % 2 == 0}, body: { print($0)}) -> //print: 0, 2, 4
    

    Declaration

    Swift

    public func forEach(where condition: (Element) throws -> Bool, body: (Element) throws -> Void) rethrows

    Parameters

    condition

    condition to evaluate each element against.

    body

    a closure that takes an element of the array as a parameter.

  • SwifterSwift: Reduces an array while returning each interim combination.

    [1, 2, 3].accumulate(initial: 0, next: +) -> [1, 3, 6]
    

    Declaration

    Swift

    public func accumulate<U>(initial: U, next: (U, Element) throws -> U) rethrows -> [U]

    Parameters

    initial

    initial value.

    next

    closure that combines the accumulating value and next element of the array.

    Return Value

    an array of the final accumulated value and each interim combination.

  • SwifterSwift: Filtered and map in a single operation.

    [1,2,3,4,5].filtered({ $0 % 2 == 0 }, map: { $0.string }) -> ["2", "4"]
    

    Declaration

    Swift

    public func filtered<T>(_ isIncluded: (Element) throws -> Bool, map transform: (Element) throws -> T) rethrows ->  [T]

    Parameters

    isIncluded

    condition of inclusion to evaluate each element against.

    transform

    transform element function to evaluate every element.

    Return Value

    Return an filtered and mapped array.

  • Undocumented

    Declaration

    Swift

    public mutating func keep(while condition: (Element) throws -> Bool) rethrows -> [Element]
  • SwifterSwift: Take element of Array while condition is true.

    [0, 2, 4, 7, 6, 8].take( where: {$0 % 2 == 0}) -> [0, 2, 4]
    

    Declaration

    Swift

    public func take(while condition: (Element) throws -> Bool) rethrows -> [Element]

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    All elements up until condition evaluates to false.

  • SwifterSwift: Skip elements of Array while condition is true.

    [0, 2, 4, 7, 6, 8].skip( where: {$0 % 2 == 0}) -> [6, 8]
    

    Declaration

    Swift

    public func skip(while condition: (Element) throws-> Bool) rethrows -> [Element]

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    All elements after the condition evaluates to false.

  • SwifterSwift: Calls given closure with an array of size of the parameter slice where condition is true.

    [0, 2, 4, 7].forEach(slice: 2) { print($0) } -> //print: [0, 2], [4, 7]
    [0, 2, 4, 7, 6].forEach(slice: 2) { print($0) } -> //print: [0, 2], [4, 7], [6]
    

    Declaration

    Swift

    public func forEach(slice: Int, body: ([Element]) throws -> Void) rethrows

    Parameters

    slice

    size of array in each interation.

    body

    a closure that takes an array of slice size as a parameter.

  • SwifterSwift: Returns an array of slices of length size from the array. If array can’t be split evenly, the final slice will be the remaining elements.

    [0, 2, 4, 7].group(by: 2) -> [[0, 2], [4, 7]]
    [0, 2, 4, 7, 6].group(by: 2) -> [[0, 2], [4, 7], [6]]
    

    Declaration

    Swift

    public func group(by size: Int) -> [[Element]]?

    Parameters

    size

    The size of the slices to be returned.

    Return Value

    grouped self.

  • SwifterSwift: Group the elements of the array in a dictionary.

    [0, 2, 5, 4, 7].groupByKey { $0%2 ? "evens" : "odds" } -> [ "evens" : [0, 2, 4], "odds" : [5, 7] ]
    

    - Parameter getKey: Clousure to define the key for each element. - Returns: A dictionary with values grouped with keys.

    Declaration

    Swift

    public func groupByKey<K: Hashable>(keyForValue: (_ element: Element) throws -> K) rethrows -> [K: [Element]]
  • SwifterSwift: Separates an array into 2 arrays based on a predicate.

    [0, 1, 2, 3, 4, 5].divided { $0 % 2 == 0 } -> ( [0, 2, 4], [1, 3, 5] )
    

    Declaration

    Swift

    public func divided(by condition: (Element) throws -> Bool) rethrows -> (matching: [Element], nonMatching: [Element])

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    Two arrays, the first containing the elements for which the specified condition evaluates to true, the second containing the rest.

  • SwifterSwift: Returns a new rotated array by the given places.

    [1, 2, 3, 4].rotated(by: 1) -> [4,1,2,3]
    [1, 2, 3, 4].rotated(by: 3) -> [2,3,4,1]
    [1, 2, 3, 4].rotated(by: -1) -> [2,3,4,1]
    

    - Parameter places: Number of places that the array be rotated. If the value is positive the end becomes the start, if it negative it’s that start becom the end. - Returns: The new rotated array

    Declaration

    Swift

    public func rotated(by places: Int) -> [Element]
  • Undocumented

    Declaration

    Swift

    public mutating func rotate(by places: Int) -> [Element]
  • Undocumented

    Declaration

    Swift

    public mutating func shuffle() -> [Element]
  • SwifterSwift: Shuffled version of array. (Using Fisher-Yates Algorithm)

    [1, 2, 3, 4, 5].shuffled // return a shuffled version from given array e.g. [2, 4, 1, 3, 5].
    

    Declaration

    Swift

    public func shuffled() -> [Element]

    Return Value

    the array with its elements shuffled.

  • SwifterSwift: Return a sorted array based on an optional keypath.

    Declaration

    Swift

    public func sorted<T: Comparable>(by path: KeyPath<Element, T?>, ascending: Bool = true) -> [Element]

    Parameters

    path

    Key path to sort. The key path type must be Comparable.

    ascending

    If order must be ascending.

    Return Value

    Sorted array based on keyPath.

  • SwifterSwift: Return a sorted array based on a keypath.

    Declaration

    Swift

    public func sorted<T: Comparable>(by path: KeyPath<Element, T>, ascending: Bool = true) -> [Element]

    Parameters

    path

    Key path to sort. The key path type must be Comparable.

    ascending

    If order must be ascending.

    Return Value

    Sorted array based on keyPath.

  • Undocumented

    Declaration

    Swift

    public mutating func sort<T: Comparable>(by path: KeyPath<Element, T?>, ascending: Bool = true) -> [Element]
  • Undocumented

    Declaration

    Swift

    public mutating func sort<T: Comparable>(by path: KeyPath<Element, T>, ascending: Bool = true) -> [Element]
  • SwifterSwift: Check if array contains an array of elements.

    [1, 2, 3, 4, 5].contains([1, 2]) -> true
    [1.2, 2.3, 4.5, 3.4, 4.5].contains([2, 6]) -> false
    ["h", "e", "l", "l", "o"].contains(["l", "o"]) -> true
    

    Declaration

    Swift

    public func contains(_ elements: [Element]) -> Bool

    Parameters

    elements

    array of elements to check.

    Return Value

    true if array contains all given items.

  • SwifterSwift: All indices of specified item.

    [1, 2, 2, 3, 4, 2, 5].indices(of 2) -> [1, 2, 5]
    [1.2, 2.3, 4.5, 3.4, 4.5].indices(of 2.3) -> [1]
    ["h", "e", "l", "l", "o"].indices(of "l") -> [2, 3]
    

    Declaration

    Swift

    public func indices(of item: Element) -> [Index]

    Parameters

    item

    item to check.

    Return Value

    an array with all indices of the given item.

  • Undocumented

    Declaration

    Swift

    public mutating func removeAll(_ item: Element) -> [Element]
  • Undocumented

    Declaration

    Swift

    public mutating func removeAll(_ items: [Element]) -> [Element]
  • SwifterSwift: Remove all duplicate elements from Array.

    [1, 2, 2, 3, 4, 5].removeDuplicates() -> [1, 2, 3, 4, 5]
    ["h", "e", "l", "l", "o"]. removeDuplicates() -> ["h", "e", "l", "o"]
    

    Declaration

    Swift

    public mutating func removeDuplicates()
  • SwifterSwift: Return array with all duplicate elements removed.

    [1, 1, 2, 2, 3, 3, 3, 4, 5].duplicatesRemoved() -> [1, 2, 3, 4, 5])
    ["h", "e", "l", "l", "o"].duplicatesRemoved() -> ["h", "e", "l", "o"])
    

    Declaration

    Swift

    public func withoutDuplicates() -> [Element]

    Return Value

    an array of unique elements.

  • SwifterSwift: First index of a given item in an array.

    [1, 2, 2, 3, 4, 2, 5].firstIndex(of: 2) -> 1
    [1.2, 2.3, 4.5, 3.4, 4.5].firstIndex(of: 6.5) -> nil
    ["h", "e", "l", "l", "o"].firstIndex(of: "l") -> 2
    

    Declaration

    Swift

    public func firstIndex(of item: Element) -> Index?

    Parameters

    item

    item to check.

    Return Value

    first index of item in array (if exists).

  • SwifterSwift: Last index of element in array.

    [1, 2, 2, 3, 4, 2, 5].lastIndex(of: 2) -> 5
    [1.2, 2.3, 4.5, 3.4, 4.5].lastIndex(of: 6.5) -> nil
    ["h", "e", "l", "l", "o"].lastIndex(of: "l") -> 3
    

    Declaration

    Swift

    public func lastIndex(of item: Element) -> Index?

    Parameters

    item

    item to check.

    Return Value

    last index of item in array (if exists).