Skip to content

Instantly share code, notes, and snippets.

@gfontenot
Created February 19, 2015 16:26
Show Gist options
  • Save gfontenot/a50fccfcd5dd40581543 to your computer and use it in GitHub Desktop.
Save gfontenot/a50fccfcd5dd40581543 to your computer and use it in GitHub Desktop.
// Original implementation with multiple returns:
class func fromId(id: String) -> Office? {
let officesData = JSONData.load("offices") as? [[String: String]] ?? []
let officeData = officesData.filter { $0["id"] == id }.first
if let office = officeData {
return decode(JSONValue.parse(office))
}
return .None
}
// Refactored using functional concepts, removing the need for multiple returns
class func fromId(id: String) -> Office? {
let officesData = JSONData.load("offices") as? [[String: String]] ?? []
let officeData = officesData.filter { $0["id"] == id }.first
return (JSONValue.parse <^> officeData) >>- decode
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment