I'm reading the great Joy of Elixir and I'm trying to wrap my head around the order of operations in the pattern matching example.
I've got the list
crew = [
%{name: "mal", age: 40},
%{name: "zoey", age: 36},
]
Now I can use pattern matching to take the first element of the list, and assign the name to a different variable like
[first = %{name: first_name} | others] = crew
I understand that the first element in crew was assigned to first and that the name of first was assigned to first_name:
iex> first
%{age: 40, name: "mal"}
iex> first_name
"mal"
According to the examples I can also assign the name to another variable from right-to-left:
%{name: first_name2} = first
But the same fails when I try it from left-to-right:
iex> first = %{name: first_name3}
** (CompileError) iex:33: undefined function first_name3/0
(stdlib) lists.erl:1354: :lists.mapfoldl/3
(stdlib) lists.erl:1355: :lists.mapfoldl/3
My question is: Why is left-to-right assignment OK within list matching, but fails outside of list matching?
[first = %{name: first_name} | others] = crew?first_nameis on the right side offirst- noamtcrewprovides the data here - denis.peplin