I got two lists x and y, both contain 10 items.
In first list some values are empty, but list 2 contains those values and visa versa, i.e
list x
[“one”]
[]
[“three”]
[“four”]
[]
list y
[]
[“two”]
[]
[]
[“five”]
new list z should contain all values [“one”][“two”][“three”][“four”][“five”], how can I merge them ?
I think it can also be done in one pass, with recursion, keeping the order of the list…
defmodule Koko do
def run [], [], acc do
Enum.reverse acc
end
def run [h1|t1], [h2|t2], acc do
new_acc = [(if not Enum.empty?(h1), do: h1, else: h2) |acc]
run t1, t2, new_acc
end
end
x = [["one"], [], ["three"], ["four"], []]
y = [[], ["two"], [], [], ["five"]]
Koko.run x, y, []
[["one"], ["two"], ["three"], ["four"], ["five"]]
Update: Oh, it looks like @Eiji already provided this solution