I am unable to figure out how this works. Here are example codes from Elixir docs, and my thought processes on how they work. Please let me know if there's something wrong.
# Code example from Elixir docs
users = [%{name: "john", age: 27}, %{name: "meg", age: 23}]
all = fn(:get, data, next) do
Enum.map(data, next)
end
get_in(users, [all, :age])
#=> [27, 23]
usersis matched to a list that contains two maps.allis matched to a function that takes:get, data, nextas parameters and performsEnum.map(data, next).Kernal.get_in(users, [all, :age])is called.- Since one of the keys,
all, is a function, it's invoked asall(:get, users, next). Enum.mapiterates over each element ofusersand callsnexton it, returning a list.- Value of
:agekey of each user in the list is returned as a list.
These are my questions based on this analysis:
- What is this function
next? It was never defined nor provided, but how come no error is reported? - Is it correct to summarize that when
get_inis called with a function (in this example,all) as a key, it returns the values of keys (in this example,:age) from the result of that function? - If my guess in 2. is correct, then what happens when the result of that function is not a dictionary type and does not have key-value pairs? Does it raise error?