The flatten function in terms of reduce

And finally, let's implement the flatten function in terms of reduce:

func flattenIntermsOfReduce<Element>(elements: [[Element]]) -> [Element] { 
return elements.reduce([]) { $0 + $1 }
}

let flattened = flattenIntermsOfReduce(elements: aTwoDimArrayOfNumbers)

flattened will be [1, 3, 5, 2, 4, 6].

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset