// 柯里化
// http://www.jianshu.com/p/6eaacadafa1a Swift 2.0 柯里化方法 (废弃)
// http://www.swifthumb.com/thread-16079-1-1.html Swift 3.0 柯里化常用方法推荐
// http://www.ruanyifeng.com/blog/2012/04/functional_programming.html 函数式编程
/*
Curried function declaration syntax func foo(x: Int)(y: Int) is of limited usefulness and creates a lot of language and implementation complexity. We should remove it.
(函数的 currying 特性的使用场景并不大,但他会增加很多语言的复杂性,所以需要删除它)
*/
class Currying
{
// uncurried:普通函数
// 接收多个参数的函数(与类相关的函数,统称为方法,但是这里就直接说函数了,方便理解)
func add(a: Int, b: Int, c: Int) -> Int{
print("\(a) + \(b) + \(c)")
return a + b + c
}
// curried:柯里化函数 --> 本质函数式编程思想
func addCur(_ a: Int) -> (Int) -> (Int) -> Int{
return {
b in
return {
c in
a + b + c
}
}
}
}
let curry = Currying()
var number = Currying.addCur(curry)(12)(23)(12)
print(number)
// NO.2
let datePrint:(Int)->(Int)->(String)->Void =
{
month in
print("\(month)月")
return{
day in
print("\(day)日")
return{
action in
print("\(action)")
}
}
}
let actionPrint = datePrint(2016)(11)
actionPrint("写详细")