RFC 201 (v1) hash slicing

[email protected] (Perl6 RFC Librarian)
Newsgroups perl.perl6.announce,perl.perl6.language.data
Message-ID <[email protected]>
This and other RFCs are available on the web at
  http://dev.perl.org/rfc/


=head1 TITLE

hash slicing

=head1 VERSION

  Maintainer: David Nicol <[email protected]>
  Date: 7 September 2000
  Mailing List: [email protected]
  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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.