Array Slice Ranges

Fredrik Olsson peylow at gmail.com
Thu Nov 9 12:48:00 PST 2006


%u skrev:
> 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

My €0.02:

0..5 should mean 0,1,2,3,4.
Because it does not break existing code.

0...5 should be 0,1,2,3,4,5.
All other suggestion I have seen looks contrived to me. .. and ... look 
obviously connected somehow, and logically "... takes it one step 
further than ..". Perfect or not, I have not seen any suggestion that 
beats it.


I also like to yet again say that having ranges as a type that can 
easily be based around would be neat.

bool checkAge(int age, int min, int max) { ... }
auto ok = checkAge(bar, 18, 25);
or
bool checkAge(int age, int$ range) { ... }
auto ok = checkAge(bar, 18...25);

Well how to denote a range type, is something I have not figured out 
yet. So I went for a $ suffix in the example.


As an extension, sets is just as useful!

int<> myFriendsAges = <18, 25, 21>;
bool foo = allAgesInRangeOver21(myFriendsAges);


And imagine slicing an array with a set!
Person[] primePersons = allPersons[<2,3,5,7,11>];


// Fredrik Olsson



More information about the Digitalmars-d mailing list