[英]Can Julia pass an argument to an inner anonymous function?
I'm trying to pass an argument to an anonymous function in map()
in a particular way (see code examples).我正在尝试以特定方式将参数传递给
map()
中的匿名函数(参见代码示例)。
The following code in Julia... Julia中的以下代码...
function f(x,y):map((z)->z+y,x) end
print(f([1,2,3],1))
returns:返回:
MethodError: objects of type Symbol are not callable
Stacktrace:
[1] f(x::Vector{Int64}, y::Int64)
@ Main .\REPL[1]:1
[2] top-level scope
@ REPL[5]:1
Same code translated to Python...相同的代码翻译成 Python...
def f(x,y):
return map(lambda z:z+y,x)
print(list(f([1,2,3],1)))
works as expected: [2, 3, 4]
.按预期工作:
[2, 3, 4]
。
Why is the same block of code misbehaving in Julia in contrast to Python and what is the workaround?为什么与 Python 相比,Julia 中的相同代码块行为异常?解决方法是什么?
It's just a syntax issue: Julia function declarations don't use a colon before the function body.这只是一个语法问题:Julia 函数声明在函数体之前不使用冒号。
julia> function f(x,y) map((z)->z+y,x) end
f (generic function with 2 methods)
julia> print(f([1,2,3],1))
[2, 3, 4]
Or more readably,或者更易读,
julia> function f(x, y)
map(z -> z .+ y, x)
end
f (generic function with 2 methods)
julia> print(f([1,2,3],1))
[2, 3, 4]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.