Created
March 5, 2017 21:48
-
-
Save dani-mp/d9d23b5a34778ceefcdf1a69e6f1d4f4 to your computer and use it in GitHub Desktop.
Reduce example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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