Enum.reduce for idiots

I would actually swap the roles of your lookup and params variables. If order doesn’t matter, you could just turn lookup into a map and iterate over params to create your expected output.

lookup = [
  %{key: "foo", type: :string},
  %{key: "bar", type: :string},
  %{key: "baz", type: :string}
]
lookup = Map.new(lookup, &{&1.key, &1.type})
#=> %{"bar" => :string, "baz" => :string, "foo" => :string}

for {key, value} <- params,
    {:ok, type} = Map.fetch(lookup, to_string(key)) do
  %{key: key, value: value, type: type}
end
3 Likes