Skip to content

Instantly share code, notes, and snippets.

@stulevine
Created February 25, 2017 13:16
Show Gist options
  • Save stulevine/67c9063db95573a857bd8d3cac6ecc0c to your computer and use it in GitHub Desktop.
Save stulevine/67c9063db95573a857bd8d3cac6ecc0c to your computer and use it in GitHub Desktop.
Example of how to use .reduce in Swift 3
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
struct ChartItem {
var label: String = ""
var value: CGFloat = 0.0
}
let data = [ChartItem(label: "one", value: 350.0),
ChartItem(label: "two", value: 1310.0),
ChartItem(label: "two", value: 750.0),
ChartItem(label: "three", value: 890.0),
ChartItem(label: "two", value: 230.0)]
let maxBaseHeight = data.reduce(0.0, { (total, item) -> CGFloat in
max(total, item.value)
})
print("maxBaseHeight: \(maxBaseHeight)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment