我正在尝试像Lodash http://lodash.com/docs#curry
那样制作咖喱功能不幸的是我收到两个编译错误。
func curry(fun: (Any...)) -> ((Any...) -> (Any)) {
let count = fun.0.count
var resultArgs: Any[] = []
func generator(newArgs: Any...) -> ((Any...) -> (Any))? {
for (index, elem) in enumerate(newArgs) {
if resultArgs.count >= count {
break
} else {
resultArgs += elem
}
}
if resultArgs.count >= count {
// fun(resultArgs)
// Commenting fun now cause that throws a compile error saying array cannot be passed in Variadic parameter function
return nil
} else {
return generator
}
}
return generator
}
这里有两个问题:
fun
performSelector
来实现
醇>
我如何解决这两个问题?
我在生成器函数中遇到以下错误,这是编译时的分段错误
1. While emitting IR SIL function @_TTRXFt_oGSaP__oBolGSaP__dSioXFo_oGSaP___oGSqFtGSaP___P____oGSqFtGSaP___P___XFo_oGSaP___oGSqFtGSaP___P___ for 'generator'
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254
答案 0 :(得分:1)
这是我接受多个参数时最接近的。根据为curry2
...
func curry<T, U>(fun: (T) -> U) -> (T)->U {
return fun
}
func curry2(fun: (Any...) -> Any) -> (Any...)->Any {
return fun
}
func to_curry(a: Int, b: Int) -> Int { return a + b }
func test() {
curry(to_curry)(3, 5) // returns 8
curry2(to_curry)(3, 5)
// tuple types '(Any...)' and '(Int, Int)' have a
// different number of elements (1 vs. 2)
}
除了做这样的事情之外,我找不到计算元组的方法:
let tuple = (1, 2)
let count = sizeofValue(tuple)/sizeof(Int)