Skip to content

Instantly share code, notes, and snippets.

@jmnavarro
Created September 30, 2014 15:18
Show Gist options
  • Save jmnavarro/daf887bf288038d52e79 to your computer and use it in GitHub Desktop.
Save jmnavarro/daf887bf288038d52e79 to your computer and use it in GitHub Desktop.
Real life map+reduce solution
//
// 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