So far we haven’t discussed any associative data structures, i.e. data structures that are able to associate a certain value (or multiple values) to a key. Different languages call these different names like dictionaries, hashes, associative arrays, etc.
In Elixir, we have two main associative data structures: keyword lists and maps. In this chapter, we'll focus on Keyword lists.
In many functional programming languages, it is common to use a list of 2-item tuples as the representation of an associative data structure. In Elixir, when we have a list of tuples and the first item of the tuple (i.e. the key) is an atom, we call it a keyword list. For example:
To retrieve the value associated with an atom in the keyword list, psss the atom as to [] after name of the list:
In Elixir, we have two main associative data structures: keyword lists and maps. In this chapter, we'll focus on Keyword lists.
In many functional programming languages, it is common to use a list of 2-item tuples as the representation of an associative data structure. In Elixir, when we have a list of tuples and the first item of the tuple (i.e. the key) is an atom, we call it a keyword list. For example:
list = [{:a, 1}, {:b, 2}]Elixir supports a special syntax for defining such lists. We can place the colon at end of each atom and get rid of the tuples entirely. For example,
list_1 = [{:a, 1}, {:b, 2}] list_2 = [a: 1, b: 2] IO.puts(list_1 == list_2)When running above program, it produces following result:
trueBoth of these represent a keyword list. Since keyword lists are also lists we can use all the operations we used on lists on them.
To retrieve the value associated with an atom in the keyword list, psss the atom as to [] after name of the list:
list = [a: 1, b: 2] IO.puts(list[:a])When running above program, it produces following result:
1Keyword lists have three special characteristics:
- Keys must be atoms.
- Keys are ordered, as specified by the developer.
- Keys can be given more than once.
Accessing a key
To access values associated with a given key, we use the Keyword.get function. It returns the first value associated with the given key. To get all the values, we use the Keyword.get_values function. For example:kl = [a: 1, a: 2, b: 3] IO.puts(Keyword.get(kl, :a)) IO.puts(Keyword.get_values(kl))When running above program, it produces following result:
1 [1, 2]
Inserting a key
To add a new value, use Keyword.put_new. If the key already exists, its value remains unchanged:kl = [a: 1, a: 2, b: 3] kl_new = Keyword.put_new(kl, :c, 5) IO.puts(Keyword.get(kl_new, :c))When running above program, it produces a new Keyword list with additional key, c and gives the result:
5
Deleting a key
If you want to delete all entries for a key, use Keyword.delete; to delete only the first entry for a key, use Keyword.delete_first.kl = [a: 1, a: 2, b: 3, c: 0] kl = Keyword.delete_first(kl, :b) kl = Keyword.delete(kl, :a) IO.puts(Keyword.get(kl, :a)) IO.puts(Keyword.get(kl, :b)) IO.puts(Keyword.get(kl, :c))This will delete the first b in the List and all the a in the list. When running above program, it produces following result:
0
No comments:
Post a Comment