Erlang Sets / Useful in Elixir?

155 views
Skip to first unread message

Joseph Wilk

unread,
Jun 12, 2013, 2:49:27 PM6/12/13
to elixir-l...@googlegroups.com
Hello all,

I've been digging around the source of Elixir and notice no sets. I've found sets to be very useful in Clojure and have been missing them in Elixir.
I've started experimenting with a patch adding Erlangs sets (http://www.erlang.org/doc/man/sets.html) but wanted to sound out the idea here before I do something silly.

So a couple of questions:

* Would sets be a useful addition to Elixir? (I think they would!)
* Is there some data structure I've missed or another approach to something similar to sets in Elixir?

While we have Erlangs sets the syntax makes me sad inside.

:sets.from_list([1,2,3,4])

I would be much happier with something similar to Clojure's Set syntax (http://clojure.org/data_structures#Data Structures-Sets):

#{1, 2, 3, 4}

If there is interest I'm happy to submit an issue/patch via the Github Flow.

Thoughts and ideas appreciated,
Thanks,
--
Joseph Wilk



José Valim

unread,
Jun 12, 2013, 2:58:40 PM6/12/13
to elixir-l...@googlegroups.com
Hello Joseph!

In general, we don't recommend simply wrapping Erlang APIs in Elixir but I have been thinking about sets for a while as well. I am all in for having a Set in Elixir with two notes:

1) Sets are sometimes implemented using the same algorithm as dictionaries. In fact, Erlang sets are implemented on the same foundation as Erlang dicts. So if we are going to have a Set in Elixir, we should reimplement it using what we have learned with HashDict, which is considerably faster than Erlang's dict.

2) I don't think however that sets deserve a literal syntax as we wouldn't be able to actually pattern match on in.

What do you think?

José Valim
Skype: jv.ptec
Founder and Lead Developer





--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jonathan Hunt

unread,
Jun 12, 2013, 4:13:33 PM6/12/13
to elixir-l...@googlegroups.com
I'm in favour of sets. I think they're a very useful construct and
there are potential improvements we can achieve by wrapping them.

José - can't we use sigils to get a set syntax?

Jonny
--
Jonathan J Hunt <j...@me.net.nz> http://www.me.net.nz
"Science is the belief in the ignorance of experts" Richard Feynman

Joseph Wilk

unread,
Jun 13, 2013, 6:14:37 AM6/13/13
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
On Wednesday, June 12, 2013 8:58:40 PM UTC+2, José Valim wrote:
Hello Joseph!

In general, we don't recommend simply wrapping Erlang APIs in Elixir but I have been thinking about sets for a while as well. I am all in for having a Set in Elixir with two notes:

1) Sets are sometimes implemented using the same algorithm as dictionaries. In fact, Erlang sets are implemented on the same foundation as Erlang dicts. So if we are going to have a Set in Elixir, we should reimplement it using what we have learned with HashDict, which is considerably faster than Erlang's dict.


Sounds good, thanks for the pointer to HashDict, I'll start looking at that for the implementation of sets. I'll open a WIP pull request when I have something to show.

 

2) I don't think however that sets deserve a literal syntax as we wouldn't be able to actually pattern match on in.

Good point, I had not realised the contraint of literals being for pattern matching. While I would be happy to see something pretty thats the icing on the cake. I can go with Set.new at least to start with.

Alexei Sholik

unread,
Jun 13, 2013, 6:18:31 AM6/13/13
to elixir-l...@googlegroups.com
It'll be interesting to compare performance of a dedicated set implementation vs just using HashDict with garbage values.
--
Best regards
Alexei Sholik

José Valim

unread,
Jun 13, 2013, 6:24:34 AM6/13/13
to elixir-l...@googlegroups.com
It'll be interesting to compare performance of a dedicated set implementation vs just using HashDict with garbage values.

I don't think we will see a large difference performance wise. However, in terms of space, using a garbage value means 3 extra words will be required per item in the set. This is because HashDict stores key-values as 2-items tuples and a set doesn't require the tuple at all.

Reply all
Reply to author
Forward
0 new messages