Created
August 27, 2016 09:48
-
-
Save tacitphoenix/a1c540841324c365a9436a66c633fab7 to your computer and use it in GitHub Desktop.
Elixir Keyword Lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Elixir Keyword Lists are a list of two item tuples where the first element of a tuple is an atom | |
iex(2)> k1 = [{:a, 1}, {:b, 2}, {:c, 3}, {:d, 4}] | |
[a: 1, b: 2, c: 3, d: 4] | |
iex(4)> k1 = [a: 1, b: 2, c: 3, d: 4] #a spoonful of sugar | |
[a: 1, b: 2, c: 3, d: 4] | |
iex(5)> k1 ++ {:b, 1} #not what we want | |
[{:a, 1}, {:b, 2}, {:c, 3}, {:d, 4} | {:b, 1}] | |
iex(6)> {:b, 1} ++ k1 #oops | |
** (ArgumentError) argument error | |
:erlang.++({:b, 1}, [a: 1, b: 2, c: 3, d: 4]) | |
iex(6)> k1 ++ [{:b, 1}] #remember the definition of keyword lists | |
[a: 1, b: 2, c: 3, d: 4, b: 1] | |
iex(7)> [{:b, 1}] ++ k1 #it works right | |
[b: 1, a: 1, b: 2, c: 3, d: 4] | |
iex(8)> k1 ++ [b: 1] #sugar is so sweet | |
[a: 1, b: 2, c: 3, d: 4, b: 1] | |
iex(9)> [b: 1] ++ k1 #sugar is so sweet | |
[b: 1, a: 1, b: 2, c: 3, d: 4] | |
iex(24)> [a: 1] ++ {:b, 2} ++ [c: 3] #please don't do this | |
** (ArgumentError) argument error | |
:erlang.++({:b, 2}, [c: 3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment