Skip to content

Instantly share code, notes, and snippets.

@keroro520
Last active November 28, 2017 07:53
Show Gist options
  • Save keroro520/61173bda3b1441e8a6be25eb0166c6f8 to your computer and use it in GitHub Desktop.
Save keroro520/61173bda3b1441e8a6be25eb0166c6f8 to your computer and use it in GitHub Desktop.
How can I iterate two list of maps at once?
defmodule IterateProposalsCustomers do
def input do
proposals = [
%{customer_id: "5202845", other: "foo"},
%{customer_id: "9778978", other: "foo"}
]
customers = [
%{id: "5202845", name: "foo"},
%{id: "5643635", name: "bar"},
%{id: "9778978", name: "baz"},
%{id: "3423454", name: "boo"}
]
{proposals, customers}
end
def run(proposals, customers) do
customer_ids = Enum.map(proposals, &Map.get(&1, :customer_id))
customers_id_names = Enum.map(customers, &{Map.get(&1, :id), Map.get(&1, :name)}) |> Enum.into(%{})
Map.take(customers_id_names, customer_ids)
end
def run do
{proposals, customers} = input
run(proposals, customers)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment