Skip to content

Instantly share code, notes, and snippets.

@ikesyo
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikesyo/afb5aa47e3e605c41350 to your computer and use it in GitHub Desktop.
Save ikesyo/afb5aa47e3e605c41350 to your computer and use it in GitHub Desktop.
Create curried initializer function
func curry<A, B, C, Result>(f: (A, B, C) -> Result) -> A -> B -> C -> Result {
return { a in { b in { c in f(a, b, c) } } }
}
struct Hoge {
let a: String
let b: Int
let c: Bool?
}
extension Hoge: Argo.Decodable {
static func decode(j: JSON) -> Decoded<Hoge> {
let create = { Hoge($0) } // create: ((a: String, b: Int, c: Bool?)) -> Hoge
let curried = curry(create) // curried: String -> Int -> Bool? -> Hoge
return curried
<^> j <| "a"
<*> j <| "b"
<*> j <|? "c"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment