mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Remove One List From Another as an elixir til
This commit is contained in:
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
|
||||
warrant a full blog post. These are mostly things I learn by pairing with
|
||||
smart people at [Hashrocket](http://hashrocket.com/).
|
||||
|
||||
_513 TILs and counting..._
|
||||
_514 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -105,6 +105,7 @@ _513 TILs and counting..._
|
||||
- [Quitting IEx](elixir/quitting-iex.md)
|
||||
- [Range Into List Using Comprehensions](elixir/range-into-list-using-comprehensions.md)
|
||||
- [Referencing Values In IEx's History](elixir/referencing-values-in-iexs-history.md)
|
||||
- [Remove One List From Another](elixir/remove-one-list-from-another.md)
|
||||
- [Replace Duplicates In A Keyword List](elixir/replace-duplicates-in-a-keyword-list.md)
|
||||
- [Requiring Keys For Structs](elixir/requiring-keys-for-structs.md)
|
||||
- [Reversing A List](elixir/reversing-a-list.md)
|
||||
|
||||
29
elixir/remove-one-list-from-another.md
Normal file
29
elixir/remove-one-list-from-another.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Remove One List From Another
|
||||
|
||||
The `--/2` operator allows you to subtract two lists, that is, remove all
|
||||
elements in the _right_ list from the _left_ list. Each occurrence of an
|
||||
element is removed if there is a corresponding element. If there is no
|
||||
corresponding element, it is ignored.
|
||||
|
||||
Here are some examples.
|
||||
|
||||
```elixir
|
||||
> [1, 2, 3] -- [2, 4]
|
||||
[1, 3]
|
||||
> [:a, :b, :c, :a, :d, :a] -- [:a, :a]
|
||||
[:b, :c, :d, :a]
|
||||
```
|
||||
|
||||
This kind of list operation is not particularly efficient, so for large
|
||||
lists it can be quite slow. The following example took several minutes to
|
||||
run.
|
||||
|
||||
```elixir
|
||||
> Enum.into(1..1000000, []) -- Enum.into(2..1000000, [])
|
||||
[1]
|
||||
```
|
||||
|
||||
To achieve a true set difference, you'll note that the docs for this
|
||||
operator recommend checking out `MapSet.difference/2`.
|
||||
|
||||
See `h Kernel.--` for more details.
|
||||
Reference in New Issue
Block a user