Concatenation of tuples in an elixir

In elixir we can concatenate lists like this

ex(52)> [1,2,3,4] ++ [5,6,7] [1, 2, 3, 4, 5, 6, 7] 

Can tuples be concatenated? Something like that?

 iex(53)> {1,2,3,4} ++ {5,6,7} ** (ArgumentError) argument error :erlang.++({1, 2, 3, 4}, {5, 6, 7}) 

The only thing I can think of is to convert the tuple to a list, and then convert it back to a tuple using the to_list and to_tuple . But this way is too awkward.

+7
elixir
source share
1 answer

You cannot link tuples.

The only reason is that you should not use them as such. Most of the use of tuples requires knowing their size, and things get rougher if you can combine them. In addition, to concatenate tuples, you need to copy both tuples in memory, which is inefficient.

In other words, if you want to combine tuples, you may have the wrong data structure. You have two options:

  • Use Lists
  • Make tuples: instead of a ++ b just write {a, b}
+21
source share

All Articles