Keyword

A keyword is a list of tuples where the first element of the tuple is an atom and the second element can be any value.

A keyword may have duplicated keys so it is not strictly a dictionary. However most of the functions in this module behaves exactly as a dictionary and mimic the API defined by the Dict behaviour.

For example, Keyword.get will get the first entry matching the given key, regardless if duplicated entries exist. Similarly, Keyword.put and Keyword.delete ensure all duplicated entries for a given key are removed when invoked. A handful of functions exist to handle duplicated keys, in particular, from_enum allows creating a new keywords without removing duplicated keys, get_values returns all values for a given key and delete_first deletes just one of the existing entries.

Since a keyword list is simply a list, all the operations defined in Enum and List can also be applied.

Source

Summary

delete(keywords, key)

Deletes all entries in the keyword list for a specific key. If the key does not exist, returns the keyword list unchanged. Use delete_first to delete just the first entry in case of duplicated keys

delete(keywords, key, value)

Deletes the entry in the keyword list for a key with value. If no key with value exists, returns the keyword list unchanged

delete_first(keywords, key)

Deletes the first entry in the keyword list for a specific key. If the key does not exist, returns the keyword list unchanged

drop(dict, keys)

Drops the given keys from the dict. Duplicated keys are preserved in the new keyword list

equal?(left, right)

Checks if two keywords are equal. I.e. they contain the same keys and those keys contain the same values

fetch!(keywords, key)

Fetches the value for specific key. If key does not exist, a KeyError is raised

fetch(keywords, key)

Fetches the value for a specific key and returns it in a tuple. If the key does not exist, returns :error

from_enum(enum)

Creates a Keyword from an enum. Unlike Keyword.new which behaves as a dict, Keyword.from_enum does not remove duplicated entries

get(keywords, key, default \\ nil)

Gets the value for a specific key

get_values(keywords, key)

Gets all values for a specific key

has_key?(keywords, key)

Returns whether a given key exists in the given keywords

keys(keywords)

Returns all keys from the keyword list. Duplicated keys appear duplicated in the final list of keys

keyword?(arg1)

Checks if the given argument is a keywords list or not

merge(d1, d2)

Merges two keyword lists into one. If they have duplicated entries, the one given as second argument wins

merge(d1, d2, fun)

Merges two keyword lists into one. If they have duplicated entries, the given function is invoked to solve conflicts

new()

Returns an empty keyword list, i.e. an empty list

new(pairs)

Creates a Keyword from an enumerable. Similar to dicts, duplicated entries are removed, the latest one prevails

new(pairs, transform)

Creates a Keyword from an enumerable with the help of the transformation function. Duplicated entries are removed, the latest one prevails

pop(dict, key, default \\ nil)

Returns the first value associated with key in the keyword list as well as the keyword list without key

pop_first(dict, key, default \\ nil)

Returns the first value associated with key in the keyword list as well as the keyword list without that particular ocurrence of key

put(keywords, key, value)

Puts the given value under key

put_new(keywords, key, value)

Puts the given value under key unless the entry key already exists

split(dict, keys)

Splits the given keywords in two given the given keys. Duplicated keys are preserved in the split keyword list

take(dict, keys)

Takes the given keys from the dict. Duplicated keys are preserved in the new keyword list

update!(list1, key, fun)

Updates the key with the given function. If the key does not exist, raises KeyError

update(list1, key, initial, fun)

Updates the key with the given function. If the key does not exist, inserts the given initial value

values(keywords)

Returns all values from the keyword list

Types

key :: atom

value :: any

t :: [{key, value}]

t(value) :: [{key, value}]

Functions

delete(keywords, key)

Specs:

Deletes all entries in the keyword list for a specific key. If the key does not exist, returns the keyword list unchanged. Use delete_first to delete just the first entry in case of duplicated keys.

Examples

iex> Keyword.delete([a: 1, b: 2], :a)
[b: 2]

iex> Keyword.delete([a: 1, b: 2, a: 3], :a)
[b: 2]

iex> Keyword.delete([b: 2], :a)
[b: 2]
Source
delete(keywords, key, value)

Specs:

Deletes the entry in the keyword list for a key with value. If no key with value exists, returns the keyword list unchanged.

Examples

iex> Keyword.delete([a: 1, b: 2], :a, 1)
[b: 2]

iex> Keyword.delete([a: 1, b: 2, a: 3], :a, 3)
[a: 1, b: 2]

iex> Keyword.delete([b: 2], :a, 5)
[b: 2]
Source
delete_first(keywords, key)

Specs:

  • delete_first(t, key) :: t

Deletes the first entry in the keyword list for a specific key. If the key does not exist, returns the keyword list unchanged.

Examples

iex> Keyword.delete_first([a: 1, b: 2, a: 3], :a)
[b: 2, a: 3]

iex> Keyword.delete_first([b: 2], :a)
[b: 2]
Source
drop(dict, keys)

Drops the given keys from the dict. Duplicated keys are preserved in the new keyword list.

Examples

iex> d = [a: 1, b: 2, c: 3, d: 4]
iex> Keyword.drop(d, [:b, :d])
[a: 1, c: 3]

iex> d = [a: 1, b: 2, c: 3, d: 4, a: 5]
iex> Keyword.drop(d, [:b, :d])
[a: 1, c: 3, a: 5]
Source
equal?(left, right)

Specs:

  • equal?(t, t) :: boolean

Checks if two keywords are equal. I.e. they contain the same keys and those keys contain the same values.

Examples

iex> Keyword.equal?([a: 1, b: 2], [b: 2, a: 1])
true
Source
fetch(keywords, key)

Specs:

Fetches the value for a specific key and returns it in a tuple. If the key does not exist, returns :error.

Examples

iex> Keyword.fetch([a: 1], :a)
{ :ok, 1 }

iex> Keyword.fetch([a: 1], :b)
:error
Source
fetch!(keywords, key)

Specs:

Fetches the value for specific key. If key does not exist, a KeyError is raised.

Examples

iex> Keyword.fetch!([a: 1], :a)
1

iex> Keyword.fetch!([a: 1], :b)
** (KeyError) key not found: :b
Source
from_enum(enum)

Specs:

Creates a Keyword from an enum. Unlike Keyword.new which behaves as a dict, Keyword.from_enum does not remove duplicated entries.

Source
get(keywords, key, default \\ nil)

Specs:

Gets the value for a specific key.

If key does not exist, return default value (nil if no default value).

If duplicated entries exist, the first one is returned. Use get_values/2 to retrieve all entries.

Examples

iex> Keyword.get([a: 1], :a)
1

iex> Keyword.get([a: 1], :b)
nil

iex> Keyword.get([a: 1], :b, 3)
3
Source
get_values(keywords, key)

Specs:

Gets all values for a specific key.

Examples

iex> Keyword.get_values([a: 1, a: 2], :a)
[1,2]
Source
has_key?(keywords, key)

Specs:

  • has_key?(t, key) :: boolean

Returns whether a given key exists in the given keywords.

Examples

iex> Keyword.has_key?([a: 1], :a)
true

iex> Keyword.has_key?([a: 1], :b)
false
Source
keys(keywords)

Specs:

Returns all keys from the keyword list. Duplicated keys appear duplicated in the final list of keys.

Examples

iex> Keyword.keys([a: 1, b: 2])
[:a,:b]

iex> Keyword.keys([a: 1, b: 2, a: 3])
[:a,:b,:a]
Source
keyword?(arg1)

Specs:

  • keyword?(term) :: boolean

Checks if the given argument is a keywords list or not.

Source
merge(d1, d2)

Specs:

  • merge(t, t) :: t

Merges two keyword lists into one. If they have duplicated entries, the one given as second argument wins.

Examples

iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4]) |> Enum.sort
[a: 3, b: 2, d: 4]
Source
merge(d1, d2, fun)

Specs:

Merges two keyword lists into one. If they have duplicated entries, the given function is invoked to solve conflicts.

Examples

iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4], fn (_k, v1, v2) ->
...>  v1 + v2
iex> end)
[a: 4, b: 2, d: 4]
Source
new()

Specs:

  • new :: t

Returns an empty keyword list, i.e. an empty list.

Source
new(pairs)

Specs:

Creates a Keyword from an enumerable. Similar to dicts, duplicated entries are removed, the latest one prevails.

Examples

iex> Keyword.new([{:b, 1}, {:a, 2}])
[a: 2, b: 1]
Source
new(pairs, transform)

Specs:

Creates a Keyword from an enumerable with the help of the transformation function. Duplicated entries are removed, the latest one prevails.

Examples

iex> Keyword.new([:a, :b], fn (x) -> {x, x} end) |> Enum.sort
[a: :a, b: :b]
Source
pop(dict, key, default \\ nil)

Returns the first value associated with key in the keyword list as well as the keyword list without key.

All duplicated entries are removed. See pop_first/3 for removing only the first entry.

Examples

iex> Keyword.pop [a: 1], :a
{1,[]}

iex> Keyword.pop [a: 1], :b
{nil,[a: 1]}

iex> Keyword.pop [a: 1], :b, 3
{3,[a: 1]}

iex> Keyword.pop [a: 1], :b, 3
{3,[a: 1]}

iex> Keyword.pop [a: 1, a: 2], :a
{1,[]}
Source
pop_first(dict, key, default \\ nil)

Returns the first value associated with key in the keyword list as well as the keyword list without that particular ocurrence of key.

Duplicated entries are not removed.

Examples

iex> Keyword.pop_first [a: 1], :a
{1,[]}

iex> Keyword.pop_first [a: 1], :b
{nil,[a: 1]}

iex> Keyword.pop_first [a: 1], :b, 3
{3,[a: 1]}

iex> Keyword.pop_first [a: 1], :b, 3
{3,[a: 1]}

iex> Keyword.pop_first [a: 1, a: 2], :a
{1,[a: 2]}
Source
put(keywords, key, value)

Specs:

Puts the given value under key.

If a previous value is already stored, all entries are removed and the value is overridden.

Examples

iex> Keyword.put([a: 1, b: 2], :a, 3)
[a: 3, b: 2]

iex> Keyword.put([a: 1, b: 2, a: 4], :a, 3)
[a: 3, b: 2]
Source
put_new(keywords, key, value)

Specs:

Puts the given value under key unless the entry key already exists.

Examples

iex> Keyword.put_new([a: 1], :b, 2)
[b: 2, a: 1]

iex> Keyword.put_new([a: 1, b: 2], :a, 3)
[a: 1, b: 2]
Source
split(dict, keys)

Splits the given keywords in two given the given keys. Duplicated keys are preserved in the split keyword list.

Examples

iex> d = [a: 1, b: 2, c: 3, d: 4]
iex> Keyword.split(d, [:a, :c, :e])
{ [a: 1, c: 3], [b: 2, d: 4] }

iex> d = [a: 1, b: 2, c: 3, d: 4, a: 5]
iex> Keyword.split(d, [:a, :c, :e])
{ [a: 1, c: 3, a: 5], [b: 2, d: 4] }
Source
take(dict, keys)

Takes the given keys from the dict. Duplicated keys are preserved in the new keyword list.

Examples

iex> d = [a: 1, b: 2, c: 3, d: 4]
iex> Keyword.take(d, [:a, :c, :e])
[a: 1, c: 3]

iex> d = [a: 1, b: 2, c: 3, d: 4, a: 5]
iex> Keyword.take(d, [:a, :c, :e])
[a: 1, c: 3, a: 5]
Source
update(list1, key, initial, fun)

Specs:

Updates the key with the given function. If the key does not exist, inserts the given initial value.

Examples

iex> Keyword.update([a: 1], :a, 13, &(&1 * 2))
[a: 2]

iex> Keyword.update([a: 1], :b, 11, &(&1 * 2))
[a: 1, b: 11]
Source
update!(list1, key, fun)

Specs:

Updates the key with the given function. If the key does not exist, raises KeyError.

Examples

iex> Keyword.update!([a: 1], :a, &(&1 * 2))
[a: 2]

iex> Keyword.update!([a: 1], :b, &(&1 * 2))
** (KeyError) key not found: :b
Source
values(keywords)

Specs:

Returns all values from the keyword list.

Examples

iex> Keyword.values([a: 1, b: 2])
[1,2]
Source