View mode: basic / threaded / horizontal-split · Log in · Help
September 02, 2012
Re: handful and interval
On 9/2/12 5:05 PM, 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 first could be make to work, the second I am against on a big-oh 
complexity basis. The problem with making the first work is inconsistency.

Andrei
September 02, 2012
Re: handful and interval
On 9/2/12 4:56 PM, Jacob Carlborg wrote:
> 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.

Iota is a different notion (it has a step).

Andrei
September 02, 2012
Re: handful and interval
On 9/2/12 4:51 PM, Jacob Carlborg wrote:
> 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.

The difference is in scale and primitives offered. Handful would be 
read-only and limited to the size of its initializer. Its representation 
would take advantage of that.

Anyhow, I think among would be a simpler solution for this all.


Andrei
September 02, 2012
Re: handful and interval
On 02-Sep-12 18: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 wouldn't question utility but rather the implementation of the above.
One thing I'd love to see is thing like
handful!("struct", "class", "union")

that does pre-process contents at compile time to speed up search.
In other words it's possible to not only come close to  a series of a == 
"struct" || a == "class" || a == "union" but surpass it.

At the very least handful!(a,b,c) can take expression tuple and make
x in handful!(a,b,c) lower to:
return x == a || x == b || x == c;

even for run-time variables a, b & c.

-- 
Olshansky Dmitry
September 02, 2012
Re: handful and interval
On Sunday, 2 September 2012 at 14:49:34 UTC, 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

I prefer this.

There doesn't seem to be a clear vision for the 'in' operator. 
Let's not make the situation more complicated before we've 
figured that out. How do we know which uses of 'in' are abuse, 
and which are merely exploiting intuition justifiably?
September 02, 2012
Re: handful and interval
On 9/2/12 6:22 PM, Jakob Ovrum wrote:
> On Sunday, 2 September 2012 at 14:49:34 UTC, 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
>
> I prefer this.

Me too, upon some more thought. (Interval could become a legit numeric 
library, but that's a separate idea.)

> There doesn't seem to be a clear vision for the 'in' operator. Let's not
> make the situation more complicated before we've figured that out. How
> do we know which uses of 'in' are abuse, and which are merely exploiting
> intuition justifiably?

Yah, agreed.


Andrei
September 02, 2012
Re: handful and interval
On 9/2/12 6:44 PM, Andrei Alexandrescu wrote:
[snip]

The remaining question is where to put among and between. std.functional?

Andrei
September 02, 2012
Re: handful and interval
Andrei Alexandrescu:

I remember naming the first thing bunch().

> if (x in interval(1, 2))
> {
>     ...
> }

Is interval() for time intervals too? :-)

Bye,
bearophile
September 02, 2012
Re: handful and interval
On 09/02/2012 06:45 PM, Andrei Alexandrescu wrote:
> On 9/2/12 6:44 PM, Andrei Alexandrescu wrote:
> [snip]
>
> The remaining question is where to put among and between. std.functional?
>
> Andrei

They are not combinators. std.algorithm.
September 02, 2012
Re: handful and interval
On Sun, Sep 2, 2012 at 5:29 PM, Dmitry Olshansky <dmitry.olsh@gmail.com> wrote:

> I wouldn't question utility but rather the implementation of the above.
> One thing I'd love to see is thing like
> handful!("struct", "class", "union")
>
> that does pre-process contents at compile time to speed up search.
> In other words it's possible to not only come close to  a series of a ==
> "struct" || a == "class" || a == "union" but surpass it.

Dmitry, don't you have a trie implementation since your GSOC project?

In any case, I'd gladly welcome such a function. I just spent hours
trying to find an quick way to search for a string among a handful
(set, group, whatever) of strings known at CT, that works at
compile-time and is swift at runtime. Having that in Phobos would be
very nice.
For strings, some ordering and prefix factorisation can probably work,
but what Andrei proposes is more general I presume?
1 2 3 4 5 6
Top | Discussion index | About this forum | D home