std.array suggestion

David Medlock noone at nowhere.com
Fri Mar 10 04:26:17 PST 2006


Oskar Linde wrote:
> David Medlock wrote:
> 
> 
>>Oskar Linde wrote:
>><snip>
> 
> 
>>I would also add:
> 
> 
> I think the following are redundant as functions as they all have an
> equivalent splice syntax:
> 
> 
>>T[]  left( T[] array, int n )
> 
> identical to array[0..n];
> 
> 
>>T[]  right( T[] array, int n )
> 
> identical to array[$-n..$];
> 
> 
>>T[]  skip( T[] array, int n )
> 
> identical to array[n..$];
> 

Yes but the last two avoid the hideous Perl-like $...hehe.
These 3 are just more readable to me, doesnt matter though.

> 
>>In my own personal parsing stuff I regularly use the above plus:
>>
>>T[]  countUntil( char[] str, bool delegate(char) pred );
>>T[]  countWhile( char[] str, bool delegate(char) pred );
> 
> 
> I assume those should return a size_t and not a T[]?
> If I understand correctly, those functions would be similar to find, but
> return arr.length instead of -1 when no matching element is found? (The
> second one would also invert the predicate.)
> 

Actually I copied the wrong utility functions.  The 
countWhile/countUntil do return size_t/uint.

The ones I meant to post are skipWhile/skipUntil which do return a slice 
of the T[].

> 
>>So skipping whitespace becomes:
>>
>>text = skip( text.countWhile( delegate bool(char c){ c<=32; } ) );
> 

should be:

text = text.skip( text.countWhile( delegate bool(char c){ c<=32; } ) );
-DavidM



More information about the Digitalmars-d mailing list