A brief talk on the swift function of curry (currying)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.