owaisqayum
How to find unique values by two parameters?
I am having a List of Tuples which contains some data and the last element of each tuple is a Map. Now, i want to remove duplications and find the unique values by maximum value %{u: [{_, _, value}]} in the map.
[
{"Elixir", 2019,
%{
values: %{u: [{:b, :r1, 1}]},
status: true
}},
{"Elixir", 2020,
%{
values: %{
u: [
{:b, :r2, 1},
{:b, :r3, 2}
]
},
status: true
}},
{"Elixir", 2020,
%{
values: %{
u: [
{:b, :r2, 2},
{:b, :r3, 2}
]
},
status: true
}}
]
The final output should look like
[
{"Elixir", 2019,
%{
values: %{u: [{:b, :r1, 1}]},
status: true
}},
{"Elixir", 2020,
%{
values: %{
u: [
{:b, :r2, 2},
{:b, :r3, 2}
]
},
status: true
}}
]
So it also removed the duplicated {elixir, 2020} and left behind the tuple having the maximum value of 2.
I tried it using groups but I don’t how to go inside the maps and then compare two different tuples.
Thanks
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 10 of 11 Posts
hauleth
Wouldn’t:
Do the job?
owaisqayum
Unfortunately, It’s still returning the same list. Also, you hardcoded the name and year, it can be more then or even less than the specified arguments.
Thanks
APB9785
Name and year are not hard-coded in @hauleth’s solution. Those are variables assigned via pattern-matching, a very common syntax in Elixir.
The reason it’s not producing your expected output, is that both of the examples given have the same maximum value of 2. There is nothing in your requirements which would expect the
[2, 2]list to be preferred over the[1, 2]list.Also, you could consider “cleaning up” the data by converting it into a more easily accessible data structure, before attempting the comparison.
dimitarvp
What do you consider a duplicate here? The way I am seeing both
{elixir, 2020}groups of data, even theuvalues aren’t the exact same lists of other values.APB9785
I did a quick prototype of how one might implement this manually:
Like I mentioned above, checking by maximum value alone might not always give the expected result, so you might need to tweak this. In order to get your desired result from the example data, I used
>=comparison so that the latter item would override the former in case of a tie. But I suspect this might not be sufficiently robust for all cases.Sebb
The requirements are not clear.
Thats always a good idea.
For example: why is there a map with only one key?
Also nested data is often a pain in the ***.
owaisqayum
Thanks for such a valuable response. Actually by
hard codedi mean that the parameters won’t always be name and year. It can be one or more arguments.The difference between [2, 1] and [2, 2] is that the later is the updated value of r1 hence updating it by + 1.
which structure would be more accessible, should i use Keyword Lists or Structs. Kindly help me on that.
Thanks
APB9785
When you use tuples, you are saying that the data format (e.g. number of arguments) will always be the same. If you plan on having additional arguments, you should be using a List, or preferably a Map/Struct so anyone else reading your code can easily see what the values are supposed to represent.
If
ris being incremented, you should not use atoms for this, but an Integer.Some of your key names are ambiguous - like, I have no idea, what
values.umeans, or what:bsignifies, and I’m only just now learning the significance of:r1,:r2etc… So it’s difficult for me to say what specifically would be the best way to structure your data, but here is a try:and here is the code I gave above, updated for this structure:
APB9785
You say these are the values, but then you have a different map called
:values. Whatever is the “payload” - the values you need for your API, those should be kept together and made easily accessible.You will notice that there is not even a function in Elixir to get the last value from a tuple. You must hard-code the specific index you want. This is because you are not supposed to have variable-length tuples. (I already mentioned this in my previous post)
Why does it matter which query type was used? It doesn’t make sense to me that you would do multiple queries for the same data, and merge them into the same entry, but then still have problems with duplicate entries.
If
:r2is a unique identifier, then it shouldn’t be hidden away inside a tuple, it should be used as a key in a map, which will ensure that only one can exist at any given time. Then if a new:r2payload comes in, your code should decide right then whether or not to replace the existing one.owaisqayum
I use elem(tuples, tuple_size(tuple, -1))
You are right, infact I can make r1 as key and 1 as its value. But then how will I utilize the :u or :j .. Thats a bit of a confusing part. It’s necessary for my structure.