Skip to content

Instantly share code, notes, and snippets.

@lukewakeford
Last active January 8, 2019 14:29
Show Gist options
  • Save lukewakeford/3afb3537fd1346504078e805d5f659e5 to your computer and use it in GitHub Desktop.
Save lukewakeford/3afb3537fd1346504078e805d5f659e5 to your computer and use it in GitHub Desktop.
Reduce Playground Example
import UIKit
struct product {
var price:Int?
var name:String
}
let array = [
product(price: 100, name: "First"),
product(price: nil, name: "Second"),
product(price: 300, name: "Thrid")
]
let numberSum = array.reduce(0, { x, y in
x + (y.price ?? 0)
})
print(numberSum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment