add some function to improve the `--` operation

46 views
Skip to first unread message

ruby...@foxmail.com

unread,
Mar 30, 2017, 11:24:03 AM3/30/17
to elixir-lang-core
hi, elixir team!

I'm using elixir in some projects, and I often using `--` operator like this:
```
list1 = [1, 2, 3]
list2 = [2, 4]

new_list = list1 -- list2
```

I think is the easiest way to find the element in list1 and not in list2. But it seems very expensive, as this article said:

http://erlang.org/doc/efficiency_guide/commoncaveats.html#id64604

Could we add a function like `List.minus` or `List.remove` to do this operation in a better way?

José Valim

unread,
Mar 30, 2017, 11:34:58 AM3/30/17
to elixir-l...@googlegroups.com
The cost of the operation is O(m * n). It doesn't matter the operands. If the left side has three elements and the right has two, you need to traverse the left side two times, resulting in (3 * 2) operations altogether. If you reverse, you need to traverse the left side three times, which results in (2 * 3).

Plus we cannot know which list is shorter since calculating the length of the list is linear itself. If you know which one is shorter or you know the elements to delete, then you can use List.delete/2.

Using -- in some cases is fine but, if you are performing it multiple times, then you do likely need a better data structure.


José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/29f1a6c9-2329-464d-b45f-8669ba7dd254%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ruby...@foxmail.com

unread,
Mar 30, 2017, 11:40:02 AM3/30/17
to elixir-lang-core, jose....@plataformatec.com.br
Thank you!


On Thursday, March 30, 2017 at 11:34:58 PM UTC+8, José Valim wrote:
The cost of the operation is O(m * n). It doesn't matter the operands. If the left side has three elements and the right has two, you need to traverse the left side two times, resulting in (3 * 2) operations altogether. If you reverse, you need to traverse the left side three times, which results in (2 * 3).

Plus we cannot know which list is shorter since calculating the length of the list is linear itself. If you know which one is shorter or you know the elements to delete, then you can use List.delete/2.

Using -- in some cases is fine but, if you are performing it multiple times, then you do likely need a better data structure.


José Valim
Skype: jv.ptec
Founder and Director of R&D

On Fri, Mar 31, 2017 at 12:24 AM, <ruby...@foxmail.com> wrote:
hi, elixir team!

I'm using elixir in some projects, and I often using `--` operator like this:
```
list1 = [1, 2, 3]
list2 = [2, 4]

new_list = list1 -- list2
```

I think is the easiest way to find the element in list1 and not in list2. But it seems very expensive, as this article said:

http://erlang.org/doc/efficiency_guide/commoncaveats.html#id64604

Could we add a function like `List.minus` or `List.remove` to do this operation in a better way?

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

Matt Jadczak

unread,
Apr 2, 2017, 10:53:02 AM4/2/17
to elixir-lang-core, jose....@plataformatec.com.br
You may find MapSet useful for your use case.
Reply all
Reply to author
Forward
0 new messages