#1。忽略 Curry 的第二个参数并硬编码换行符
试试这个,它将cat 的最后一个参数硬编码在一个匿名函数中。它实际上并没有在第一个参数之后使用 Curry 参数:
catnip <- Curry(function(...) cat(..., "\n") )
#2。通过柯里化匿名函数来制造函数
这是第二种解决方案,它使用匿名函数对cat 的参数进行重新排序,从而将cat 的最后一个参数集中起来。
catnip2 <- Curry(function(last.arg, ...) cat(..., last.arg), "\n")
# test
catnip2("hi", "there")
#3。通过对更基本的函数进行柯里化来制造所需的函数
也许这一切的真正意义在于看看我们如何获取基本组件并将它们库里化以获得我们想要的东西。因此我们可以定义一个通用的last.arg.fun,然后通过对它的咖喱来制造所需的功能:
last.arg.fun <- function(FUN, last.arg, ...) FUN(..., last.arg)
catnip3 <- Curry(last.arg.fun, cat, "\n")
# test
last.arg.fun(cat, "\n", "hi", "there")
# test
catnip3("hi", "there")
如果我们在某个时候需要last.arg.cat,我们可以分两步完成:
last.arg.cat <- Curry(last.arg.fun, cat)
catnip4 <- Curry(last.arg.cat, "\n")
# test
last.arg.cat("\n", "hi", "there")
# test
catnip4("hi", "there")
请注意,每个测试都应该产生一行,说 hi there 以换行符结尾。
编辑:更多解决方案。