Jump to page: 1 27  
Page
Thread overview
handful and interval
Sep 02, 2012
David Nadlinger
Sep 02, 2012
Tyro[17]
Sep 03, 2012
Jacob Carlborg
Sep 02, 2012
Jakob Ovrum
Sep 02, 2012
Timon Gehr
Sep 02, 2012
Jonathan M Davis
Sep 03, 2012
Timon Gehr
Sep 02, 2012
SomeDude
Sep 02, 2012
Walter Bright
Sep 02, 2012
Simen Kjaeraas
Sep 03, 2012
Walter Bright
Sep 03, 2012
Jonathan M Davis
Sep 03, 2012
Sven Torvinger
Sep 03, 2012
David Piepgrass
Sep 03, 2012
David Piepgrass
Sep 03, 2012
sclytrack
Sep 02, 2012
Jacob Carlborg
Sep 02, 2012
deadalnix
Sep 02, 2012
SomeDude
Sep 03, 2012
Don Clugston
Sep 03, 2012
Jacob Carlborg
Sep 02, 2012
Jacob Carlborg
Sep 03, 2012
Jacob Carlborg
Sep 03, 2012
Simen Kjaeraas
Sep 02, 2012
Dmitry Olshansky
Sep 02, 2012
Philippe Sigaud
Sep 03, 2012
Jacob Carlborg
Sep 03, 2012
Simen Kjaeraas
Sep 03, 2012
Philippe Sigaud
Sep 03, 2012
Sven Torvinger
Sep 03, 2012
Sven Torvinger
Sep 03, 2012
David Nadlinger
Sep 03, 2012
David Nadlinger
Sep 03, 2012
David Nadlinger
Sep 03, 2012
Timon Gehr
Sep 03, 2012
Jonathan M Davis
Sep 03, 2012
Timon Gehr
Sep 03, 2012
David Nadlinger
Sep 03, 2012
bearophile
Sep 03, 2012
Timon Gehr
Sep 02, 2012
bearophile
Sep 02, 2012
Jonathan M Davis
Sep 02, 2012
Mehrdad
Sep 02, 2012
Jonathan M Davis
Sep 03, 2012
ixid
Sep 03, 2012
David Nadlinger
Sep 03, 2012
Jonathan M Davis
September 02, 2012
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
September 02, 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@lycus.org
http://lycus.org
September 02, 2012
On 9/2/12 4:22 PM, Andrei Alexandrescu wrote:
[snip]

The alternative would be to simply define these as functions:

if (a.among("struct", "class", "union")) { ... }
if (b.between(1, 100)) { ... }


Andrei
September 02, 2012
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?

David
September 02, 2012
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
September 02, 2012
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
September 02, 2012
On 9/2/12 11:45 PM, Andrei Alexandrescu wrote:
> On 9/2/12 4:22 PM, Andrei Alexandrescu wrote:
> [snip]
>
> The alternative would be to simply define these as functions:
>
> if (a.among("struct", "class", "union")) { ... }
> if (b.between(1, 100)) { ... }
>
>
> Andrei

if (a in ["struct", "class", "union"]) { ... }
if (a in someArrayVar) { ... }

works wonders for me. Why not? Somehow I cannot see the disadvantages. Please educate.

Andrew
September 02, 2012
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
September 02, 2012
On 02-09-2012 17:05, Tyro[17] wrote:
> On 9/2/12 11:45 PM, Andrei Alexandrescu wrote:
>> On 9/2/12 4:22 PM, Andrei Alexandrescu wrote:
>> [snip]
>>
>> The alternative would be to simply define these as functions:
>>
>> if (a.among("struct", "class", "union")) { ... }
>> if (b.between(1, 100)) { ... }
>>
>>
>> Andrei
>
> if (a in ["struct", "class", "union"]) { ... }
> if (a in someArrayVar) { ... }
>
> works wonders for me. Why not? Somehow I cannot see the disadvantages.
> Please educate.
>
> Andrew

The argument seems to be that since an array search is O(n) and an AA lookup is O(1), this example would be "misleading".

Not that I think this matters at all in practice.

-- 
Alex Rønne Petersen
alex@lycus.org
http://lycus.org
September 02, 2012
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.
« First   ‹ Prev
1 2 3 4 5 6 7