Community

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?
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))
{
...
}
Destroy,
Andrei

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@lycus.org
http://lycus.org

On 2012-09-02 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?
I really don't like the name "handful". What would be the difference
compared to a regular set container? To me it sounds like we should have
a standard set container in Phobos, std.container.set.
--
/Jacob Carlborg

On 2012-09-02 16:22, Andrei Alexandrescu wrote:
> 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))
> {
> ...
> }
Isn't this a bit like std.range.iota? Perhaps extending that returned
value with opIn and other functionality.
--
/Jacob Carlborg

On 9/2/12 4:50 PM, David Nadlinger wrote:
> On Sunday, 2 September 2012 at 14:42:43 UTC, Alex Rønne Petersen wrote:
>> On 02-09-2012 16:22, Andrei Alexandrescu wrote:
>>> 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.
>
> Hm, maybe assert(interval(1, 3) & interval(2, 4) == interval(2, 3)) and
> similar operations?
Thought about sugar for set operations using arithmetic operators, but
decided against, because that would create a lot of confusion with
http://en.wikipedia.org/wiki/Interval_arithmetic.
Andrei

Le 02/09/2012 16:51, Jacob Carlborg a écrit :
> On 2012-09-02 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?
>
> I really don't like the name "handful". What would be the difference
> compared to a regular set container? To me it sounds like we should have
> a standard set container in Phobos, std.container.set.
>
+1, and we are back to the allocator design.