標籤: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函數