How to curry a … argument by position in R?

谁都会走 提交于 2020-01-11 04:54:08

问题


This may fall under "you can't, and there's no reason to anyway," but I'm curious if it's possible. At very least, maybe it will be a fun R puzzle.

I was pondering currying cat to always append \n. However, cat is written so that it pastes together as many arguments as it is given (via ...).

Surprisingly, this works:

> library(functional)
> catnip <- Curry( cat, "\n" )
> catnip("hi")

 hi

However, the \n winds up before the user's text. Is there any way to curry the function such that you specify the curried argument always ends the ... arguments?


回答1:


Looks like Curry() pretty effectively hardwires the two argument lists in the opposite order from what you'd like. It's a simple enough function though, that you can just construct its mirror image, and use it instead.

Curry2 <- function(FUN, ...) {
    .orig = list(...)
    function(...) do.call(FUN, c(list(...), .orig))
}

catnip <- Curry2( cat, "\n" )
catnip("hi")



回答2:


#1. Ignore the second argument of Curry and hard code the newline

Try this which curries the last argument of cat by hard coding it in an anonymous function. It does not actually make use of Curry arguments after the first:

catnip <- Curry(function(...) cat(..., "\n") )

#2. Manufacture function by currying an anonymous function

Here is a second solution which curries the last argument of cat by using an anonymous function which reorders cat's arguments.

catnip2 <- Curry(function(last.arg, ...) cat(..., last.arg), "\n")

# test
catnip2("hi", "there")

#3. Manufacture desired function by currying an even more basic function

Perhaps the real point of all this is to see how we can take basic components and Curry them to get what we want. Thus we could define a general last.arg.fun and then manufacture the desired function by a curry of it:

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

We could do it in two steps if we needed last.arg.cat at some point:

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

Note that each of the tests should produce a line saying hi there ended in a newline.

EDIT: more solutions.



来源:https://stackoverflow.com/questions/11232444/how-to-curry-a-argument-by-position-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!