# Collect doesn't terminate

**URL:** <https://discourse.julialang.org/t/collect-doesnt-terminate/43938>\
**Category:** New to Julia\
**Created:** [July 30, 2020, 1:50am UTC](https://discourse.julialang.org/t/collect-doesnt-terminate/43938 "2020-07-30T01:50:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sobhan](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@sobhan](https://discourse.julialang.org/u/sobhan)\
**Post date:** [July 30, 2020, 1:50am UTC](https://discourse.julialang.org/t/collect-doesnt-terminate/43938/1 "2020-07-30T01:50:18Z")

</div>

hi i’m trying to normalize(in the db sense) a dictionary{Int8, Vecctor{Float}} but the code never ends

```julia
d = Dict()
for i in 1:100
    c = rand(Int8)
    n = rand()
    a = get!(d, c, [])
    push!(a, n)
end
map(collect, zip(Iterators.flatten([[(k, i) for i in v] for (k, v) in d])...))

```

i want to convert something like `1 => [1,2,3], 2=>[3]` to something like `[(1, 1), (1,2), (1,3), (2,3)]` and then `[1,1,1,2], [1,2,3,3]`

---

<div class="post-metadata">

**Author:** ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)\
**Post date:** [July 30, 2020, 2:25am UTC](https://discourse.julialang.org/t/collect-doesnt-terminate/43938/2 "2020-07-30T02:25:44Z")

</div>

> [@sobhan](#):
>
> `zip(Iterators.flatten([[(k, i) for i in v] for (k, v) in d])...)`

You are passing 100 collections to the `zip` function, so this is the bottleneck of the code.

You can use this function to accomplish what you want:

```julia
function fill_key_val(dict)
	n = sum(length, values(dict))
	key_vector = Array{keytype(dict)}(undef, n)
	val_vector = Array{eltype(valtype(dict))}(undef, n)
	ind = 1
	@inbounds for (k, v) = dict
		for i = v
			key_vector[ind] = k
			val_vector[ind] = i
			ind += 1
		end
	end
	key_vector, val_vector
end

```

Note that you can specify concrete types for the keys and values of the dictionary by `Dict{Int8, Vector{Float64}}()`, so the compiler can generate specialized code.

---

<div class="post-metadata">

**Author:** ![sobhan](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@sobhan](https://discourse.julialang.org/u/sobhan)\
**Post date:** [July 30, 2020, 4:26pm UTC](https://discourse.julialang.org/t/collect-doesnt-terminate/43938/3 "2020-07-30T16:26:11Z")

</div>

it doesn’t terminate, as in i let it run for 8 hours and didn’t finish, zip isn’t the bottleneck since

```julia
collect(zip(Iterators.flatten([[(k, i) for i in v] for (k, v) in d])...))

```

runs pretty fast

---

<div class="post-metadata">

**Author:** ![sobhan](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@sobhan](https://discourse.julialang.org/u/sobhan)\
**Post date:** [July 30, 2020, 4:40pm UTC](https://discourse.julialang.org/t/collect-doesnt-terminate/43938/4 "2020-07-30T16:40:07Z")

</div>

```julia
map(collect, collect(zip(Iterators.flatten([[(k, i) for i in v] for (k, v) in d])...)))

```

runs fast too

---

<div class="post-metadata">

**Author:** ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)\
**Post date:** [July 30, 2020, 11:20pm UTC](https://discourse.julialang.org/t/collect-doesnt-terminate/43938/5 "2020-07-30T23:20:14Z")

</div>

> [@sobhan](#):
>
> zip isn’t the bottleneck

Actually, what I meant to say is that the bottleneck is the argument of the `zip` function itself. You are creating a `Tuple` with 100 elements, so the compiler struggles with type inference. This problem occurs specifically when you try to iterate the following `Generator`:

```julia
iter = (collect(k) for k in zip(Iterators.flatten([[(k, i) for i in v] for (k, v) in d])...))
iterate(iter)

```

---

<div class="post-metadata">

**Author:** ![sobhan](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@sobhan](https://discourse.julialang.org/u/sobhan)\
**Post date:** [July 31, 2020, 3:16am UTC](https://discourse.julialang.org/t/collect-doesnt-terminate/43938/6 "2020-07-31T03:16:50Z")

</div>

it struggles even with 5 arguments, why does adding a collect to zip solve the problem?

---

<div class="post-metadata">

**Author:** ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)\
**Post date:** [July 31, 2020, 4:20pm UTC](https://discourse.julialang.org/t/collect-doesnt-terminate/43938/7 "2020-07-31T16:20:00Z")

</div>

> [@sobhan](#):
>
> it struggles even with 5 arguments

Are you sure? If you replace `for i in 1:100` in your original code by `for i in 1:5`, it should run in acceptable time. Of course it is not optimized, but the code should run.

> [@sobhan](#):
>
> why does adding a collect to zip solve the problem?

`map` dispatches on a specialized method for the case of an `AbstractArray`, which is the result of `collect`.

---

<div class="post-metadata">

**Author:** ![sobhan](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@sobhan](https://discourse.julialang.org/u/sobhan)\
**Post date:** [July 31, 2020, 5:11pm UTC](https://discourse.julialang.org/t/collect-doesnt-terminate/43938/8 "2020-07-31T17:11:14Z")

</div>

> map dispatches on a specialized method for the case of an AbstractArray, which is the result of collect.  
> makes sense

you’re right it runs fine with less than 30 items.  
thanks
