# Iterating through the fieldnames of tuples

**URL:** <https://discourse.julialang.org/t/iterating-through-the-fieldnames-of-tuples/103288>\
**Category:** General Usage\
**Tags:** tuple\
**Created:** [August 28, 2023, 1:06pm UTC](https://discourse.julialang.org/t/iterating-through-the-fieldnames-of-tuples/103288 "2023-08-28T13:06:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gravlax](https://avatars.discourse-cdn.com/v4/letter/g/dbc845/32.png) [@Gravlax](https://discourse.julialang.org/u/Gravlax)\
**Post date:** [August 28, 2023, 1:06pm UTC](https://discourse.julialang.org/t/iterating-through-the-fieldnames-of-tuples/103288/1 "2023-08-28T13:06:20Z")

</div>

Hello,  
I have a problem where I use several named tuples with common structure and field names, e.g.:

```julia
a = (x=ones(N), y=ones(N), z=ones(N))
b = (x=ones(N), y=ones(N), z=ones(N))

```

I would like to loop through the fields of the tuples and apply some arithmetic operation, for now I do:

```julia
a.x .+= c*b.x + d
a.y .+= c*b.y + d
a.z .+= c*b.z + d

```

where `c` and `d` are either scalars or vectors of size `N`.

Is there a way to use a loop and the fact that tuple structure are similar to do this operation instead of doing it manually?  
If not, is there another type of data structure that would be more suitable?  
Thanks!

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [August 28, 2023, 1:18pm UTC](https://discourse.julialang.org/t/iterating-through-the-fieldnames-of-tuples/103288/2 "2023-08-28T13:18:02Z")

</div>

you can use `fieldtypes(typeof(a)` and `fieldnames(typeof(a))` if you are ok with scalar indexing to get the types of the tuple entries

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [August 28, 2023, 1:47pm UTC](https://discourse.julialang.org/t/iterating-through-the-fieldnames-of-tuples/103288/3 "2023-08-28T13:47:29Z")

</div>

Try

```julia
map(a, b) do a, b
    a .+= c * b + d
end

```

---

<div class="post-metadata">

**Author:** ![George9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george9000/32/23619_2.png) [@George9000](https://discourse.julialang.org/u/George9000)\
**Post date:** [August 28, 2023, 4:52pm UTC](https://discourse.julialang.org/t/iterating-through-the-fieldnames-of-tuples/103288/4 "2023-08-28T16:52:27Z")

</div>

Slight broadcasting edit. But works:

```julia
let
    N = 10
    c = 3
    d = fill(5, N)
    a = (x=ones(N), y=ones(N), z=ones(N))
    b = (x=ones(N), y=ones(N), z=ones(N))
    map(a, b) do a, b
        a .+= c .* b .+ d
    end
end

```

---

<div class="post-metadata">

**Author:** ![Gravlax](https://avatars.discourse-cdn.com/v4/letter/g/dbc845/32.png) [@Gravlax](https://discourse.julialang.org/u/Gravlax)\
**Post date:** [August 28, 2023, 4:54pm UTC](https://discourse.julialang.org/t/iterating-through-the-fieldnames-of-tuples/103288/5 "2023-08-28T16:54:13Z")

</div>

> [@aplavin](#):
>
> ```julia-auto
> map(a, b) do a, b
> a .+= c * b + d
> end
> 
> ```

yes, it does the job great on this example.  
How could that work when using tuples of tuples, e.g. in in the case a and b are already fields of a tuple?

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [August 28, 2023, 5:10pm UTC](https://discourse.julialang.org/t/iterating-through-the-fieldnames-of-tuples/103288/6 "2023-08-28T17:10:20Z")

</div>

> [@Gravlax](#):
>
> How could that work when using tuples of tuples, e.g. in in the case a and b are already fields of a tuple?

You can use a recursive function definition. Something like

```julia
function apply!(a, b)
	a .+= c .* b .+ d
end
function apply!(a::Tuple, b::Tuple)
	map(apply!, a, b)
end

```

I did not test this so there’s some chance there’s a subtle mistake. But the gist is usable.

For this, just call `apply!(a,b)` and it will keep drilling down through `Tuple` layers until it reaches a non-`Tuple` input pair, at which point it applies your calculation.

You’ll have to decide whether you want it to _continue recursing until you stop seeing a particular type_ (like here, where it goes until it gets non-tuples) or _stop recursing when you reach a specific type_ (where you would instead specialize the base case, for example when `a` and `b` are `AbstractArray` or `AbstractArray{<:Number}`).

---

<div class="post-metadata">

**Author:** ![Gravlax](https://avatars.discourse-cdn.com/v4/letter/g/dbc845/32.png) [@Gravlax](https://discourse.julialang.org/u/Gravlax)\
**Post date:** [August 29, 2023, 1:19pm UTC](https://discourse.julialang.org/t/iterating-through-the-fieldnames-of-tuples/103288/7 "2023-08-29T13:19:12Z")

</div>

thanks for your answer. Not sure I follow you, would you mind expanding a bit?
