этот код будет работать только в том случае, если f(a,b)
создает одинаковый тип для каждого элемента кортежа и только два элемента (но его можно изменить в соответствии с вашей операцией):
function mytuplemaker(f,a,b)
if (len=length(a)) == length(b) #asigns len and compares at the same time, neat trick
x1 = f(a[1],b[1]) #wastes a calculation to obtain the resulting type
T = typeof(x1)
return NTuple{len,T}(f(a[i],b[i]) for i = 1:len)
else
return nothing
end
end
некоторые тесты:
julia> @btime map(Tuple(zip($a, $b))) do (i, j)
do_something(i,j)
end
377.340 ns (13 allocations: 528 bytes)
((1, "a"), (2, "b"), (3, "c"), (4, "d"))
@btime map(zip($a, $b)) do (i, j)
do_something(i,j)
end |> Tuple
377.223 ns (10 allocations: 480 bytes)
((1, "a"), (2, "b"), (3, "c"), (4, "d"))
@btime mytuplemaker(do_something,$a,$b)
38.655 ns (5 allocations: 176 bytes)
((1, "a"), (2, "b"), (3, "c"), (4, "d"))
его 5 распределений, по одному на элемент +1 для потраченного впустую