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’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
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
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
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
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
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
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #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: