1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-04 23:58:01 +00:00

Add Replace Duplicates In A Keyword List as an elixir til

This commit is contained in:
jbranchaud
2016-02-11 12:05:11 -06:00
parent 25079085db
commit 5e9e235bc5
2 changed files with 27 additions and 0 deletions

View File

@@ -66,6 +66,7 @@ _330 TILs and counting..._
### Elixir
- [Append To A Keyword List](elixir/append-to-a-keyword-list.md)
- [Replace Duplicates In A Keyword List](elixir/replace-duplicates-in-a-keyword-list.md)
### Git

View File

@@ -0,0 +1,26 @@
# Replace Duplicates In A Keyword List
Use the
[`Keyword.put`](http://elixir-lang.org/docs/stable/elixir/Keyword.html#put/3)
function to replace duplicate key entries in a keyword list.
If there are no duplicate entries, the entry will just be added.
```elixir
Keyword.put([a: 1], :b, 2)
[b: 2, a: 1]
```
If there is a duplicate entry, it will be replaced by the new value.
```elixir
> Keyword.put([b: 1, a: 1], :b, 2)
[b: 2, a: 1]
```
If there are multiple duplicate entries, they will all be replaced.
```elixir
> Keyword.put([b: 3, b: 4, a: 1], :b, 2)
[b: 2, a: 1]
```