[erlang-questions] map elements in defined order

Ulf Wiger ulf@REDACTED
Thu Oct 26 21:13:07 CEST 2017


But again, Jesper, just about everyone relies on the fact that maps follow
the general principle that there is a well-defined term comparison order.
Otherwise, maps would be highly unsuitable to use in keys, and generally
treacherous to use as a replacement for records. Following the Principle of
Least Surprise, it's a darned good thing that Erlang doesn't randomize the
key order in its maps.

I doubt that anyone would abuse an extra function that produces the map
element pairs in the internally defined sort order, given that the
documentation would clearly state that it's more expensive than
maps:to_list/1 (though likely faster than lists:sort(maps:to_list(M), fun
custom_sort_fun/2) - not to mention less error-prone.)

But it's not a feature I'm willing to go to war over. If no one else sees a
use for it, I'm willing to concede that it has low priority. :)

BR,
Ulf W

2017-10-26 20:31 GMT+02:00 Jesper Louis Andersen <
jesper.louis.andersen@REDACTED>:

> It is generally a bad idea to rely on order in anything which uses a hash
> function. The hash function is often subject to change---rather quickly I
> might add if it proves to be a security bug. Picking a family of hashes and
> seeding it randomly is usually a good trick.
>
> Our "sister language" Go *randomizes* iteration order on its maps. This is
> to force programmers into not relying on the map order at all, even if it
> happens to be ordered right now. This opens up implementations in the
> future.
>
> If you wanted order in a map, it would be *far* better if you could create
> a map based on RB-trees or the like. Those are naturally ordered by
> structure. OCaml, for instance, defines Hash Tables as well as Maps. The
> latter is the ordered variant.
>
>
> On Thu, Oct 26, 2017 at 8:07 PM Ulf Wiger <ulf@REDACTED> wrote:
>
>> But they *are* ordered. Otherwise, comparison of two maps would be
>> undefined.
>>
>> BR,
>> Ulf W
>>
>>
>> Den 26 okt. 2017 18:20 skrev "Roger Lipscombe" <roger@REDACTED>:
>>
>> On 26 October 2017 at 16:30, Ulf Wiger <ulf@REDACTED> wrote:
>> > Wouldn't it be reasonable to have such a function?
>>
>> First thought: No, because people would start abusing it. Maps *aren't*
>> ordered.
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171026/4eb8305d/attachment.htm>


More information about the erlang-questions mailing list