如何將Swift中的多參函數轉化成Curring函數

來源:互聯網
上載者:User

標籤:ios   swift   currying function   curried   curring   

假設我們有一個含有多個參數的函數,用於構建一個人的基本資料:

func buildInfoWithName(name: String,#age: Int,#gender: String,#address: String,#phone: String) -> String {        return "My name is " + name        +  ",I'm a " + gender        +  ",I live in " + address        +  ",my phone number is " + phone}let mike = buildInfoWithName("Mike",age: 20,gender: "boy",address: "Tokyo Japan",phone: "12345678")


如果我們不想一次性提供所有的參數,可以把它改造成Curring函數:

func buildInfoWithName(name: String)(age: Int)(gender: String)(address: String)(phone: String) -> String {        return "My name is " + name    +  ",I'm a " + gender    +  ",I live in " + address    +  ",my phone number is " + phone}let benson = buildInfoWithName("Benson")(age: 24)(gender: "boy")(address: "Chengdu Sichuan")(phone: "87654321")


我們把原始的函數已經分解成了接受單一參數的函數序列。


參考連結: http://justtesting.org/post/94325843216/what-is-currying-in-swift

如何將Swift中的多參函數轉化成Curring函數

相關文章

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.