How to add two structs with associations and non-numeric fields?

I have two same structs.
a = %S1{numeric: 1, non_numeric: “hi”}
b = %S1{numeric: 1, non_numeric: "hello}

I want to add a’s numeric field to b, like;
b = b + a = %S1{numeric: 2, non_numeric: "hello}

If I have only one numeric field, it might be easy. But, for more then 100 fields, is there any shortcut to do it?

Thank you again, al2o3cr and NobbZ for my last questions.

This ought to get you on your way:

    a = %S1{n1: 1, n2: 2, alpha: "hi"}
    b = %S1{n1: 3, n2: 4, alpha: "hello"}

    Map.merge(a, b, fn
      _key, a, b when is_number(a) and is_number(b) -> a + b
      _key, _a, b -> b
    end)
1 Like