Reduce array to a single value

 Problem

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



 Recipe


// 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 }
print(sum)
				


 Playground