# Iterating an action over fields of tuples with similar structure

**URL:** <https://discourse.julialang.org/t/iterating-an-action-over-fields-of-tuples-with-similar-structure/111806>\
**Category:** General Usage\
**Created:** [March 19, 2024, 9:08am UTC](https://discourse.julialang.org/t/iterating-an-action-over-fields-of-tuples-with-similar-structure/111806 "2024-03-19T09:08:28Z")\
**Posts on this page:** 5\
**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:** [March 19, 2024, 9:08am UTC](https://discourse.julialang.org/t/iterating-an-action-over-fields-of-tuples-with-similar-structure/111806/1 "2024-03-19T09:08:29Z")

</div>

Dear all,  
I’m trying to find a way to repeat an operation over the fields of tuples, which have consistent size.  
For example, let’s consider the sum `A = B + C`, but applied to each field of a tuple.

```julia
let
    size1 = (10, 10)
    size2 = (11, 11)

    a = (zeros(size1), zeros(size2))
    b = ( ones(size1), ones(size2))
    c = ( ones(size1), ones(size2))

    # Two lines
    @. a[1] = b[1] + c[1] 
    @. a[2] = b[2] + c[2] 
end

```

I would like to replace the two line statement by a single line. Is it possible to write a macro that could used to that end?  
e,g,:

```julia
@my_macro a = b + c 

```

Thanks for the hints!

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [March 19, 2024, 9:12am UTC](https://discourse.julialang.org/t/iterating-an-action-over-fields-of-tuples-with-similar-structure/111806/2 "2024-03-19T09:12:34Z")

</div>

I think this is one of the (comparatively) rare times that `map` is useful in Julia

```julia
a = map(.+, b, c)

```

seems to do what you want

ETA:  
actually, since matrices add element-wise, just plain `a = b .+ c` works fine. Are you doing something more complex?

---

<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:** [March 19, 2024, 9:29am UTC](https://discourse.julialang.org/t/iterating-an-action-over-fields-of-tuples-with-similar-structure/111806/3 "2024-03-19T09:29:23Z")

</div>

Thanks for this one! Haven’t thought about a `map`.  
Yes, I do more complex things than `.+`, I just took this for purpose of a MWE.  
There could be things like `a = b.*c - d +e` and things with shifted indices (finite difference operations).

---

<div class="post-metadata">

**Author:** ![narnold0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/narnold0/32/206975_2.png) [@narnold0](https://discourse.julialang.org/u/narnold0)\
**Post date:** [March 20, 2024, 4:35am UTC](https://discourse.julialang.org/t/iterating-an-action-over-fields-of-tuples-with-similar-structure/111806/4 "2024-03-20T04:35:34Z")

</div>

> [@Gravlax](#):
>
> Dear all,  
> I’m trying to find a way to repeat an operation over the fields of tuples, which have consistent size.  
> For example, let’s consider the sum `A = B + C`, but applied to each field of a tuple.
> 
> ```julia
> let
> size1 = (10, 10)
> size2 = (11, 11)
> 
> a = (zeros(size1), zeros(size2))
> b = ( ones(size1), ones(size2))
> c = ( ones(size1), ones(size2))
> 
> # Two lines
> @. a[1] = b[1] + c[1] 
> @. a[2] = b[2] + c[2] 
> end
> 
> ```
> 
> I would like to replace the two line statement by a single line. Is it possible to write a macro that could used to that end?  
> e,g,:
> 
> ```julia
> @my_macro a = b + c 
> 
> ```
> 
> Thanks for the hints!

Would this not just be

```julia
a = @. b +c 

```

or

```julia
 a = @. b*c-d+e

```

---

<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:** [March 20, 2024, 10:56am UTC](https://discourse.julialang.org/t/iterating-an-action-over-fields-of-tuples-with-similar-structure/111806/5 "2024-03-20T10:56:41Z")

</div>

Hmmm nice ones too! Is there a way such to avoid that `a` gets allocated? Could something like `a = @. b +c ` be made in-place?
