Array Slice Ranges

rm roel.mathys at gmail.com
Thu Nov 9 13:12:59 PST 2006


rm wrote:
> %u wrote:
>> I'm learning ruby right now, and I noticed they use a very cool syntax 
>> for ranges.
>>
>> 0..5 means 0, 1, 2, 3, 4, 5
>> 0...5 means 0, 1, 2, 3, 4
>>
>> The current array slicing is useful half the time and a pain in the 
>> arse the
>> other half, so I was wondering if anyone else has mentioned this idea 
>> for D
>> before...
>>
>> - Trevor
> 
> in python you have a slice object, which you can pass like you would 
> pass a range, works beautifully, is more powerful and in my eyes less 
> error prone than .. or ... and I think ruby must change it <g> because 
> ... has one point more than .. but one element less ... (these latest 3 
> points indicate I could go on and on ... :-) )
> 
> roel

hmmm, replying to my own messages :-)
slicing in python:

 >>> str = "hello world from roel"
 >>> s = slice(3,10,2) # start,stop(excluding)[,step]
 >>> str[s]
'l ol'
 >>> s = slice(1,12,2)
 >>> str[s]
'el ol '
 >>> s = slice(1,14,2)
 >>> str[s]
'el ol r'

so it's just a type (and yes python is a typed language :-) )
I guess it's possible to create a type Slice in D and overload the 
opIndex operator of another type to accept a Slice type as argument.





More information about the Digitalmars-d mailing list