Concat Lists with Elixir

Essential Elixir : part 6 of 10 published on Jul 08, 2015

Coming from Ruby, you might try to concat two lists with +. That fails.

x = ["Iron Man", "Captain America", "Black Widow"]
y = ["The Hulk", "Hawkeye"]
# FAIL
avengers = x + y
** (ArithmeticError) bad argument in arithmetic expression
    :erlang.+([1], [2])

Using Enum.concat

You can concat two lists using the function on the Enum module.

x = ["Iron Man", "Captain America", "Black Widow"]
y = ["The Hulk", "Hawkeye"]
avengers = Enum.concat(x, y)

Using ++

But lets face it, Enum.concat lacks a bit of sexiness. Luckily there is the ++ operator.

x = ["Iron Man", "Captain America", "Black Widow"]
y = ["The Hulk", "Hawkeye"]
avengers = x ++ y

Sexy.

Resources

Next: Elixir is just cool. An example with pattern matching and structs.