Re: RFC - Hashing PMC's albie@halfarrabio.di.uminho.pt

2002-07-23 Thread [EMAIL PROTECTED]

From:  [EMAIL PROTECTED]
 If I have:
 
$a = [ 1, 2, 3 ];
$b = [ 1, 2, 3 ];
 
%foo{$a} = 'A';
%foo{$b} = 'B';
 
 Then I want C (%foo{$a} == 'A')  %foo{$b} == 'B'  to be true.

Maybe this a case of And Now For Something Completely Similar.  This
looks like something we already have in Perl 5, so the language wouldn't
need to be changed in this regard.  In Perl 5 the string ID's of the
objects, which are guaranteed globally unique during their lifetime, can be
used as hash keys.  So for the code above, your desire for C (%foo{$a} ==
'A')  %foo{$b} == 'B'  would be met.  Of course, you can't pull the
objects themselves back out of the Ckeys array, but FWIW there's a module
on CPAN that gives you that ability, so even that requirement is sorta met
in Perl 5.

 I want to be able to do C $b[0] = 3  and 
 still be able to lookup %foo{$b}

Again, looks like you want something that's already in Perl 5.  Tweak the
above code to Perl5 syntax and it Does What You Expect.

 But then sometimes you'd *want* hashing to be based
 on the content. Hmm. Assuming $b hasn't been 
 modified, how about:
 
%foo{*$a} = 'A';
%foo{*$b} = 'B';

If we can make the assumption that the need to do this type of thing is so
rare that we don't need a special language feature to do it, we can again
look to existing Perl 5 capabilities to give you what you want:
  
   %foo{join '', @a} = 'A'

 Sorry if this got a bit rambly; I'm not sure working 
 things out as you type is necessarily a good idea

Maybe I'm doing that myself.  Here's my point: the features you suggest
sound great.  However, all of those features are already available in Perl
5 in an only slightly more roundabout way.  Ergo, I submit that the
language does not need to be changed in any way to do what you suggest.

-Miko


mail2web - Check your email from the web at
http://mail2web.com/ .





Re: RFC - Hashing PMC's albie@halfarrabio.di.uminho.pt

2002-07-23 Thread Aaron Sherman

On Mon, 2002-07-22 at 04:36, [EMAIL PROTECTED] wrote:

  Now, I ask for PMC programmers to take care implementing this! Notice
  that, for example in arrays, arrays with the same length but different
  elements should return different hash codes (or try). But for the same
  elements MUST return the same hash code.
 
 Um... not necessarily. Bordering on the 'not at all'. Perl 6 will
 apparently allow one to have things other than strings as keys to
 hashes. If I have:
 
$a = [ 1, 2, 3 ];
$b = [ 1, 2, 3 ];
 
%foo{$a} = 'A';
%foo{$b} = 'B';
 
 Then I want C (%foo{$a} == 'A')  %foo{$b} == 'B'  to be true.

That's correct.

 Hmm... actually, that doesn't require a hashing algorithm which
 returns distinct values for $a and $b does it...

Also correct.

 Actually there's a whole can of worms here. Assuming I've just run
 the above code. I want to be able to do C $b[0] = 3  and still be
 able to lookup %foo{$b}, which implies that Chash should be based
 on some invariant of the PMC that's independent of its content. 

And that is also correct. Larry has discussed the idea that the string
form of the key (which would be used for hashing) would still be the old
reference notation ala Perl5, so that would not change. The thing that
does change is that Perl6 stores the *actual key* and hashes it when
required (presumably with some sort of internal caching), where Perl5
stores Cscalar(key) and does not maintain a copy of or reference to
the original.

 
 But then sometimes you'd *want* hashing to be based on the
 content. Hmm. Assuming $b hasn't been modified, how about:
 

That's fine. When you want that you could declare it as a property, and
that property could be defined in a module. There are many (infinite?)
ways that you might want to hash complex data structures, and all of the
really useful ones should be supported. But, there's no reason for that
support to be in the core language.

So, for example:

my %w is Hashed::ByLength;
my %x is Hashed::ByReference;
my %y;
%y{%w} = 'A';
%y{%x} = 'B';
my %z is Hasher::ByContent;
%z{%w} = 'C';
%z{%x} = 'D';

In this example, I posit two types of property. One describes how an
object is hashed when stored (e.g. Hashed::ByLength and
Hashed::ByReference). The other describes how a hash hashes its keys
(which would override the former because it never calls, e.g.
C%w.hashkey or whatever the method is that Perl will call to hash an
object).

This is all off the top of my head, and you could define modules that
would work a different way, but my point is that I think you can get
everything you want in Perl6 as planned (assuming that user code will be
able to override hashing, which seems to have been in the plan).





Re: RFC - Hashing PMC's albie@halfarrabio.di.uminho.pt

2002-07-23 Thread Dan Sugalski

At 9:36 AM +0100 7/22/02, [EMAIL PROTECTED] wrote:
Um... not necessarily. Bordering on the 'not at all'. Perl 6 will
apparently allow one to have things other than strings as keys to
hashes. If I have:

Yes. Hashes will take either strings or object IDs, depending on the 
hash. (The hash can choose)

Objects are required to provide a fixed id. (No sneaky changing IDs 
on the fly. Not allowed) Multiple objects may or may not be allowed 
to have the same ID--opinions differ there.
-- 
 Dan

--it's like this---
Dan Sugalski  even samurai
[EMAIL PROTECTED] have teddy bears and even
   teddy bears get drunk