handful and interval

Jonathan M Davis jmdavisProg at gmx.com
Mon Sep 3 11:53:18 PDT 2012


On Monday, September 03, 2012 20:29:27 Timon Gehr wrote:
> On 09/03/2012 08:01 PM, David Nadlinger wrote:
> > On Monday, 3 September 2012 at 14:04:18 UTC, Andrei Alexandrescu wrote:
> >>> A sufficiently smart compiler (tm) could
> >>> optimize this to a efficient string "prefix switch statement" just as
> >>> well…
> >> 
> >> I agree. But then http://c2.com/cgi/wiki?SufficientlySmartCompiler.
> > 
> > This is exactly my point: My feeling is that the increased complexity
> 
> What complexity?

He probably means that adding another function to do exactly what an existing 
function already does is complicating the library. The new function needs to 
bring enoug to the table to be worth it, and the syntactic change of allowing 
you to do

if(value in handful(value1, value2, value3) {}

instead of

if(canFind([value1, value2, value3], value)) {}

isn't worth that.

> > by introducing a second syntax resp. a new special case (i.e. among) for
> > such a simple operation is only worth it if it leaves no reason to
> > revert to a hand-written replacement for performance reason,
> 
> [ ].canFind( ) allocates on the GC heap without a sufficiently smart
> compiler.

That's then a performance issue which make may something like handful worth 
it, but Andrei seems to be arguing based on aesthetics rather than 
performance. And if you're arguing performance, I'd argue that the "in" 
solution is a bad one anyway, because it requires constructing and returning a 
struct, whereas you could just make what you're looking for the first argument. 
e.g.

if(among(value, value1, value2, value3)) {}

The syntax is less clear this way, and perhaps the syntactic convenience of 
the whole in idea outweighs the performcance cost there (particularly since it 
should still be cheaper than a heap allocation), but the only reason to argue 
against canFind that I can see is performance, and if you're arguing 
performance, then I'm not sure that the in idea is necessarily the way to go.

Regardless, the point is that there needs to be a practical reason for 
something like handful, not an aesthetic one.

- Jonathan M Davis


More information about the Digitalmars-d mailing list