Created
November 14, 2015 06:31
-
-
Save motokiee/27ba6a6dee3aad92209b to your computer and use it in GitHub Desktop.
勉強会の休憩中に部分適用とかいじった #CodePiece
This file contains hidden or 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
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