Created
September 30, 2014 15:18
-
-
Save jmnavarro/daf887bf288038d52e79 to your computer and use it in GitHub Desktop.
Real life map+reduce solution
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
// | |
// returns the list of different extensions from a bunch of files | |
// | |
func differentExtensions(fileNames: [String]) { | |
// Step 1. Map: process file names list to convert to extensions list | |
let allExtensions = fileNames.map { $0.pathExtension.lowercaseString } | |
// Step 2. Reduce: iterate removing duplicates | |
return allExtensions.reduce([String]()) { (acum, value) -> [String] in | |
// use filter here to find whether or not it already exists | |
if (acum.filter() { return $0 == value }).isEmpty { | |
return acum + [value] | |
} | |
return acum | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment