# Create vector with dependent values

**URL:** <https://discourse.julialang.org/t/create-vector-with-dependent-values/80611>\
**Category:** New to Julia\
**Tags:** vector\
**Created:** [May 6, 2022, 1:41pm UTC](https://discourse.julialang.org/t/create-vector-with-dependent-values/80611 "2022-05-06T13:41:05Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [May 6, 2022, 3:36pm UTC](https://discourse.julialang.org/t/create-vector-with-dependent-values/80611/5 "2022-05-06T15:36:09Z")

</div>

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

```julia
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](https://discourse.julialang.org/t/sum-elements-of-a-struct/80559/8), 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.

---

_[View the full topic](https://discourse.julialang.org/t/create-vector-with-dependent-values/80611)._
