Ettu_R
Merge two list of same length, empty values in one list filled with values from 2nd
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 ?
Marked As Solved
Eiji
defmodule Example do
def sample([], []), do: []
def sample([head | tail], [[] | tail2]), do: [head | sample(tail, tail2)]
def sample([[] | tail], [head2 | tail2]), do: [head2 | sample(tail, tail2)]
end
first = [["one"], [], ["three"], ["four"], []]
second = [[], ["two"], [], [], ["five"]]
Example.sample(first, second)
# [["one"], ["two"], ["three"], ["four"], ["five"]]
5
Also Liked
Phillipp
I would maybe start with List.zip/1 which gives you a result like:
[
{"one", ""},
{"", "two"},
{"three", ""},
...
]
And then you Enum.map/2 over it with a custom function that checks the tuple and returns the value.
5
lud
I would go with: :lists.zipwith(fn [], [v] -> [v] ; [v], [] -> [v] end, list_a, list_b).
2
mindok
Snap - you saved me typing…
1
kokolegorille
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 
1
Popular in Questions
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it.
I’m very interested in Elixir,...
New
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”:
14:57:30.512 [warn] ...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New







