Reduce array to a single value


I have an array and want to recursively perform a specific action on all elements and get one single value/result.


// Initialize the Array
var a = [1,2,3,4,5,6]

// Recursively perform the defined action ($0 + $1)
// to all elements and get result

var sum = a.reduce(0) { $0 + $1 }

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)