Since we're going to need these, and I'm in a documenting and defining mood (yes, I am making a final decision on strings today. Whee!) I figure we need to tackle them.

First, slices. Perl's got 'em, Python has them, Ruby, interestingly, doesn't. (Sort of) A slice is a subset of elements in an aggregate. They don't have to be contiguous, unique, or in any order. As an example:

    @foo = ('A', 'B', 'C', 'D');
    @bar = @foo[0,2]; # A slice--elements 0 and 2

@bar is now ('A', 'C'). Or:

    @bar = @foo[0,0,0,0,0,0];

@bar is now ('A',  'A',  'A',  'A',  'A',  'A').

I think for this to work we need to add a slice vtable entry. Not because I'm particularly fond of vtable entries as such, but it's a pretty fundamental operation. (Python devotes opcodes to it even)

The slice vtable entry should take as its parameter a slice pmc. This should be an array of typed from/to values, so we can do something like:

    @foo[0..2,4..8,12..];

with three entries in the slice array--one with a from/to of 0/2, one with 4/8, and one with 12/inf. Typed since these will be used with hashes, and we'll need to differentiate between something that should be taken as a string and something taken as an integer. (If the range ends are PMCs, since they may behave differently depending on which way they're read)

This vtable entry should return an iterator, which is why they're here--not because I've any particular love of the things, but because if someone does:

    @foo = @bar[0..];

on an array that generates data randomly we'll get caught in an infinite loop, which is generally a bad thing.

Since we're working on iterators, all aggregates should be able to generate them, which leads to the iterator vtable entry (since everyone wants to iterate over everything).

So, the proposal:

*) We add a slice vtable entry which takes a slice pmc and returns an interator
*) We add an iterator vtable entry which returns an interator for the PMC
*) We consider ways to make slices. I can see ops, or I can see basic functions. Either is fine, depends on how often the things are used. (Ops have less overhead, functions mean fewer ops)


Please, let discussion ensue. We'll decide on the slice creation method in a day or two and then just make it all happen.
--
Dan


--------------------------------------it's like this-------------------
Dan Sugalski                          even samurai
[EMAIL PROTECTED]                         have teddy bears and even
                                      teddy bears get drunk

Reply via email to