delegate object instead of a literal
Jonathan M Davis
jmdavisProg at gmx.com
Sun Aug 14 19:55:33 PDT 2011
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
More information about the Digitalmars-d-learn
mailing list