Implement iterator over subsets of array

Maybe if I provide some more information on the problem it gets clearer:

I have a struct named Atom, which contains the information of the atoms of my system. To simplify, let us suppose that it has 2 fields, the name and the molecule to which it belongs, i. e.:

struct Atom
  name::String
  molecule::Int
end

Now I have a vector of “atoms”, for example with 2 water molecules:

julia> atoms = [ Atom("O",1), Atom("H",1), Atom("H",1), 
                 Atom("O",2), Atom("H",2), Atom("H",2) ]
6-element Vector{Atom}:
 Atom("O", 1)
 Atom("H", 1)
 Atom("H", 1)
 Atom("O", 2)
 Atom("H", 2)
 Atom("H", 2)

The molecules are always consecutive (not necessarily consecutive, but the molecule numbers are unique for each molecule), and are numbered according to the molecule field. What I want is to iterate over the molecules, with:

for molecule in eachmolecule(atoms)
    ...
end

So I have to implement the eachmolecule function that generates the iterator.

One thing that I have to decide what is one molecule. It may be a vector of atoms, a view of a vector of atoms, or another struct with the range of atoms of the original vector of atoms (the option I am leaning to).

Working on it from the information provided. Thanks!