handful and interval

Alex Rønne Petersen alex at lycus.org
Sun Sep 2 07:43:14 PDT 2012


On 02-09-2012 16:22, 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?

Yes! I have wanted this so many times in my code. I think std.algorithm 
is a reasonable place.

Though, maybe just call the function set(). It won't collide with 
anything else anyway (like a Set in std.container or whatever).

Also, please avoid backing it with an AA (I don't know if you wanted to 
do that, but just saying). While it would make the code significantly 
simpler, it means involving the GC, which is undesirable for such a 
simple utility function.

(I still think allowing 'in' on arrays would be a better approach, but 
this is better than nothing!)

>
> 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 don't deal much in this sort of code, but I can see it being useful 
(it's certainly prettier than writing the checks out manually). I don't 
really know what else I would expect from such a type, though.

>
>
> Destroy,
>
> Andrei

-- 
Alex Rønne Petersen
alex at lycus.org
http://lycus.org


More information about the Digitalmars-d mailing list