Array

public extension Array
public extension Array where Element: Equatable
  • Flattens the current Array of any type

    This method will help you convert an array like [ [1, 2], [3, 4], 5 ] to [1, 2, 3, 4, 5] Here is how you use it:

    let array = [
        [1, 2],
        [3, 4], 5
    ]
    
    array.flatten() // => [1, 2, 3, 4, 5]
    

    Declaration

    Swift

    func flatten(_ index: Int = 0) -> [Any]

    Parameters

    index

    The starting index of the array

  • This will create a range of number starting from the number a, going to the number b with x numbers in it.

    If you don’t have any idea of how it works, the example may help you:

    Array<Any>.linspace(start: 0, end: 100, n: 10) // => [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
    

    Declaration

    Swift

    static func linspace(start: Double, end: Double, n: Int) -> [Double]

    Parameters

    start

    The start of the array

    end

    The end of your array

    n

    The length of the outputed array

  • Similar as Array.range, but with more options (start, end, step)

    Example:

    Array<Any>.arange(start: 1, end: 11, step: 2, offset: 0) // => [ 1, 3, 5, 7, 9, 11 ]
    

    Declaration

    Swift

    static func arange(start: Double, end: Double, step: Double = 1, offset: Double = 0) -> [Double]

    Parameters

    start

    The start of the array

    end

    The end of the array

    step

    The step the function will use to generate the array

    offset

    The offset you want to use to generate your array

  • This will create a range of number, starting from 0 up to a number n

    Example:

    Array<Any>.range(10) // => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    

    Declaration

    Swift

    static func range(_ n: Int) -> [Int]

    Parameters

    n

    The number n represent the end of the range

  • The reshape function is the opposite of the Array.flatten function. It will group items in an array by x items:

    Example:

    Array<Any>.range(10).reshape(part: 2)
    /* => [
        [0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9],
        [10]
    ] */
    

    Declaration

    Swift

    func reshape(part: Int) -> [[Element]]

    Parameters

    part

    The length of each part

Available where Element: Equatable