Confused by docs about Enum.map/2

Hello,

The docs for Enum.map/2 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> Enum.map([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.

2 Likes

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 Enum.map([a: 1, b: 2], fn {k, v} -> {k, -v} end) works as it is shown.

2 Likes

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.