# How to lazily \`collect(zip(fn(a), b))\` for some function \`fn\`?

**URL:** <https://discourse.julialang.org/t/how-to-lazily-collect-zip-fn-a-b-for-some-function-fn/9244>\
**Category:** Performance\
**Tags:** lazy-evaluation\
**Created:** [February 22, 2018, 4:34am UTC](https://discourse.julialang.org/t/how-to-lazily-collect-zip-fn-a-b-for-some-function-fn/9244 "2018-02-22T04:34:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [February 22, 2018, 4:34am UTC](https://discourse.julialang.org/t/how-to-lazily-collect-zip-fn-a-b-for-some-function-fn/9244/1 "2018-02-22T04:34:39Z")

</div>

I want to lazily `collect` a `zip` iterator. See MWE below where I applied `pointer.` to `a` before using a `zip` and `collect`, but `pointer.(a)` allocated a new vector, so I don’t want that. Is there a way to lazily do `pointer.(a)` so I dont’ have to create a new vector first but still end up with `ptra` being a vector of tuples?

```julia
a = rand([randstring(8) for i = 1_000_000], 100_000_000)
b = rand(100_000_000)
c = similar(b)
ptra = collect(zip(pointer.(a), b, c))

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 22, 2018, 4:53am UTC](https://discourse.julialang.org/t/how-to-lazily-collect-zip-fn-a-b-for-some-function-fn/9244/2 "2018-02-22T04:53:23Z")

</div>

```julia
julia> a = [1,2,3]; b = copy(a); c = copy(a);

julia> collect(zip(log.(a), b, c))
3-element Array{Tuple{Float64,Int64,Int64},1}:
 (0.0, 1, 1)
 (0.693147, 2, 2)
 (1.09861, 3, 3)

julia> map((a,b,c)->(log(a),b,c), a, b, c)
3-element Array{Tuple{Float64,Int64,Int64},1}:
 (0.0, 1, 1)
 (0.693147, 2, 2)
 (1.09861, 3, 3)

julia> collect((log(x),y,z) for (x,y,z) in zip(a,b,c))
3-element Array{Tuple{Float64,Int64,Int64},1}:
 (0.0, 1, 1)
 (0.693147, 2, 2)
 (1.09861, 3, 3)

julia> it = ((log(x),y,z) for (x,y,z) in zip(a,b,c))
Base.Generator{Base.Iterators.Zip{Array{Int64,1},Base.Iterators.Zip2{Array{Int64,1},Array{Int64,1}}},##40#41}(#40, Base.Iterators.Zip{Array{Int64,1},Base.Iterators.Zip2{Array{Int64,1},Array{Int64,1}}}([1, 2, 3], Base.Iterators.Zip2{Array{Int64,1},Array{Int64,1}}([1, 2, 3], [1, 2, 3])))

julia> collect(it)
3-element Array{Tuple{Float64,Int64,Int64},1}:
 (0.0, 1, 1)
 (0.693147, 2, 2)
 (1.09861, 3, 3)

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [February 22, 2018, 10:51am UTC](https://discourse.julialang.org/t/how-to-lazily-collect-zip-fn-a-b-for-some-function-fn/9244/3 "2018-02-22T10:51:36Z")

</div>

I feel dumb now. Maybe others will have the same question…
