mguimas

mguimas

Hi,

it seems there are several (too many, I think) options for making sets, but no clue which one to choose in which situations, like number of elements in the collection, speed and space complexity, to name a few factors.

How do you choose between all these options when you need to use element sets in your applications?

Thanks,

Showing Posts 1 to 6

NobbZ

NobbZ

Map is not a set. You can implement one on top of it, then you get a MapSet.

sets and gb_sets are some erlang implementations of different sets, also there is ordsets.

I’m not sure about how sets is implemented, ordsets though is just an ordered list of elements. gb_sets uses a self balancing tree to implement an ordered set.

In elixir code, I’d always prefer MapSet, as its interface just fits into elixir, as its designed for/in it.

In erlang code though I quite often just use sets, because it has the shortest module name :wink:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Some of the erlang modules are effectively legacy implementations from before Maps existed on which you could build MapSet. I believe there are benchmarks which show MapSet to be faster on all operations than the original sets.

mguimas

mguimas OP

I would say that by default one should then opt for MapSet, and to consider the other options if rewriting the application-level algorithm is not enough to get the desired increase in performance.

I see a use of Map to implement a set in those cases where a MapSet.pop would be handy. I believe that mimicking such non-existent function via MapSet.member? followed by MapSet.delete can be slower when data is large, otherwise Map would not need to provide such function.

peerreynders

peerreynders

defmodule Demo do
  def pop(%MapSet{map: map} = map_set, value, default \\ nil) do
    case :maps.take(value, map) do
      {_, new_map} -> {value, %{map_set | map: new_map}}
      :error -> {default, map_set}
    end
  end
end

set = MapSet.new([1, 2, 3])
IO.puts(inspect(set))
IO.puts(inspect(Demo.pop(set, 4)))
IO.puts(inspect(Demo.pop(set, 2)))

$ elixir demo.exs
#MapSet<[1, 2, 3]>
{nil, #MapSet<[1, 2, 3]>}
{2, #MapSet<[1, 3]>}
$ 

https://github.com/elixir-lang/elixir/blob/v1.9.1/lib/elixir/lib/map_set.ex#L138-L140

https://github.com/elixir-lang/elixir/blob/v1.9.1/lib/elixir/lib/map.ex#L631-L636

LostKobrakai

LostKobrakai

MapSet is an opaque datatype though. So this would make dialyzer scream at you.

peerreynders

peerreynders

I would have expected as much.

I see this as an optimization of edge functionality - I want this value removed from the set and know whether it existed in the set - in the service of still using a type that communicates the nature of a set rather than that of a key value store for its overall intended use.

The tradeoff I see here is that I can still use MapSet where appropriate (rather than Map) but am potentially forced to take extra steps to placate dialyzer for the optimized edge functionality (i.e. pop on a set).

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
kpanic
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
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 Top

JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews