But... I don't want my delegates to be lazy - breeding advice

Andy Knowles andy.knowles at gmail.com
Wed Aug 23 07:15:18 PDT 2006


Rémy J. A. Mouëza wrote:
> Andy, is there a particular reason why you don't use the array 
> concatenation operator in your select ( and others ) function(s) ?
> Like this one :
> # ArrayT [] select ( ArrayT ) ( ArrayT [] array, bool delegate ( ArrayT 
> ) predicate )
> # {
> #     ArrayT [] result ;
> #
> #     foreach ( item ; array )
> #         if ( predicate ( item ))
> #             result ~= item ;
> #
> #     return result ;
> # }

:)

An excellent question!  Frankly, I forgot about it.  While I pay close 
attention to D's development and these news groups, I rarely get a 
chance to write D code.

It *might* be better to (over)allocate only once (as I do) rather than 
realloc the array who knows how many times.  Depends on usage really - 
wasted space on one hand, wasted cycles on the other.

For the range function, it depends how Walter's realloc code works.  I 
know it allocates more than it needs to, but I don't know what factor or 
constant it increase the size by.  Depends again on usage which is the 
better option.

I should probably just use the concat operator and leave the performance 
tuning for when it is really needed.

And of course, you can't use a predicate with an argument if you want to 
use lazy evaluation, even though it makes the code more logical.

Andy



More information about the Digitalmars-d mailing list