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

117 (v1): Perl syntax support for ranges

From:
Casey R. Tweten
Date:
August 24, 2000 09:54
Subject:
117 (v1): Perl syntax support for ranges
Message ID:
Pine.OSF.4.21.0008241246140.24565-100000@home.kiski.net
=head1 TITLE

Perl syntax support for ranges

=head1 VERSION

  Maintainer: pdl-porters team <pdl-porters@jach.hawaii.edu>
  Date: 16 August 2000
  Version: 1
  Mailing List: perl6-language@perl.org
  Number: 117

=head1 ABSTRACT

This RFC proposes syntactic support for ranges. Range objects
would be especially useful to specify indexing ranges of compact
numerical arrays (currently implemented by PDL) in a concise
manner, e.g.

  $y = $x->slice(0:$n-1:4);
  
Note that currently (perl5) we have to say

  $n1 = $n-1;  # since we need to stringify
  $y = $x->slice("0:$n1:4");

This should be contrasted with the less cluttered syntax offered
by numerical Python and commercial systems such as Matlab and IDL:

  y = x[0:n-1:4]; 
  
In perl we desire to say:

 $y = $x[0:$n-1,4];  
 
or even 

 @y = @x[0:$n-1,4]; 
 
If there is a more general unification of PDL arrays and normal
perl arrays. (See L<RFCnumerical>).

This RFC proposes ranges as part of the syntax cleanup,
L<RFCdefault> proposes a default method for objects. This RFC is
closely linked to RFC 81: C<Lazily evaluated list generation functions>
(and proposes a subset of the features of that RFC) but emphasizes
two additional aspects: Motivation by requirements of
PDL and the important implementation aspect of I<infinitely
lazy evaluation>.


=head1 DESCRIPTION

A range would be an object that is different from current perl5
lists but could be interpolated into one if desired. The C<:> operator
would be the range generating operator.

The behaviour would be similar to the '..' operator but generally
no explicit in-memory list would be generated. These ranges would be
very useful to concisely specify subslices into multi-dimensional
numerical arrays.

Perhaps the '..' operator can be recycled as
something else and ':' used for all ranges (or ':' simply be an alias
for '..').

=head2 Examples:

         :               # all the things on this dimension: full span
         5:-1            # 5..last
         5:-1:2          # Every second item, up to the last or second last
         -1:7:3          # Start with last item, then fourth last, etc. until 7

  for (0:7) { ... }
  for (0.1:1.0:0.1) { ... }

  $pdl->slice(-1:$n:3) .= 5;
  $pdl->slice(:,::2) *= 2;

An issue is the treatment of potentially infinite ranges of the form

  :
  :5

and similar cases. Those could raise an error if used in a context
where actual list items are generated, e.g.

   for (:) { $a *= $_ }

but be allowed in circumstances where no explicit list item is
ever created, e.g.

  $a->slice(:)

which might actually be written in perl6 as

  $a[:]


=head1 IMPLEMENTATION

Possible. A range should probably be a lazily evaluated list which
functions can choose to accept without the need to actually generate
the list in memory. Compare the RFC on lazy list generation.

A detail that is crucial from our point of view that is not mentioned
in RFC 81 is the ability for a function to inquire the C<start:stop:step>
parameters of the range and do its own thing without ever generating
a list or list iterator -- an implementation aspect we refer to as
I<infinitely lazy evaluation>.

=head1 REFERENCES

L<PDL> (http://pdl.sourceforge.net/PDLdocs)

http://pdl.perl.org

RFC 24: Semi-finite (lazy) lists

RFC 81: Lazily evaluated list generation functions


-- 
print(join(' ', qw(Casey R. Tweten)));my $sig={mail=>'crt@kiski.net',site=>
'http://home.kiski.net/~crt'};print "\n",'.'x(length($sig->{site})+6),"\n";
print map{$_.': '.$sig->{$_}."\n"}sort{$sig->{$a}cmp$sig->{$b}}keys%{$sig};
my $VERSION = '0.01'; #'patched' by Jerrad Pierce <belg4mit at MIT dot EDU>




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