微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift 柯里化(currying)和反柯里化(uncurrying)

// Demo of currying


func addTwoNums(a: Int)(num: Int) -> Int {

return a + num

}


let addToFour = addTwoNums(4)

let result = addToFour(num: 6)

print("result: \(result)")


func greaterThan(comparor: Int)(input: Int) -> Bool {

return input > comparor

}


let greaterThan10 = greaterThan(10)

let b1 = greaterThan10(input: 10)

let b2 = greaterThan10(input: 4)

print("b1: \(b1)")

print("b2: \(b2)")


// Demo of uncurrying


let foo = {(a: Int) -> (Int) -> Int in

return {(b: Int) -> Int in

return a * a + b * b

}

}


let interesting = foo(3)(4)

print("interesting: \(interesting)")


let interestingAgain = (foo(3))(4)

print("interestingAgain: \(interestingAgain)")

原文地址:https://www.jb51.cc/swift/325128.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐