Skip to content

Instantly share code, notes, and snippets.

@motokiee
Created November 14, 2015 06:31
Show Gist options
  • Save motokiee/27ba6a6dee3aad92209b to your computer and use it in GitHub Desktop.
Save motokiee/27ba6a6dee3aad92209b to your computer and use it in GitHub Desktop.
勉強会の休憩中に部分適用とかいじった #CodePiece
typealias Closure = (Int,Int) -> Int
let addClosure = { (a:Int, b:Int) -> Int in
return a+b
}
let multipleClosure = { (a:Int, b:Int) -> Int in
return a*b
}
func applyClosure(a:Int)(_ b:Closure) -> Int {
return b(a,a)
}
func calc(closure:Closure) -> Int -> Int {
return { x in closure(x,x)}
}
applyClosure(10)(addClosure)
applyClosure(10)(multipleClosure)
let addFunc = calc(addClosure)
calc(addClosure)(10)
addFunc(10)
let multipleFunc = calc(multipleClosure)
calc(multipleClosure)(10)
multipleFunc(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment