At 3:24 AM -0800 11/18/06, Jonathan Lang wrote:
Jonathan Lang wrote:
Larry Wall wrote:
> it seems to me that .keys.sort is
> suboptimal if sort has to second-guess the ordering provided by the
underlying hash.
Not only is it suboptimal, it might not be possible. Sorting depends
on cmp returning Order::Increase, Order::Same, or Order::Decrease in
every case; but what if you're comparing Color::Blue to Bool::True?
And what about classes that involve partial ordering, such as sets?
Heck, how do you '[cmp] Color::Blue, Color::Gray', and what does
"sqrt(-1) cmp 0" (or even "sqrt(-1) <=> 0") return?
OK: IIRC, this original definition also preceded Sets. So instead of
"$_.keys.sort »===« $x.keys.sort", perhaps this should simply be
"Set($_.keys) === Set($x.keys)", corrected for proper syntax. Heck,
perhaps "$_.keys" and "$x.keys" should _be_ Sets.
I seem to remember having this discussion months
ago when trying to implement Set::Relation.
Absolutely .keys should return a Set.
We *know* already that all keys in a keyed
collection are unique, so why not explicitly
stamp them as such from the start by returning
them in a Set container; then users of those keys
don't have to recheck them for uniqueness, such
as in a Set's constructor, if they want to use
them as a set.
Then we can reliably and tersely say "$_.keys ===
$x.keys" and it will do the right thing.
Similarly, we can do set operations with the keys
more tersely, such as "$_.keys subset $x.keys" or
"$_.keys superset $x.keys" or "$_.keys union
$x.keys" or "$_.keys intersection $x.keys" or
"$_.keys minus $x.keys" etc.
And you can compare the keysets of 2 collections
reliably even when keys are objects of a data
type that is *not* ordinal, which makes it more
general.
Finally, common operations like .keys.sort (which
returns a Seq or List) can be written just as
tersely or the same as before, so that syntax can
be kept.
On a tangential matter, if you follow my
suggestion of another email and actually add a
(immutable) Bag type to the language, which your
documentation already references as a common
thing people may use, then .values can return
that, since it is an unordered collection that
may have duplicates. Once again, you can then
compare the value lists of 2 Hashes set without
sorting them. Still, regardless of what you do
here, making .keys return a Set should be done.
-- Darren Duncan