Skip to content

Instantly share code, notes, and snippets.

@NikitaAvvakumov
Created October 30, 2017 10:10
Show Gist options
  • Save NikitaAvvakumov/57a2376743a41785aca4dd8bcfc0ab6c to your computer and use it in GitHub Desktop.
Save NikitaAvvakumov/57a2376743a41785aca4dd8bcfc0ab6c to your computer and use it in GitHub Desktop.
Elixir pattern matching in `for` loop
items = [%{id: 1, deleted: false}, %{id: 2, deleted: true}, %{id: 3, deleted: false}]
for item <- items do
IO.puts Map.fetch!(item, :id)
end
# 1
# 2
# 3
# [:ok, :ok, :ok]
for %{deleted: false} = item <- items do
IO.puts Map.fetch!(item, :id)
end
# 1
# 3
# [:ok, :ok]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment