Hi,

Thank you George for your answer.

On Wednesday, October 13, 2021 at 5:37:39 PM UTC+2 gneuner2 wrote:

>
> On 10/12/2021 7:01 PM, unlimitedscolobb wrote: 
> > I wrote myself this little function: 
> > 
> > (define (hash->ordered-list h) 
> >   (hash-map h cons #t)) 
> > 
> > which uses the try-order? argument of hash-map. 
> > 
> > Is there a reason for hash->list not have an optional argument 
> > try-order?  Or perhaps having such a standalone function would be 
> better? 
> > 
> > I was planning to submit a patch, but then I thought I may be missing 
> > something. 
> > 
> > - 
> > Sergiu 
> > 
>
> I can't speak for the Racket team, but ... 
>
> Hash tables entries inherently are unordered, so there really is no 
> reason to expect an ordered list from reading out the data.  Also, the 
> docs indicate that 'try-order?' doesn't work for all data types - so it 
> may produce unexpected results.  


I have two main use cases for producing an ordered list from a hash table:

1. A canonical way to pretty print a hash table: In my projects, I carry 
around and print out hash tables a lot, so I like the elements to appear in 
the same order all the time, whatever that order may be. Luckily, 
`hash-map` orders symbols alphabetically and numbers according to the 
natural order, so it's perfect for my use.

2. Testing the contents of a mutable hash table: The only way I found to 
that is to convert the hash table to an ordered list and compared it to the 
test list. This is clearly not the most efficient use of a hash table, but 
I can totally go with that, since it's about testing and not the actual 
performance.

Of course, I am totally open to learning better ways of doing these things!
 

> Further, sorting the output potentially 
> can take a lot of extra time ... having never tried to get ordered 
> output from hash-map, I can only hypothesize that (at least in some 
> cases) it may be faster to create the unordered list and then sort it 
> than to try to produce an ordered list with 'try-order?'. 
>

I scrounged around Racket sources a bit, and even though I am not sure I 
looked in the right place, I got the impression that that is what hash-map 
with try-order? set to #t does already: creating an unordered list out of 
the hash table and then `sort`ing it.

Certainly, you are welcome to submit a change, but I think it would be 
> best to leave the existing behavior and make ordered output an addition. 
>
>
FYI, here's the PR: https://github.com/racket/racket/pull/4025

I pored a lot over this, and the reasoning which won at the end was that 
`hash->list` is basically a call to `hash-map` with `cons` as the function 
to apply. Thus, adding try-order? to hash->list is a very easy change, so 
why not have it.

Just to be clear, my PR does not alter the default behavior of 
`hash->list`, and only adds an optional argument.

-
Sergiu

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" 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/racket-users/a1e0bbd2-69f8-439e-b362-306c897926d3n%40googlegroups.com.

Reply via email to