develooper Front page | perl.perl6.language.data | Postings from September 2000

RFC 201 (v1) hash slicing

From:
Perl6 RFC Librarian
Date:
September 7, 2000 21:57
Subject:
RFC 201 (v1) hash slicing
Message ID:
20000908045746.22834.qmail@tmtowtdi.perl.org
This and other RFCs are available on the web at
  http://dev.perl.org/rfc/


=head1 TITLE

hash slicing

=head1 VERSION

  Maintainer: David Nicol <perl6rfc@davidnicol.com>
  Date: 7 September 2000
  Mailing List: perl6-language-data@perl.org
  Number: 201
  Version: 1
  Status: Developing

=head1 ABSTRACT

a more concise syntax for producing subsets of hashes.

=head1 DESCRIPTION

=head2 details

Instead of

	%subhash = map { f($_) ? ($_, $hash{$_}) : () } keys %hash;	# lengthy

one may now write

	%subhash = %hash{f($_)};	# code block f($_) will be evaluated for Truth over all the keys

=head2 syntactically identifiable degenerate cases

Additionally, these special cases are recognized:

	%subhash2 = %hash{@a};		# %subhash2 = map {($_, $hash{$_})} @a
	%subhash3 = %hash{$s};		# $subhash3{$s} = $hash{$2}

Otherwise, C<%subhash2> and C<%subhash3> would become either empty or simple
copies, depending on the Truth of C<@a> and C<$s>, which would be misleading.

=head2 the function may be evaluated "with" the hash as well as "with" the key

In addition to the default scalar C<$_> being set to each key in the hash being
sliced, the "default hash" C<%_> may also be lexically aliased to the hash
being sliced.  In the event that a "with" keyword is adopted which has a
different effect, that effect will occur so that the element selection function
is evaluated for truth for each key, with the hash.

this means that

	%subhash = map { $hash{$_} =~ /string/ ? ($_, $hash{$_}) : () } keys %hash;

can be generalized to

	%subhash = %hash{ $_{$_} =~ /string/ };

which is more general. All on the same line it looks unnecessary, but if the
filtering function was a big hairy monster and you wanted to create a dozen
subhashes all with different names it would same a lot of copying.

	%subhashA = %hashA{ Criteria };
	%subhashB = %hashB{ Criteria };
	%subhashC = %hashC{ Criteria };
	%subhashD = %hashD{ Criteria };

=head2 reasons

When I suggested a syntax extension that used 

	%hash_name{something in here}

it was roundly misread as a data access.  Adoption of this proposal will fill
the curious apparent hole left by the validity of the other slicing operations
in what I hope is a reasonable way.


=head1 IMPLEMENTATION

The rewrites suggested above suffice as a reference implementation;
optimizations may be possible due to disappearance of various intermediate
temporary storages.

a framework for providing visibility of the magic C<$_> and C<%_> inside the
filtering functions (in threaded environments) will be required.

=head1 REFERENCES

http://www.tmcm.com/tmweb/openingpage/tm6.gif

http://tmcm.com/tmweb/comics/comics111_121/I20_freecoffee.gif





nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About