Skip to content

Instantly share code, notes, and snippets.

@gfontenot
Created July 22, 2015 15:40
Show Gist options
  • Save gfontenot/c0ea07d48c9225944c6a to your computer and use it in GitHub Desktop.
Save gfontenot/c0ea07d48c9225944c6a to your computer and use it in GitHub Desktop.
Example of using partial application as a way to solve side loaded JSON with immutable value objects
// Returning a function here, instead of a fully realized object
extension Company: Decodable {
static func decode(j: JSON) -> Decoded<[People] -> Company> {
return create
<^> j <| "id"
<*> j <| ["attributes", "name"]
}
}
func parse(j: JSON) -> Company? {
// "included" holds the related people for the company
let company = (j <| "data" >>- Company.decode) <*> j <|| "included"
return company.value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment