owaisqayum

owaisqayum

Filtering list of lists

I am having a list of lists and i want to obtain an output by filtering them.

list =
[
  [[key: 1, value: 2], [key: 1, value: 2]],
  [[key: 1, value: 2], [key: 2, value: 3]],
  [[key: 2, value: 3], [key: 1, value: 2]],
  [[key: 2, value: 3], [key: 2, value: 3]]
]

here In each list i want to filter out those values where the keys are equal.

For example, in first list we have

[[key: 1, value: 2], [key: 1, value: 2]]

here both keys are equal, so how can we filter only those results where both the keys are equal.

Thanks

Marked As Solved

owaisqayum

owaisqayum

So i have worked on it and found the solution for it

defmodule J do
  def join(list_of_lists) do
    join_single_row(list_of_lists, [])
    |> Enum.map(fn row -> List.flatten(row) end)
  end

  def join_single_row([], final), do: final

  def join_single_row([row | rest_of_rows], final) do
    length =
      Enum.map(row, fn [key, _value] -> key end)
      |> Enum.uniq()
      |> length()

    case length do
      1 ->
        join_single_row(rest_of_rows, final ++ [row])

      _ ->
        join_single_row(rest_of_rows, final)
    end
  end
end

Also Liked

gregvaughn

gregvaughn

:golf: :grin:

iex(5)> list =
...(5)> [
...(5)>   [[key: 1, value: 2], [key: 1, value: 2]],
...(5)>   [[key: 1, value: 2], [key: 2, value: 3]],
...(5)>   [[key: 2, value: 3], [key: 1, value: 2]],
...(5)>   [[key: 2, value: 3], [key: 2, value: 3]]
...(5)> ]

iex(6)> for [left, right] = row <- list, left[:key] == right[:key], do: row
[
  [[key: 1, value: 2], [key: 1, value: 2]],
  [[key: 2, value: 3], [key: 2, value: 3]]
]
kokolegorille

kokolegorille

Using Enum.map with Enum.filter would have worked too…

Please note that [key: :value] is a Keyword list… a special kind of list.

Where Next?

Popular in Questions Top

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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement