scalex(α) = ((x,y),) -> (α*x, y)
Ah, that ,
in the outer parens denotes that you want to construct a tuple:
julia> ((1,2),)
((1, 2),)
julia> typeof(ans)
Tuple{Tuple{Int64, Int64}}
julia> ((1,2))
(1, 2)
julia> typeof(ans)
Tuple{Int64, Int64}
And in the context of this function definition seems to denote that you mean to pull a tuple of (x,y)
_out_ of the argument to scalex
. I.e.:
julia> scalex(α) = ((x,y),) -> (α*x, y)
scalex (generic function with 1 method)
julia> scalex(5)([1,2])
(5, 2)
julia> scalex(α) = (x,y) -> (α*x, y)
scalex (generic function with 1 method)
julia> scalex(5)(1,2)
(5, 2)
julia> scalex(5)([1,2])
ERROR: MethodError: no method matching (::var"#5#6"{Int64})(::Vector{Int64})
Closest candidates are:
(::var"#5#6")(::Any, ::Any) at REPL[16]:1
Stacktrace:
[1] top-level scope
@ REPL[18]:1
i can see it happening, but not why.
I was just tinkering with tuple arguments and the splat operator, and was going really well until I saw that comma:
f₈(x,y,z) = 5sin(xy) + 2y/4z
f₈(v)=f₈(v...)
and
f₉((x,y,z)) = 5sin(xy) + 2y/4z
f₉(x,y,z)=f₉((x,y,z))
w = (1,2,3)
f₉(w...),f₉(w)
Yeah, so
((x,y),) -> (α*x, y)
is just fancy lambda syntax equivalent to
function (tpl)
x,y = tpl
return α*x, y
end
and the whole thing is equivalent to
function scalex(α)
function (tpl)
x,y = tpl
return α*x, y
end
end
OK thanks... I can see the tuple literal with the lonely ,
being equivalent to the function parameters declaration.
A tuple of one element takes a trailing comma. Otherwise the parentheses would be interpreted as grouping operations together.
Last updated: Nov 06 2024 at 04:40 UTC