Ettu_R

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

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"]]

Also Liked

Phillipp

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.

lud

lud

I would go with: :lists.zipwith(fn [], [v] -> [v] ; [v], [] -> [v] end, list_a, list_b).

mindok

mindok

Snap - you saved me typing…

kokolegorille

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 :slight_smile:

Where Next?

Popular in Questions Top

bsollish-terakeet
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
LegitStack
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
Werner
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
stefanchrobot
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
shahryarjb
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
LegitStack
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
lanycrost
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 Top

Qqwy
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...
3268 119930 1237
New
shahryarjb
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
srinivasu
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
pmjoe
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
William
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
JorisKok
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
fayddelight
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
ovidiubadita
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
aesmail
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

We're in Beta

About us Mission Statement