Zip and namedtuple

Hi all,

I wonder why zip of named tuples does not take into the account names of variables. Is this a desired behavior? Is there an alternative I am not aware of, which does?

julia> x, y  = (a = 1, b = 2), (b = 4, a = 3);
julia> map(i -> i[1] + i[2], zip(x,y))
2-element Array{Int64,1}:
 5
 5

but

julia> x, y  = (a = 1, b = 2), (a = 3, b = 4);

julia> map(i -> i[1] + i[2], zip(x,y))
2-element Array{Int64,1}:
 4
 6

i.e order matters but not names.
Thanks for explanation in advance.
Tomas

NamedTuples are tuples and iterate by value.

julia> x = (a = 1, b = 2)
(a = 1, b = 2)

julia> collect(x)
2-element Array{Int64,1}:
 1
 2

You can use pairs to iterate by Symbol => value:

julia> collect(pairs(x))
2-element Array{Pair{Symbol,Int64},1}:
 :a => 1
 :b => 2
2 Likes

Thanks Kristoffer for answer.

That means you suggest this?

julia> collect(zip(pairs(x), pairs(y)))
2-element Array{Tuple{Pair{Symbol,Int64},Pair{Symbol,Int64}},1}:
 (:a => 1, :a => 3)
 (:b => 2, :b => 4)