# Help updating older Julia iterator code

**URL:** <https://discourse.julialang.org/t/help-updating-older-julia-iterator-code/6599>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 21, 2017, 7:27pm UTC](https://discourse.julialang.org/t/help-updating-older-julia-iterator-code/6599 "2017-10-21T19:27:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpmuller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rpmuller/32/2262_2.png) [@rpmuller](https://discourse.julialang.org/u/rpmuller)\
**Post date:** [October 21, 2017, 7:27pm UTC](https://discourse.julialang.org/t/help-updating-older-julia-iterator-code/6599/1 "2017-10-21T19:27:22Z")

</div>

Hi Julia-ites. I’m the author of the Python package [PyQuante](https://github.com/rpmuller/pyquante2), which is a quantum chemistry package written in Python. In order to better learn Julia, I spent a weekend a couple of years ago [translating the core of the program into Julia](https://github.com/rpmuller/pyquante2/blob/master/julia/pyquante.jl). I just picked up the code again, and several pieces of it no longer work. The one I’m tripping over now is the code I wrote to iterate over pairs of indices:  
function pairs2(n::Int64)  
function \_it()  
for i in 1:n  
for j in 1:i  
produce((i,j))  
end  
end  
end  
Task(\_it)  
end

My intention was to make this roughly equivalent to the python itertools.combinations(range(n),2).  
Or even  
def pairs2(n):  
for i in range(n):  
for j in range(i):  
yield i,j

The problem with writing a whole lot of code in a single weekend using a new language is that one doesn’t remember exactly one did things. I can’t remember why the Task command was necessary, but my vague recollection was that it turned the \_it() function into an iterator.

In any case, I now get come confusing command that I should be using Channels for interprocess communications, which makes me suspect that Task() was the wrong thing to use in the first place.

Can anyone help me out? Thanks in advance,

Rick

---

<div class="post-metadata">

**Author:** ![Alexander\_Belopolsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_belopolsky/32/4882_2.png) [@Alexander\_Belopolsky](https://discourse.julialang.org/u/Alexander_Belopolsky)\
**Post date:** [October 21, 2017, 8:05pm UTC](https://discourse.julialang.org/t/help-updating-older-julia-iterator-code/6599/2 "2017-10-21T20:05:54Z")

</div>

Is this what you are looking for?

```julia
julia> pairs(n) = ((i, j) for i = 1:n for j = 1:i)
pairs (generic function with 1 method)

julia> collect(pairs(3))
6-element Array{Tuple{Int64,Int64},1}:
 (1, 1)
 (2, 1)
 (2, 2)
 (3, 1)
 (3, 2)
 (3, 3)

```

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [October 21, 2017, 8:08pm UTC](https://discourse.julialang.org/t/help-updating-older-julia-iterator-code/6599/3 "2017-10-21T20:08:14Z")

</div>

[https://docs.julialang.org/en/stable/stdlib/iterators/#Base.Iterators.product](https://docs.julialang.org/en/stable/stdlib/iterators/#Base.Iterators.product) ?

---

<div class="post-metadata">

**Author:** ![rpmuller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rpmuller/32/2262_2.png) [@rpmuller](https://discourse.julialang.org/u/rpmuller)\
**Post date:** [October 21, 2017, 8:33pm UTC](https://discourse.julialang.org/t/help-updating-older-julia-iterator-code/6599/4 "2017-10-21T20:33:28Z")

</div>

Sheesh! (Bangs head against the desk several times.)

Thank you very much. It’s frustrating being so _bad_ at a language, after being so _good_ at another.

---

<div class="post-metadata">

**Author:** ![rpmuller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rpmuller/32/2262_2.png) [@rpmuller](https://discourse.julialang.org/u/rpmuller)\
**Post date:** [October 21, 2017, 8:34pm UTC](https://discourse.julialang.org/t/help-updating-older-julia-iterator-code/6599/5 "2017-10-21T20:34:03Z")

</div>

Not quite, but thanks for the pointer. This routine give the cartesian product. In the current instance I want something that gives the triangular product, which is the same thing, except i\<j.
