Why not just fold over your list and `Map.merge/3` over them with a
function that handles the duplicates (and throws or whatever you wish)?
On Wednesday, October 4, 2017 at 2:58:17 AM UTC-6, Pedro Medeiros wrote:
>
> Overall I like the idea of having a separate function that throws an error
> in case there are duplicated keys. Just don't think it's the place to be
> done on Map.new/2 It works that way on Erlang maps:from_list and also with
> for
>
> for n <- input, into: %{}, do: {n.name, n.age}
>
>
> 2017-10-03 23:23 GMT-04:00 Myron Marston <[email protected]
> <javascript:>>:
>
>> Map.new/2 is quite useful and I use it frequently. Whenever I use it, I
>> expect that my transform function returns unique keys, so that
>> Enum.count(input)
>> == map_size(output) holds true. Unfortunately, this hasn’t always held
>> true for the data I’m working with, and it results in an output map that’s
>> essentially “lost” some of the input data. Here’s an example:
>>
>> iex(2)> input = [%{name: "Bob", age: 37}, %{name: "Jill", age: 24}, %{name:
>> "Bob", age: 0}]
>> [%{age: 37, name: "Bob"}, %{age: 24, name: "Jill"}, %{age: 0, name: "Bob"}]
>> iex(3)> Map.new(input, &{&1.name, &1.age})
>> %{"Bob" => 0, "Jill" => 24}
>>
>> Here the %{name: "Bob", age: 37} data has been lost because it was
>> overwritten by %{name: "Bob", age: 0} since maps obviously can’t have
>> duplicates. In this situation, it would be really nice if Map.new/2 did
>> not silently ignore the fact that I gave it duplicate keys. I’d like a
>> strict from of Map.new/2 that raises an error when your transform
>> function returns duplicate keys, to notify you that you aren’t using it
>> properly.
>>
>> If we add this, I’m not sure if it can just be a change to Map.new/2
>> (would that be a breaking change for some users?) or if we’d need to add an
>> alternate function like Map.new!/2 or Map.strict_new/2 or something.
>>
>> Thoughts?
>>
>> Myron
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "elixir-lang-core" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/elixir-lang-core/5e15315d-b6c3-4fa7-b31f-a2d7c5576941%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/elixir-lang-core/5e15315d-b6c3-4fa7-b31f-a2d7c5576941%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Pedro Henrique de Souza Medeiros
> ----------------------------------
> Cel: +55 (21) 9914-86898
> Email: [email protected] <javascript:>
>
> Beautiful is better than ugly,
> Explicit is better than implicit,
> Simple is better than complex,
> Complex is better than complicated.
>
> The Zen of Python, by Tim Peters
>
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/dd49f8a6-43f3-4ce9-bc75-e89dba65b887%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.