Created
February 19, 2015 16:26
-
-
Save gfontenot/a50fccfcd5dd40581543 to your computer and use it in GitHub Desktop.
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
// 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