[erlang-questions] map elements in defined order

Benoit Chesneau bchesneau@REDACTED
Thu Oct 26 22:49:23 CEST 2017



> On 26 Oct 2017, at 20:31, Jesper Louis Andersen <jesper.louis.andersen@REDACTED> wrote:
> 
> 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 <mailto: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 <mailto:roger@REDACTED>>:
> On 26 October 2017 at 16:30, Ulf Wiger <ulf@REDACTED <mailto: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 <mailto:erlang-questions@REDACTED>
> http://erlang.org/mailman/listinfo/erlang-questions <http://erlang.org/mailman/listinfo/erlang-questions>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions


I guess if at least `maps:to_list/1` would return the keys in order it would  be already a benefit. 

Another usage of such order is when you want to sign the object to compare  with others across the network. I have such usage when I’m using maps as a representation for JSON.

- benoît
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171026/a2f06bb4/attachment.htm>


More information about the erlang-questions mailing list