jjabba
I’m working with Ecto modules and want to use MapSet and in particular MapSet.difference(s2, s1) to find which members of prime number collectors cards first edition I’m missing.
Let me exemplify:
my_cards = MapSet.new([{1, 2},{1, 3}, {1, 7}, {1, 11}])
full_collection = MapSet.new([{1, 2},{1, 3},{1, 5},{1, 7}, {1, 11}, {1, 13}])
missing_cards = MapSet.difference(full_collection, my_cards)
# MapSet<[{1, 5}, {1, 13}]>
Now I persist the full collection using an Ecto schema.
When trying to do the same math using instances of this model, it will fail, as the schema will have other members such as primary key {id}, timestamps and other.
How Can I augment either MapSet or the schema so that &MapSet.difference/2 is still useful using %PrimeNumberCollectorsCard{edition: 1, prime: 2} instances???
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
Are you wanting to treat your
%PrimeNumberCollectorsCard{}type ‘as’ things in the set? Or are you wanting to extract the numbers and put those in the set? Or are you wanting the set to be inside of%PrimeNumberCollectorsCard{}'s?Qqwy
Interesting question!
Only indirectly, by transforming your
%PrimeNumberCollectorsCard{}-struct into something that has an identical structure for equivalent datatypes.Let me first show you a simple ‘solution’ that directly uses
MapSet.difference, but still has a problem:This works mostly, but the return result of
missing_cards/2will be a MapSet containing identity-tuples like{1, 2}. You could write a function to recover the original%PrimeNumberCollectorsCard{}-struct, by constructing one (but then you do not have the fields that are missing from the identity representation), or by looking it up again in the database (which is slow since we basically discard results from an earlier query, and now do at least one (but possibly N) new queries).Instead of using
MapSet.difference, we might be able to use aMapdirectly: In a map, the keys have to be unique, but under each key we can track any value we like (whose uniqueness is not enforced by the map). This would make our code look as follows:Interestingly,
MapSet.difference/2is implemented much in the same way under the hood.jjabba
@OvermindDL1 My idea was to piggyback on mapset to sort out which members of s1 and s2 are the “same” but as per my own definition of what is ‘same’.
Yes
No, the whole point is to use the set operations in MapSet without having to transform and then resurrect the instances in the result from and to %PrimeNumberCollectorsCard{}
Or are you wanting the set to be inside of
%PrimeNumberCollectorsCard{}'s?No, I don’t see any point of that.
@Qqwy
Perhaps my brain is still too OOP wired, but I was expecting to find some way to provide my custom comparer to the ‘MapSet.difference(s1,s2,[comparer: fn(s, t) → …]’ or perhaps a way to augment the %PrimeNumberCollectorsCard{} object with a new way to compare itself to another instance. A way which would then be automatically honored by the &MapSet.difference/2
I could certainly take the route of re-implementing the MapSet.difference but the whole idea with my approach was to avoid re-inventing the wheel
OvermindDL1
MapSet is an old setup, it doesn’t have comparator function support or anything.
If you are just wanting the difference information between two of the sets then you could use something like map_diff or a variety of other libraries out there for that, or @Qqwy’s code is a good start for it.
I wouldn’t recommend MapSet in that case, it’s designed to be a very low level construct.
Qqwy
No worries: This has nothing to do with OOP vs functional programming.
Instead,
MapSetwas built in the way it is for (a) simplicity and (b) low memory (both RAM and when serialized on disk) usage. The trade-off has been made that elements in aMapSetare always expected to be unique according to their structural equality, see Erlang term comparisons’ “exactly equal to”.When adding a separate ‘element’ vs ‘element identity’ representation, you’ll have to either:
And with the alternate approach of using a ‘comparer’ function and storing the elements internally in a binary tree, we still reduce amortized constant running time to logarithmic running time, as well as needing
n * log namount of space for the tree.Neither of these approaches are bad, but they make different trade-offs than the ones made for
MapSet.I definitely do think that there is a place for sets that allow either custom identities or a custom comparer function. Hopefully, this will be filled by a library at some point, because re-inventing this wheel for every project definitely is not fun and not productive.
If you are wondering by the way, the difference between these two approaches (a comparer function vs custom identities) is similar to the difference between
Enum.sort/2andEnum.sort_by/2.