Why I cannot do this
map((i,j,v) -> v/(i*j), zip([1,2],[10,20],[100,200]))
But this is just fine
[v/(i*j) for (i,j,v) in zip([1,2],[10,20],[100,200])]
And instead I have to write
map(x -> x[3]/(x[1]*x[2]), zip([1,2],[10,20],[100,200]))
which I consider unnecessary as they should be syntactic sugar from the same jar.
map(((i,j,v),) -> v/(i*j), zip([1,2],[10,20],[100,200]))
2 Likes
You can use map, but the syntax is slightly different:
-
(i,j,v) -> v/(i*j)
is an anonymous function with three arguments, but zip(x, y, z)
creates three tuples which map then passes to the function.
To make the example work, either
- pass an anonymous function with a single argument destructuring into a three tuple:
map(((i,j,v),) -> v/(i*j), zip([1,2],[10,20],[100,200]))
- or just pass all vectors as individual arguments to map (it will then call the function on three arguments instead of zipping them into three tuples):
map((i,j,v) -> v/(i*j), [1,2],[10,20],[100,200])
8 Likes
Thank you both for clarifying my misunderstanding that a function that accepts a 3-tuple may also take three inputs and my ignorance that map
accepts multiple collections.
I wonder whether the destructuring might be more visible if a keyword as
were used, for this example, (t as (i,j,v)) -> v/(i*j)
to denote something like
t -> begin
i, j, v = t
v/(i*j)
end
similar to the grammar used in SML/NJ and Haskell to bind to a subpattern identifier.
The closest shape I can think of
map(t-> ((i,j,v)=t; v/(i*j)), zip([1,2],[10,20],[100,200]))