Skip to content

Instantly share code, notes, and snippets.

@dani-mp
Created March 5, 2017 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dani-mp/d9d23b5a34778ceefcdf1a69e6f1d4f4 to your computer and use it in GitHub Desktop.
Save dani-mp/d9d23b5a34778ceefcdf1a69e6f1d4f4 to your computer and use it in GitHub Desktop.
Reduce example
func arrayToTuple(array: [Int]) -> (Int, Int, Int) {
return array.reduce((0, 0, 0)) { temp, item in
switch item {
case 0..<10: return (temp.0 + 1, temp.1, temp.2)
case 10..<100: return (temp.0, temp.1 + 1, temp.2)
default:
return item > 100 ? (temp.0, temp.1, temp.2 + 1) : temp
}
}
}
let a = [42, 7, 23, 146, 19]
let ar = arrayToTuple(array: a)
ar == (1, 3, 1)
let b = [1, 2, -34, 100, 33]
let br = arrayToTuple(array: b)
br == (2, 1, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment