码迷,mamicode.com
首页 > 编程语言 > 详细

如何将Swift中的多参函数转化成Curring函数

时间:2015-04-30 10:37:07      阅读:133      评论:0      收藏:0      [点我收藏+]

标签: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函数

标签:ios   swift   currying function   curried   curring   

原文地址:http://blog.csdn.net/tounaobun/article/details/45391523

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!