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

Swift 柯里化(currying)和反柯里化(uncurrying),第1张

概述// 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: I

// 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)")

总结

以上是内存溢出为你收集整理的Swift 柯里化(currying)和反柯里化(uncurrying)全部内容,希望文章能够帮你解决Swift 柯里化(currying)和反柯里化(uncurrying)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1081580.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存