czrpb
This took me way too long and I think its right/working, but .. am I missing a better solution?
defmodule IntersectingSets do
def meld([], [], [], result) do
result
end
def meld([item|rest], [], [], result) do
meld(rest, [item], result, [])
end
def meld(rest, [item], [], result) do
meld(rest, [], [], [item|result])
end
def meld(rest, [item], [head|tail], result) do
cond do
MapSet.intersection(item, head) |> MapSet.size == 0 ->
meld(rest, [item], tail, [head|result])
true ->
meld(rest, [MapSet.union(item, head)], tail, result)
end
end
def meld([item|rest]) do
meld(rest, [], [], [item])
end
end
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’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
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
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
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
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
Why so complicate?
… or maybe I didn’t get what You want
UPDATE: Well, I didn’t get what You want to do… better use data and expected result in that case.
Also, I would not write it like this… first the main entry function, then the rest private…
With your code, You need to start to read from the bottom.
asummers
I’m also unclear what the end result is at first glance. Additionally of the actual code, if/else is generally preferred over cond and you can use
Enum.empty?/1instead of theMapSet.size == 0al2o3cr
FWIW, you’ll get much better responses if you describe what this code is supposed to do.
Tracing through an example by hand, it will return the whole input list if every given MapSet intersects with every other one. Non-intersecting sets will be combined.
Some general notes on structure:
the second argument is always patterned-matched against either
[]or[item]. Consider removing the useless list wrapping and usenilvsiteminstead+1 to using
ifover a cond with one non-default branchconsider using
MapSet.disjoint?/2- there’s probably a tiny performance benefit to not constructing the whole result ofMapSet.intersection, but more importantly it’s clearer what the intent of the check isshanesveller
This is much more idiomatic, agreed. If it’s part of a very hot loop in the code with tight performance tolerances, the specialized/simple version may still perform better, so it would be a good idea to measure with Benchee or similar.
mudasobwa
You might use
Enum.reduce/3in the following way (untested, but it should work):The only interesting thing here is that once we discovered all the sets joined the tested one, all of them are to be joined.
czrpb
thx everyone! working a bit too much but once i do some rewrite based on comments here ill post again!
czrpb
(cant figure out how to add some description to the original post?!! i must be a dummy!
)
addressing replies:
Goal: Given a list of sets, return a list of the union of intersecting sets. Can think of this as a finding all the components in a network graph. Component (graph theory) - Wikipedia
Algo:
Reducethe set, and on each reduction,reducethe found intersecting sets and merge (union) the current set (from the 1st/outsidereduce) into the appropriate intersecting set.czrpb
This is AWESOME!
Testing:
mudasobwa
Actually, it might be even simplified. As @saverio-kantox pointed out to me,
caseis not needed at all. The second clause perfectly does in both cases:For empty
joined,reduce/3would immediately return the initial accumulator.czrpb
tested: