How can I flatten an array swiftily in Swift?

前端 未结 3 1358
说谎
说谎 2021-01-17 10:23

I want to turn this:

let x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

into this:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

ver

3条回答
  •  时光取名叫无心
    2021-01-17 11:27

    With Swift 5, you can choose one of the three following ways in order to flatten an array.


    #1. Flatten an array with Array's flatMap(_:) method

    With Swift, types that conform to Sequence protocol (including Array) have a flatMap(_:) method. Array's flatMap(_:) has the following declaration:

    func flatMap(_ transform: (Element) throws -> SegmentOfResult) rethrows -> [SegmentOfResult.Element] where SegmentOfResult : Sequence
    

    Returns an array containing the concatenated results of calling the given transformation with each element of this sequence.

    The Playground sample code below shows how to flatten an Array of type [[Int]] to type [Int] using flatMap(_:):

    let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    let flattenArray = array.flatMap({ (element: [Int]) -> [Int] in
        return element
    })
    print(flattenArray) // prints [1, 2, 3, 4, 5, 6, 7, 8, 9]
    

    #2. Flatten an array with Array's joined() method

    Array's has a method called joined(). joined() has the following declaration:

    func joined() -> FlattenSequence>
    

    Returns the elements of this sequence of sequences, concatenated.

    The following Playground sample code shows how to flatten an Array of type [[Int]] to type [Int] using joined():

    let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    let flattenCollection = array.joined()
    let flattenArray = Array(flattenCollection)
    print(flattenArray) // prints [1, 2, 3, 4, 5, 6, 7, 8, 9]
    

    #3. Flatten an array with Array's reduce(_:_:) method

    Swift Array has a reduce(_:_:) method with the following declaration:

    func reduce(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> Result
    

    Returns the result of combining the elements of the sequence using the given closure.

    The following Playground sample code shows how to flatten an Array of type [[Int]] to type [Int] using reduce(_:_:):

    let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    let flattenArray = array.reduce([], { (result: [Int], element: [Int]) -> [Int] in
        return result + element
    })
    print(flattenArray) // prints [1, 2, 3, 4, 5, 6, 7, 8, 9]
    

    As an alternative to reduce(_:_:), you can use reduce(into:_:):

    let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    let flattenArray = array.reduce(into: [Int]()) { (result: inout [Int], element: [Int]) in
        result += element
    }
    print(flattenArray) // prints [1, 2, 3, 4, 5, 6, 7, 8, 9]
    

提交回复
热议问题