Create vector with dependent values

If I understand your goal correctly, you almost had it. Something like this?

julia> mystructgenerator(amin, amax) = 
  ( MyStruct(a, b, 0) for a in amax:-1:amin
      for b in Iterators.product(0:amax-a,0:amax-a,0:amax-a)
        if a+sum(b)==amax
  )
mystructgenerator (generic function with 1 method)

julia> gen = mystructgenerator(18, 20)
Base.Iterators.Flatten{Base.Generator{StepRange{Int64, Int64}, var"#39#40"{Int64}}}(Base.Generator{StepRange{Int64, Int64}, var"#39#40"{Int64}}(var"#39#40"{Int64}(20), 20:-1:18))

julia> collect(gen)
10-element Vector{MyStruct}:
 MyStruct(20, [0, 0, 0], 0)
 MyStruct(19, [1, 0, 0], 0)
 MyStruct(19, [0, 1, 0], 0)
 MyStruct(19, [0, 0, 1], 0)
 MyStruct(18, [2, 0, 0], 0)
 MyStruct(18, [1, 1, 0], 0)
 MyStruct(18, [0, 2, 0], 0)
 MyStruct(18, [1, 0, 1], 0)
 MyStruct(18, [0, 1, 1], 0)
 MyStruct(18, [0, 0, 2], 0)

I tend to agree with this comment Sum elements of a struct - #8 by stevengj, that you may not be using the right data structure for your problem. It’s hard to tell, because you haven’t explained what it is you are ultimately trying to achieve. You have been asking a few questions around MyStruct and how to extend various methods for it. Yet, I for one fail to see where you are going with this. The operations you are defining for this type seem quite artificial to me.

Anyway, hope this helps and please feel free to add any details.

2 Likes