develooper Front page | perl.perl6.language | Postings from December 2008

Proposal: Make @a and @@a different variables

Thread Next
Daniel Ruoso
December 16, 2008 05:34
Proposal: Make @a and @@a different variables
Message ID:

One of the hardest features in Perl 6 is the slice context. It is
undoubtfully usefull, since it provides semantics to acces each
iteration of a map, for instance.

But there's one thing in the spec that makes not only slices, but the
lists themselves considerably harder to implement, and that is the idea
that a slice is a view of a list.

  my @@a = map { $^a == 2 ?? 1,2,3 !! () }, 1, 2, 3;
  say @a[0]; # this should print 1.
  say @@a[0]; # this should print ().
  say @a[1]; # this should print 2.
  say @@a[1;0]; # this should print 1.

That happens because '@a' and '@@a' are the same variable, just
different views of that variable.

That being said, we should note that this example looks simple because
we have almost no lazyness implied (since there's an assignment in the
first line), every list access requires the evaluation of the flatenning
of the list.

  my @@a = ((),(1,2,3),());
  # the following will require a flatenning to get the actual index
  say @a[3];

This looks much more complicated when you have a lazy list...

  my @a <== map { $^a == 2 ?? 1,2,3 !! () }, 1, 2, 3;
  # the line below will require two map iterations to get a value,
  # and at that time, there will be three values available
  say @a[0];
  # having to support a slice view here will require the lazy list
  # to always be a lazy slice, and each access will require a flatenning
  # of the values already obtained to only then decide if it has to
  # obtain more items
  say @a[2];

This all makes me think that it would be much easier if the slice
existed on its own, which would mean:

  # this will provide only a flatenned view
  my @a <== map { $^a == 2 ?? 1,2,3 !! () }, 1, 2, 3;
  # and this will provide a slice view
  my @@a <== map { $^a == 2 ?? 1,2,3 !! () }, 1, 2, 3;
  # but @a and @@a are different values
  @a =!= @@a :test;
  # and trying to get the slice on @a will fail
  say @a[0;1]; # fail
  # and you still can flatten the slice if you want to...
  # and it can preserve the lazyness
  my @b <== @@a;
  # or enforce a mostly eager level
  my @c = @@a;

So, what do you think?


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About