mirror of
https://github.com/jbranchaud/til
synced 2026-01-08 01:28:02 +00:00
Add Append To A Keyword List as an elixir til
Add a new Elixir section too
This commit is contained in:
42
elixir/append-to-a-keyword-list.md
Normal file
42
elixir/append-to-a-keyword-list.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# Append To A Keyword List
|
||||
|
||||
If you have two keyword lists, you can append them like so:
|
||||
|
||||
```elixir
|
||||
> a = [a: 1]
|
||||
[a: 1]
|
||||
> b = [b: 2]
|
||||
[b: 2]
|
||||
> a ++ b
|
||||
[a: 1, b: 2]
|
||||
```
|
||||
|
||||
But what if something a bit more programmatic is happening and you are
|
||||
building up the additions to the keyword list based on variables?
|
||||
|
||||
```elixir
|
||||
> x = :x
|
||||
:x
|
||||
> c = a ++ [x 5]
|
||||
** (CompileError) iex:5: undefined function x/1
|
||||
(stdlib) lists.erl:1353: :lists.mapfoldl/3
|
||||
(stdlib) lists.erl:1354: :lists.mapfoldl/3
|
||||
```
|
||||
|
||||
That makes elixir think `x` is some function when in fact it is just a
|
||||
variable containing the keyword `:x`.
|
||||
|
||||
Simply adding a comma doesn't quite do it either.
|
||||
|
||||
```elixir
|
||||
> c = a ++ [x, 5]
|
||||
[{:a, 1}, :x, 5]
|
||||
```
|
||||
|
||||
We need to wrap the internal part with curly braces to create the tuple that
|
||||
can then be appended to `a`.
|
||||
|
||||
```elixir
|
||||
> c = a ++ [{x, 5}]
|
||||
[a: 1, x: 5]
|
||||
```
|
||||
Reference in New Issue
Block a user