Slices

8 views
Skip to first unread message

Matt Diephouse

unread,
Mar 20, 2005, 11:45:41 PM3/20/05
to Perl6 Language List
Is it possible to assign to an array slice?

@array[0..4] = ( 0..4 ); # splice @array, 0, 5, 0..4

If so (and I'm hoping it is), is there an equivalent of Ruby's `[]=`
method? (Is there a way to define this behavior within my own
array-like classes?)

Can I use slice notation when dealing with strings?

say $string[-1]; # say substr($string, -1);
$string[0..2] = "Hello";

I know some people find strings-as-arrays too much like C, but I think
it can be convenient. (And I happen to think `splice` and `substr` are
really ugly.) I'm close to suggesting that we don't need `splice` and
`substr` at all, but there are times when it's more convenient to
specify with a starting point and a length than with a range.

--
matt diephouse
http://matt.diephouse.com

Rod Adams

unread,
Mar 21, 2005, 12:08:49 AM3/21/05
to Perl6 Language List
Matt Diephouse wrote:

>Is it possible to assign to an array slice?
>
> @array[0..4] = ( 0..4 ); # splice @array, 0, 5, 0..4
>
>If so (and I'm hoping it is), is there an equivalent of Ruby's `[]=`
>method? (Is there a way to define this behavior within my own
>array-like classes?)
>
>

I assign to array and hash slices constantly in Perl 5. I would be
shocked if Perl 6 did not support doing the same. One of the things I
point to non-Perl people about why I like Perl over other languages is a
statement like (p5) C< @b{keys %a} = values %a; >, and then explain what
it does, and how it's different from C< %b = %a >.

As for defining your own, that falls on the as yet unwritten S14
concerning the Perl 6 version of "tie". At a minimum, I think you'd
need to say something like

multi sub postcircumflex::<[ ]>(MyArray $obj : *@indices) is rw {...}

but I'll wait for S14 before speculating further.

>Can I use slice notation when dealing with strings?
>
> say $string[-1]; # say substr($string, -1);
> $string[0..2] = "Hello";
>
>I know some people find strings-as-arrays too much like C, but I think
>it can be convenient. (And I happen to think `splice` and `substr` are
>really ugly.) I'm close to suggesting that we don't need `splice` and
>`substr` at all, but there are times when it's more convenient to
>specify with a starting point and a length than with a range.
>

At present, I'd say the answer is "no", but generating a tied Array of
Chars or Ints to it will be very easy to do.

I don't think you can get rid of C<splice> and C<substr> that easily.
Besides being really handy to have around, they also have the ability to
delete and insert things of arbitrary length from the middle. And that's
not something you can do with slices, unless you start stiching the
prefix, replacement, and suffix slices on your own, which requires a lot
of copying that could be skipped.

-- Rod Adams


Brent 'Dax' Royal-Gordon

unread,
Mar 21, 2005, 12:39:21 AM3/21/05
to Luke Palmer, ma...@diephouse.com, Perl6 Language List
Luke Palmer <lu...@luqui.org> wrote:
> > Can I use slice notation when dealing with strings?
> >
> > say $string[-1]; # say substr($string, -1);
> > $string[0..2] = "Hello";
>
> No. I'm pretty sure that's the Right Thing, too. First, the "sixth
> element" in a string depends on how you're defining "element": byte,
> codepoint, grapheme, etc. Second, if you think you're using an array,
> and it's actually a string (even ""), then you'll be missing an error
> (though I understand that this is a weak argument). Third, we already
> have substr.

A while back, I think I suggested that .{bytes,points,graphs,chars} be
defined to return an array with the contents of the string split into
the indicated pieces. If this was a tied array which updated the
underlying string, would we get the OP's behavior for free?

say $string.chars[-1];
$string.points.splice(0, 2, "Hello"); # Or "Hello".points

--
Brent 'Dax' Royal-Gordon <br...@brentdax.com>
Perl and Parrot hacker

"I used to have a life, but I liked mail-reading so much better."

Luke Palmer

unread,
Mar 21, 2005, 12:09:01 AM3/21/05
to ma...@diephouse.com, Perl6 Language List
Matt Diephouse writes:
> Is it possible to assign to an array slice?
>
> @array[0..4] = ( 0..4 ); # splice @array, 0, 5, 0..4

Of course. You could in Perl 5, right?

> If so (and I'm hoping it is), is there an equivalent of Ruby's `[]=`
> method? (Is there a way to define this behavior within my own
> array-like classes?)

Well, when you really get down to it, []= would be implementing the []
method to return something tied. A lot of things do that. We'll likely
provide roles to make doing that not as prohibitive conceptually.

> Can I use slice notation when dealing with strings?
>
> say $string[-1]; # say substr($string, -1);
> $string[0..2] = "Hello";

No. I'm pretty sure that's the Right Thing, too. First, the "sixth


element" in a string depends on how you're defining "element": byte,
codepoint, grapheme, etc. Second, if you think you're using an array,
and it's actually a string (even ""), then you'll be missing an error
(though I understand that this is a weak argument). Third, we already
have substr.

Perhaps as an alternative to substr, Str could provide a slice method:

$string.slice(0..2) = "Hello";

Which could massage everything to work as if it were an array of chars,
whatever the heck chars are.

Luke

Thomas Sandlaß

unread,
Mar 22, 2005, 11:18:44 AM3/22/05
to Perl6 Language List
Rod Adams wrote:
> multi sub postcircumflex::<[ ]>(MyArray $obj : *@indices) is rw {...}
>
> but I'll wait for S14 before speculating further.

Will that ever be written? And if yes, will it be like S13 which
is basically saying that overloading is subsumed by A12/S12?
I see the 'does' operator as the sole successor to tie/bless.

There are actually three tasks for 'does':

1) declarative compile time role composer
2) declarative compile time contraint imposer
3) imperative runtime on the fly class composer
(does operator 'but' do similar things on copies?)

1 and 2 are backed by the runtime dispatch and type check system.
Depending on compiler switches they also constitute the compile
time type checking.

Regards,
--
TSa (Thomas Sandlaß)

Larry Wall

unread,
Mar 24, 2005, 8:14:06 PM3/24/05
to Perl6 Language List
On Tue, Mar 22, 2005 at 05:18:44PM +0100, Thomas Sandlaß wrote:
: Rod Adams wrote:
: > multi sub postcircumflex::<[ ]>(MyArray $obj : *@indices) is rw {...}
: >
: >but I'll wait for S14 before speculating further.
:
: Will that ever be written? And if yes, will it be like S13 which
: is basically saying that overloading is subsumed by A12/S12?

We'll might put a few things into S14 eventually if they seem to be
causing confusion.

: I see the 'does' operator as the sole successor to tie/bless.

We still have a Class.bless, but it does something a bit different on
standard objects, where it now fires off all the internal allocation and
initialization routines. But yes, for adding functionality to an existing
object, 'does' (and 'but') are better ideas.

: There are actually three tasks for 'does':


:
: 1) declarative compile time role composer

When applied to a class definition.

: 2) declarative compile time contraint imposer

When applied to a variable or parameter.

: 3) imperative runtime on the fly class composer

When applied to an object (or class?). In its imperative it always
creates an anonymous class via mixin, rather than trying to level
everything as 1 does.

: (does operator 'but' do similar things on copies?)

That seems to be the effect of it.

: 1 and 2 are backed by the runtime dispatch and type check system.


: Depending on compiler switches they also constitute the compile
: time type checking.

You can theoretically choose your level of pain in Perl 6. To what extent
we can make that work out in practice remains to be seen.

Larry

Reply all
Reply to author
Forward
0 new messages