handful and interval
Jonathan M Davis
jmdavisProg at gmx.com
Sun Sep 2 15:33:39 PDT 2012
On Sunday, September 02, 2012 16:22:20 Andrei Alexandrescu wrote:
> I'd like to add a simple function to std called "handful". It would
> return a small, read-only set of which main facility is membership test:
>
> string a = "class";
> if (a in handful("struct", "class", "union"))
> {
> ...
> }
>
> Would it be generally useful, and if so what module does it belong to?
How is this different from
if(canFind(["struct", "class", "union"], a) {...}
Is it because it can be made more efficient? From a usage standpoint, they're
essentially the same.
> Same question about "interval", which is a fair amount more interesting
> if done right. An interval is a pair of values from a type that supports
> inequality comparison. It would have a variety of interval-specific
> operations, of which this would probably be quite popular:
>
> int x;
> ...
> if (x in interval(1, 2))
> {
> ...
> }
I take it that that's a closed interval (opened vs closed would potentially
complicate this a bit)? If so, then that's the same as
if(x >= 1 && x <= 2) {...}
right? That's kind of nice and kind of pointless. It's slightly less verbose,
but unless the optimizer does a lot better than it's probably going to do,
you're gonig to take a performance hit. The only real advantage there that I
see is that it's a bit more idomatic.
Though, on thinking about it, it _would_ have the advantage of allowing you to
pass around an interval, which doesn't work anywhere near as well with
separate values. And if the interval is doing a lot more than in, then that
could make it valuable as well.
But the example doesn't really do much to show interval's value IMHO.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list