defmodule App do
def list_users do
[
%{id: "1", name: "José", username: "josevalim"},
%{id: "2", name: "Bruce", username: "redrapids"},
%{id: "3", name: "Chris", username: "chrismccord"}
]
end
def test() do
Enum.find(list_users(), fn element ->
match?(%{name: "Bruce"}, element)
end)
end
end
IO.inspect(App.test()) # %{id: "2", name: "Bruce", username: "redrapids"}
The above code has data hardcoded as the argument of the match? function, and it works as expected.
If I pass an argument (the code below) it doesn’t work. I don’t understand why.
defmodule App do
def list_users do
[
%{id: "1", name: "José", username: "josevalim"},
%{id: "2", name: "Bruce", username: "redrapids"},
%{id: "3", name: "Chris", username: "chrismccord"}
]
end
def test(data) do
Enum.find(list_users(), fn element ->
match?(data, element)
end)
end
end
IO.inspect(App.test(%{name: "Bruce"}))
warning: variable "data" is unused (if the variable is not meant to be used, prefix it with an underscore)
app.ex:12: App.test/1
warning: variable "data" is unused (if the variable is not meant to be used, prefix it with an underscore)
app.ex:10: App.test/1
%{id: "1", name: "José", username: "josevalim"}