From 5e9e235bc55f31373d89c56c661fd89f787087e0 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 11 Feb 2016 12:05:11 -0600 Subject: [PATCH] Add Replace Duplicates In A Keyword List as an elixir til --- README.md | 1 + .../replace-duplicates-in-a-keyword-list.md | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 elixir/replace-duplicates-in-a-keyword-list.md diff --git a/README.md b/README.md index 3f59e67..330f40b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/elixir/replace-duplicates-in-a-keyword-list.md b/elixir/replace-duplicates-in-a-keyword-list.md new file mode 100644 index 0000000..acf5e1a --- /dev/null +++ b/elixir/replace-duplicates-in-a-keyword-list.md @@ -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] +```