delegate object instead of a literal
Joel Christensen
joelcnz at gmail.com
Sun Aug 14 22:21:00 PDT 2011
On 15-Aug-11 2:55 PM, Jonathan M Davis wrote:
> On Sunday, August 14, 2011 20:50:03 Joel Christensen wrote:
>> Hi,
>>
>> This program loops through a string until it finds a number and gives
>> the position of it.
>>
>> The first assert works, but not the second one.
>>
>> import std.algorithm;
>>
>> void main() {
>> static bool isNumber( char input, char dummy ) {
>> if ( ( input>= '0'&& input<= '9' ) || input == '.' )
>> return true;
>> else
>> return false;
>> }
>>
>> string str = "abc123";
>> assert( countUntil!( ( input, b ) {
>> if ( ( input>= '0'&& input<= '9' ) || input == '.' ) return true;
>> else return false;
>> } )(str, 0) == 3 ); // works
>> assert( countUntil!( isNumber, string, string )( str, 0 ) == 3 );
>> }
>>
>> This is the error:
>> parse.d(15): Error: template instance
>> countUntil!(isNumber,string,string) does not match template declaration
>> countUntil(alias pred = "a == b",R1,R2) if
>> (is(typeof(startsWith!(pred)(haystack,needle))))
>
> By the way, it looks like std.algorithm.count will do what you want to do
> (though you'll have to negate the predicate for it to work - either with
> std.functional.not or by changing it appropriately).
>
> - Jonathan M Davis
Ok, cool.
I think this is nice:
immutable isNum = `a >= '0' && a <= '9' && a != '"'`;
auto input = "abc123";
auto indexEnd = count!( not!isNum )( input );
assert( indexEnd == 3 );
- Joel
More information about the Digitalmars-d-learn
mailing list