Help updating older Julia iterator code

question

#1

Hi Julia-ites. I’m the author of the Python package PyQuante, 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. 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


#2

Is this what you are looking for?

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)

#3

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


#4

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.


#5

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.