In elixir, we can concatenate, lists like so
ex(52)> [1,2,3,4] ++ [5,6,7]
[1, 2, 3, 4, 5, 6, 7]
Can we also concatenate tuples? Something like this?
iex(53)> {1,2,3,4} ++ {5,6,7}
** (ArgumentError) argument error
:erlang.++({1, 2, 3, 4}, {5, 6, 7})
The only other thing I can think of is to convert a tuple to list, then convert back to tuple using the to_list
and to_tuple
functions. But that's way too clumsy.
You can't concatenate tuples.
The only reason is that you are not supposed to use them as such. Most of tuple usage requires knowing their size and things get blurrier if you can concatenate them. Furthermore, concatenating tuples requires copying both tuples in memory, which is not efficient.
In other words, if you want to concatenate tuples, you may have the wrong data structure. You have two options:
a ++ b
, just write {a, b}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With