Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please more advice, if you feel good please support a lot of praise. Thank you! Hopy;)
The following is a simple way to say that the function of the swift language is simply to divide the function that receives multiple parameters into several "nested" single-parameter functions.
So that no one can understand, we combine an example for everyone to simply say.
We need to define a function A, which returns a function B, and the function B creates a big spider. Why return to the big spider indirectly, because this cat is afraid of the big spider, so dare not directly return to the big spider;)
The first is the spider's Class:
class Spider:CustomStringConvertible { let name:String let leg:Int var description:String{ return"蜘蛛(\(name) leg:\(leg))" } init(name:String,leg:Int){ self.= name self.= leg }}
Because we don't want to create a big spider directly, we need a function that returns a function:
func createSpawnSpiderFunc(name:String)->()->Spider{ func spwanSpider()->Spider{ return Spider(nameleg8) } return spwanSpider}let x = createSpawnSpiderFunc("BigSpider")let//leg:8)
Looks good, but the above method to limit the spider's legs to 8, which is not very good, so we make some adjustments:
func createSpawnSpiderFunc2(name:String)->(Int)->Spider{ func spwanSpider(leg:Int)->Spider{ return Spider(nameleg: leg) } return spwanSpider}let y = createSpawnSpiderFunc2("XLegSpider")let spiderXLeg = y(16//leg:16)
We can simplify the function appropriately:
func createSpawnSpiderFunc3(name:String)->(Int)->Spider{ returnin return Spider(nameleg: leg) }}
You would say that this function is not pretty, because behind the two-and-a-tail, indeed! Not only is it not elegant and it's easy to get the novice around, so we turn the function: Curry smecta:
func createSpawnSpiderFuncCurry(name:String)(leg:Int)->Spider{ return Spider(nameleg: leg)}
We call this:
let 蜘蛛 = createSpawnSpiderFuncCurry("大蜘蛛"121//蜘蛛(大蜘蛛 leg:121)
Note that the above function looks like the return type is spider, but it is not! It still returns a function! Prove to you:
let 创建超级大蜘蛛 = createSpawnSpiderFuncCurry("超级大蜘蛛")let100000//蜘蛛(超级大蜘蛛 leg:100000)
A brief talk on the swift function of curry (currying)