Andres
Hello.
In my journey to learning Elixir, I gave myself the task of implementing an
algorithm that obtains the intersection of two MapSets without using the
built-in MapSet.intersection/2 function:
a = MapSet.new([3, 3, 3, 2, 2, 1])
b = MapSet.new([3, 4, 5])
# => Expected result #MapSet<[3]>
def intersection_a(a, b) do
Enum.reduce(b, MapSet.new(), fn x, acc ->
if MapSet.member?(a, x) do
MapSet.put(acc, x)
else
acc
end
end)
end
def intersection_b(a, b) do
# Returns a List[3] instead of #MapSet<[3]>
Enum.filter(b, fn x -> MapSet.member?(a, x) end)
end
The intersection_b function returns a List instead of a MapSet.
Why does this happen? ![]()
Any suggestions to improve the algorithm is welcome.
Thanks.
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
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 everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
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
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sribe
It’s a quirk of Elixir that most of the Enum functions that return collections return lists rather than the type you start with–it has to do with lack of typing and no good way to definitely determine what the return type should be. You’re looking for
Enum.intoto turn your list into a map.As for the first algorithm, that is pretty much the core of what you want to do; my only suggestion would be a modification for performance in some edge cases. Imagine that
ahas 1,000,000 elements andbhas one, what does your code do? Now imagine the opposite,ahas one andbhas 1,000,000Andres
Hi @sribe. Thanks so much for your answer.
I did not know that. I will pay close attention to this caveat.
I would greatly appreciate an example of the optimization you suggest.
lucaong
I think that @sribe is suggesting to choose which collection you iterate through (
aorbin your code) depending on which one is the smallest. You can in fact minimize the number of comparisons necessary to produce the intersection by iterating over the smallest of the two collections.If
ahas 1,000,000 elements andbhas one, iterating throughbinstead ofalets you produce the intersection after only one comparison instead of 1,000,000This is more efficient by virtue of the fact that
MapSet.member?has a performance that grows less than linearly with the size of the collection. So making fewerMapSet.member?checks on bigger collections tends to be better than many checks on smaller collectionsAndres
Hello @lucaong.
Thank you very much for your explanation. Very clear and concise.
al2o3cr
The source is a good place to start:
https://github.com/elixir-lang/elixir/blob/98485daab0a9f3ac2d7809d38f5e57cd73cb22ac/lib/elixir/lib/map_set.ex#L247-L250
LostKobrakai
It’s not so much “lack of types”, but more that there are quite a few Enumerable types out there, which cannot be transformed if you’d like to preserve the type. Especially if you’re looking at lazy or infinite collections things might just not work. But even if you look at something as simple as
%Date.Range{}. If you map a date range to only the day of month it’s no longer a date range.Not keeping the shape of data was a conscious decision, which is explained (in a sadly little hidden place) here: Collectable — Elixir v1.20.2
Andres
Hi @al2o3cr
Definitely. Thanks so much for your answer.
Andres
Thank you very much for your clarification and for sharing this link.