jazzyer

jazzyer

Iterating or Pattern Matching on a certain conditions

I have a pretty straight-forward problem, but struggle to make it clear and concise (is that possible at all!?).

So having two lists:

list_1 = ["$100", "$200", "$300"]
list_2 = ["bob", "$200", "sam"]

success output should be:

"bob: $100, sam: $300"

fail output:

list_1 = ["$100", "$200", "$300"]
list_2 = ["bob", "bob", "sam"]
false

Obviously, list_2 (or our pattern) might be different, but if prices are matching we should return a success output similar to above.

I approached it right away by:

iex(2)> Enum.zip(["bob", "$200", "sam"], ["$100", "$200", "$300"])
[{"bob", "$100"}, {"$200", "$200"}, {"sam", "$300"}]

and then with a lot of conditional logic and accumulators in Enum.reduce solved the problem, but it’s super ugly… When my Elixir code is ugly - I’m always questioning myself, since there is always another and cleaner way.

How would you approach this? Is there a way?

Thank you all in advance!

Marked As Solved

gregvaughn

gregvaughn

Format more nicely in production code, but here’s a quick iex solution

iex(91)> list_1 = ["$100", "$200", "$300"]
["$100", "$200", "$300"]
iex(92)> list_2 = ["$100",  "tom", "sam"]
["$100", "tom", "sam"]
iex(93)> pairs = Enum.zip(list_1, list_2)
[{"$100", "$100"}, {"$200", "tom"}, {"$300", "sam"}]
iex(94)> if Enum.any?(pairs, &match?({a, a}, &1)), do: Enum.join((for {a, b} <- pairs, a != b, do: "#{b}: #{a}"), ", "), else: false
"tom: $200, sam: $300"

Also Liked

al2o3cr

al2o3cr

Based solely on what you’ve posted (I don’t really understand why list_2 has a mix of names and amounts):

list_1 = ["$100", "$200", "$300"]
list_2 = ["bob", "$200", "sam"]

pairs = Enum.zip(list_1, list_2)

unmatched_pairs = Enum.reject(pairs, fn {l1, l2} -> l1 == l2 end)

if length(unmatched_pairs) == length(list_1) do
  false
else
  unmatched_pairs
  |> Enum.map(fn {l1, l2} -> "#{l1}: #{l2}" end)
  |> Enum.join(", ")
end

(Edited to fix a bug - was looking at length(pairs) without filtering first)

al2o3cr

al2o3cr

Nope, you’re not missing anything - that was a bug! :slight_smile:

The intent is to check the length of the zipped pairs after filtering out ones like {"$200", "$200"} - if none of the pairs got filtered out, we need to return false (a “fail case”).

czrpb

czrpb

i too dont think i understand the 2 lists, but here is what i have for your 3 tests/examples:

iex(5)> test1 = {["$100", "$200", "$300"], ["bob",  "$200", "sam"]}
{["$100", "$200", "$300"], ["bob", "$200", "sam"]}
iex(6)> test2 = {["$100", "$200", "$300"], ["$100",  "tom", "sam"]}
{["$100", "$200", "$300"], ["$100", "tom", "sam"]}
iex(7)> test3 = {["$100", "$200", "$300"],["bob",  "tom", "sam"]}
{["$100", "$200", "$300"], ["bob", "tom", "sam"]}
iex(8)> [test1, test2, test3] |>
...(8)> Enum.map(fn {list1, list2} ->
...(8)>   Enum.zip(list1, list2) |>
...(8)>   Enum.reduce({false, []}, fn
...(8)>     {l1,l1}, {_, pairs} -> {true, pairs}
...(8)>     l1l2, {result, pairs} -> {result, [l1l2|pairs]}
...(8)>   end)
...(8)> end)
[
  true: [{"$300", "sam"}, {"$100", "bob"}],
  true: [{"$300", "sam"}, {"$200", "tom"}],
  false: [{"$300", "sam"}, {"$200", "tom"}, {"$100", "bob"}]
]

result/accumulator of the reduce is a tuple: {PASS-FAIL, PAIRS}

jazzyer

jazzyer

@czrpb and @gregvaughn
Thank you so much for your input and sorry about my delay here! That pandemic definitely is not over, got sick finally :frowning:

Again, really appreciate it!

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability 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
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement