Buddha Buck wrote:
>
> In a hash implementation, your hash keys -are- your set elements!
>
> my %set;
>
> # add elements to %set
> $set{'elem1','elem2'} = 1;
>
> # Compute union
> $union{keys %set1, keys %set2} = 1;
Oh, yeah, using native hashes for sets -- what could be simpler?
(Hint:
@set{'elem1','elem2'} = ();
@union{keys %set1, keys %set2} = ();
)
> # Compute intersection
> for $elem (keys %set1) { $intersect{$elem} = 1 if exists($set2{$elem});}
@intersection = grep { exists $set1{$_} } keys %set2;
And this, to me, illustrates why the pure hash syntax is not
entirely optimal for sets. Here we've computed the intersection
set of keys in one neat stroke -- and we're left with an array,
not a hash. To get it into a hash, we'd have to do something
like
%intersection = map { exists $set1{$_} ? ( $_ => 1 ) : () } keys %set2;
or the somewhat more efficient
%intersection=();
@intersection{ grep { exists $set1{$_} } keys %set2 } = ();
If it were possible to assign to the keys of a hash, we'd be
a lot closer to our ideal:
keys(%intersection) = map { exists $set1{$_} ? ( $_ => 1 ) : () } keys %set2;
but this is not currently legal perl.
--
John Porter
We're building the house of the future together.
- Re: RFC 179 (v1) More functions from set theory to manipu... Tom Christiansen
- Re: RFC 179 (v1) More functions from set theory to manipu... Michael Maraist
- Re: RFC 179 (v1) More functions from set theory to m... Tom Christiansen
- Re: RFC 179 (v1) More functions from set theory to m... Gael Pegliasco
- Re: RFC 179 (v1) More functions from set theory ... Jeremy Howard
- Re: RFC 179 (v1) More functions from set the... Gael Pegliasco
- Re: RFC 179 (v1) More functions from set... Tom Christiansen
- Re: RFC 179 (v1) More functions fro... Tom Christiansen
- Re: RFC 179 (v1) More functions... Gael Pegliasco
- Re: RFC 179 (v1) More functions... Buddha Buck
- Re: RFC 179 (v1) More functions... John Porter
- Re: RFC 179 (v1) More functions... Tom Christiansen
- proto-RFC: keys(HASH) as lvalue... John Porter
- Re: proto-RFC: keys(HASH) as lv... Tom Christiansen
- Re: proto-RFC: keys(HASH) as lv... Bart Lateur
- Re: proto-RFC: keys(HASH) as lv... John Porter
- Re: RFC 179 (v1) More functions... Johan Vromans
- Re: RFC 179 (v1) More functions... Gael Pegliasco
- Re: RFC 179 (v1) More functions... Tom Christiansen
- Re: RFC 179 (v1) More functions... Gael Pegliasco
- Re: RFC 179 (v1) More functions... Chaim Frenkel
