Confused by docs about


The docs for say:

Returns a list where each item is the result of invoking fun on each corresponding item of enumerable .

For maps, the function expects a key-value tuple.

However, the last example below that is a keyword list, not a map:

iex>[a: 1, b: 2], fn {k, v} -> {k, -v} end)
[a: -1, b: -2]

Is it a mistake in the docs?
Note that I’m a complete beginner just yet. Thank you for helping.


No, this is not mistake. Keyword lists are just lists of {atom(), any()} tuples, so:

[{:a, 1}, {:b, 2}] == [a: 1, b: 2]

Now you can understand why[a: 1, b: 2], fn {k, v} -> {k, -v} end) works as it is shown.


Thank you @hauleth
I got confused because it said that for maps, the function gets a tuple, while in the example, the function gets a tuple for a keyword list. I understand that keyword lists are a list of tuples, so yeah the example is correct. Just got me confused:)

1 Like

Certainly, I got confused this syntax sugar at first too.