# Any shorter form of \`\`zip(1:2, cycle(3))''?

**URL:** <https://discourse.julialang.org/t/any-shorter-form-of-zip-1-2-cycle-3/3309>\
**Category:** General Usage\
**Created:** [April 21, 2017, 1:15am UTC](https://discourse.julialang.org/t/any-shorter-form-of-zip-1-2-cycle-3/3309 "2017-04-21T01:15:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)\
**Post date:** [April 21, 2017, 1:15am UTC](https://discourse.julialang.org/t/any-shorter-form-of-zip-1-2-cycle-3/3309/1 "2017-04-21T01:15:25Z")

</div>

Normally `zip` will stop if any component reaches end. e.g.,

```julia
julia> for (a,b) in zip(1:2,3)
       println("$a,$b")
       end
1,3

```

If I want it to align with the longest component (the shorter components just repeat), then have to write as

```julia
julia> for (a,b) in zip(1:2,cycle(3))
       println("$a,$b")
       end
1,3
2,3

```

My question is, is there any dedicated function for doing this latter task?

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [April 21, 2017, 2:00am UTC](https://discourse.julialang.org/t/any-shorter-form-of-zip-1-2-cycle-3/3309/2 "2017-04-21T02:00:26Z")

</div>

You can always define your own, but `zip(x, [Iterators.]cycle(y))` is pretty simple already.

---

<div class="post-metadata">

**Author:** ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)\
**Post date:** [April 21, 2017, 3:20am UTC](https://discourse.julialang.org/t/any-shorter-form-of-zip-1-2-cycle-3/3309/3 "2017-04-21T03:20:53Z")

</div>

Okay, good to know that. It turns out to be a one-line thing 🙂

```julia
julia> zip_align_first(a,b...) = zip(a, cycle.([b...])...)
zip_align_first (generic function with 1 method)

julia> collect(zip_align_first(1:6, 3, 4, [5,6], [7,8,9]))
6-element Array{Tuple{Int64,Int64,Int64,Int64,Int64},1}:
 (1,3,4,5,7)
 (2,3,4,6,8)
 (3,3,4,5,9)
 (4,3,4,6,7)
 (5,3,4,5,8)
 (6,3,4,6,9)

```

---

<div class="post-metadata">

**Author:** ![shorty66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shorty66/32/3254_2.png) [@shorty66](https://discourse.julialang.org/u/shorty66)\
**Post date:** [April 21, 2017, 11:42am UTC](https://discourse.julialang.org/t/any-shorter-form-of-zip-1-2-cycle-3/3309/4 "2017-04-21T11:42:51Z")

</div>

You could also sort the arguments before zipping. That way, you do not need to pass the longest argument first:

```julia
julia> function zip_align(args...)
           sorted = sort([args...],by=x->-length(x))
           zip(sorted[1],cycle.(sorted[2:end])...)
       end
zip_align (generic function with 1 method)

julia> collect(zip_align([1,2,3],[4,5],[6,7,8,9,10]))
5-element Array{Tuple{Int64,Int64,Int64},1}:
 (6,1,4)
 (7,2,5)
 (8,3,4)
 (9,1,5)
 (10,2,4)

```
