Would it be possible in Javascript or Python to implement something like “non standard evaluation...
在 JavaScript 或 Python 中实现类似于“非标准评估(non-standard evaluation)”的功能是否可能……
题意:在 JavaScript 或 Python 中实现类似于“非标准评估(non-standard evaluation)”的功能是否可能……
问题背景:
In R you can write functions that allow arguments to be unquoted attributes of a pre-defined object. For example, the interface to the DataFrame object allows the following:
在 R 语言中,你可以编写允许参数作为预定义对象未引用属性的函数。例如,DataFrame 对象的接口允许以下操作:
# df has columns "A" and "B"
df = mutate(df, C=A*B)
Now df
has a new column "C" that is the product of columns "A" and "B".
现在,df
有了一个新列 "C",它是列 "A" 和 "B" 的乘积。
There is also the "formula" type which is unquoted:
还有一种未引用的“公式(formula)”类型:
lm(data=df, A~B)
This "Non-Standard Evaluation." 这就是“非标准评估(Non-Standard Evaluation)”。
Is it fundamentally possible to do something similar in Javascript or Python.
在 JavaScript 或 Python 中是否从根本上可以实现类似的功能。
问题解决:
No, it is not possible to have NSE in Python and JavaScript.
不,在Python和JavaScript中不可能有NSE
Why is that? That is because in Python and JS, arguments are evaluated BEFORE they are passed to the function, while it is not the case in R.
为什么是这样呢?那是因为在Python和JavaScript中,参数在传递给函数之前就已经被求值了,而在R语言中则不是这样。
Let's consider the two similar codes in R and Python:
让我们来看一下R语言和Python中的两段相似代码:
main.R
enthusiastic_print <- function(x) {
print("Welcome!")
print(x)
}
enthusiastic_print("a" + 3)
main.py
def enthusiastic_print(x):
print("Welcome!")
print(x)
enthusiastic_print("a" + 3)
They will both procude an error. But now, let's have a look at when the error occurs:
它们都会产生一个错误。但是现在,让我们来看一下错误发生的时间:
.R
[1] "Welcome!"
Error in "a" + 3 : non-numeric argument to binary operator
exit status 1
.py
Traceback (most recent call last):
File "main.py", line 6, in <module>
enthusiastic_print("a" + 3)
TypeError: can only concatenate str (not "int") to str
You can see that Python evaluates what is passed to the function BEFORE the call. While R, keeps the complete expression passed as argument and evaluates it only when it is necessary.
你可以看到,Python在函数调用之前就已经对传递给函数的参数进行了求值。而R语言则保留了作为参数传递的完整表达式,并且只在必要时才对其进行求值。
You can even write this code in R that will produce NO error:
你甚至可以在R语言中编写这样的代码,而不会产生任何错误:
foo <- function(x) {
print("Welcome in foo!")
print("I never mess with args...")
}
foo("a" + 3)
You can also capture what was passed as argument and not evaluate it:
你还可以捕获作为参数传递的内容而不进行求值:
verbatim_arg <- function(x) {
print(substitute(x))
}
verbatim_arg("a" + 3)
which produces: 其输出为:
"a" + 3
Finally, that's the functions substitute()
combined with eval()
which permit to use formulas and all the other NSE stuff.
最后,这就是substitute()
函数与eval()
函数的结合使用,它们允许使用公式和所有其他非标准求值(NSE)的东西。
Supplementary: 补充内容
You can do the same test in node.js
你可以在Node.js中进行相同的测试
function enthusiastic_print(x) {
console.log("Welcome!")
console.log(x)
}
enthusiastic_print("a".notAMethod())

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)