Currying:实际意义(Currying: practical implications)
我对这个问题的理解来自Heilperin等人。 “混凝土抽象” 。 我得到的结论是对函数的求值的转换,该函数将几个参数用于评估函数序列,每个函数都有一个参数。 我清楚了两种方法之间的语义差异(我可以这样称呼它们吗?)但我确信我没有掌握这两种方法背后的实际意义。
请考虑在Ocaml中:
# let foo x y = x * y;; foo : int -> int -> int = <fun>
和
# let foo2 (x, y) = x * y;; foo2 : int * int -> int = <fun>
两个函数的结果相同。 但是,实际上,这两个功能有什么不同呢? 可读性? 计算效率? 我缺乏经验不能给这个问题足够的阅读。
My comprehension of the problem comes from Heilperin's et al. "Concrete Abstraction". I got that currying is the translation of the evaluation of a function that takes several arguments into evaluating a sequence of functions, each with a single argument. I have clear the semantic differences between the two approaches (can I call them this way?) but I am sure I did not grasp the practical implications behind the two approaches.
Please consider, in Ocaml:
# let foo x y = x * y;; foo : int -> int -> int = <fun>
and
# let foo2 (x, y) = x * y;; foo2 : int * int -> int = <fun>
The results will be the same for the two functions. But, practically, what does make the two functions different? Readability? Computational efficiency? My lack of experience fails to give to this problem an adequate reading.
原文:https://stackoverflow.com/questions/39835395
最满意答案
lapply(v, function(i){ print(i) s = some.complex.function.that.updates.s(s) return(s) })
结果将是为
v
每个值创建的对象列表。 即使它应该已经传递了v
的值,因为它是该函数执行的最后一个操作。lapply(v, function(i){ print(i) s = some.complex.function.that.updates.s(s) return(s) })
the result will be a list of object
s
created for each value ofv
. Even if it should have passed the value ofv
anyway cause it was the last operation performed by the function.
相关问答
更多-
泛型函数范围R(Scope in generic functions R)[2022-03-20]
定义do_usemethod的C源文件中的以下评论至少暗示了发生了什么。 特别参见第二个枚举项目的第二句。 基本上,它看起来像(由于第二点中的规则应用不严格),x的值并没有被复制,因为C代码检查它是否在形式中,看到它是,并且如果排除插入到方法评估环境中的变量列表。 /* usemethod - calling functions need to evaluate the object * (== 2nd argument). They also need to ensure that the * argum ... -
循环中Sass的变量范围(Variable scope in Sass for loops)[2024-01-27]
我不认为这是编译器中的错误,因为我认为这是@for循环的预期行为,但你是正确的,因为它没有记录 。 我会提交一份请求 , 要求澄清/添加到官方文档中。 I don't think this is a bug in the compiler as I believe this to be the intended behavior of the @for loop, but you're correct in that that it's not documented. I would submit a re ... -
for循环中的变量范围(Variable Scope in for-loops)[2022-01-10]
在您的代码中,您已经定义了i和j多个实例(每个实例占用自己的内存空间)。 至少这导致难以理解和不可维护的代码: int i = 0, int j = 0; //i & j defined here for(int i = 0; i < 10; i++){//and new i here static int j = 0;//new j defined here with new block scope. 关于范围:此代码片段没有意义,只是为了说明每次出现的i都是一个单独的变量,因 ... -
我对OP的追求与@ josilber相同的猜测。 这是一个非基本的R方式: library(data.table) setDT(data)[, c("x","y") := {ii = which.max(area) ; list(x[ii], y[ii])}, by = POSTAL] (就给出的例子而言,这只在第39行进行了一次改变。) My guess for what the OP's seeking is the same as @josilber's. Here's a non-base R w ...
-
全局变量和范围 - R(Global variable and scope - R)[2022-05-31]
基本上,由于您使用的是“< - ”赋值,因此该函数正在创建“全局”变量的副本,以便在函数范围内使用。 这可以通过在第二个函数g()中添加一个函数g()来实现,该函数在f()打印出来之前改变'global'的值,但这次使用“<< - ”赋值。 f()中的第一行为f(x)创建了'global'的本地范围副本,然后使用g(x)更新'global'的全局副本。 global <<- list() f <- function(x) { global[[x]] <- "blah" g(x) global ... -
R:局部/全局变量的范围(R: scope of local/global variables)[2021-03-31]
environment(getDetails) = parent.env(environment(getDetails))是一种删除全局范围的方法。 这对我有用。 唯一的问题是,必须为我编写的每个函数执行此操作。 environment(getDetails) = parent.env(environment(getDetails)) is a way to remove the global scope. This works for me. Only problem is, this has to be ... -
一种解决方案是使用eval()和parse() : p.val <- eval(parse(text=paste0("summary(",mod.name,")[",paste0(attr.name),"]"))) 所以功能是: mod.pvals <- NULL p.func <- function(attr.name) { for(i in c('dat1','dat2')) { mod.name <- paste0('fit.',i) p.val <- eval(parse(te ...
-
R中的循环和变量范围(Loops and variable scope in R)[2023-05-01]
lapply(v, function(i){ print(i) s = some.complex.function.that.updates.s(s) return(s) }) 结果将是为v每个值创建的对象列表。 即使它应该已经传递了v的值,因为它是该函数执行的最后一个操作。 lapply(v, function(i){ print(i) s = some.complex.function.that.updates.s(s) ... -
c ++函数中的变量作用域通过R使用sourceCpp调用(variable scope in c++ function called through R with sourceCpp)[2024-03-16]
你可以通过Rcpp将R环境传递给C ++就好了; 并且示例存在于文档,此处和其他位置。 然而,随便在那里随机扔一个C ++ extern会访问一个环境。 You can pass R environments down to C++ just fine via Rcpp; and examples exists in the docs, here, and in other places. Just randomly tossing a C++ extern in there, however, does ... -
在R中循环内的ifelse(ifelse within loop in R)[2022-05-10]
要测试NA ,您需要使用is.na函数。 看到: > 1 != NA # bad [1] NA > !is.na(1) # good [1] TRUE 所以最后,只需要替换: olddata[,paste("q08pa",spell,sep="")]!=NA 同 !is.na(olddata[,paste("q08pa",spell,sep="")]) 你应该没问题。 To test for NA, you need to use the is.na function. See that: ...