December 16, 2013
On Mon, Dec 16, 2013 at 11:39:56PM +0100, Joseph Rushton Wakeling wrote:
> On 16/12/13 23:30, Andrei Alexandrescu wrote:
> >I guess if we need several versions of between, we could give up on it.
> 
> What's wrong with having it implemented analogous to std.random.uniform -- taking a bounds parameter which allows for open and/or closed at either end, with the default being "[)" ... ?
> 
> By the way, I'd also like to see that open/closed-at-either-end specialization extended to std.range.iota().

Oooh, do I hear a volunteer for cleaning up iota? ;-)

While you're at it, you might as well implement:

	https://d.puremagic.com/issues/show_bug.cgi?id=10762

:-P


T

-- 
The peace of mind---from knowing that viruses which exploit Microsoft system vulnerabilities cannot touch Linux---is priceless. -- Frustrated system administrator.
December 16, 2013
On Monday, 16 December 2013 at 21:45:40 UTC, Walter Bright wrote:
>> uint among(T, Us...)(T v, Us vals)
>> {
>>     foreach (i, U; Us)
>>     {
>>         if (v == vals[i]) return i + 1;
>>     }
>>     return 0;
>> }
>
> This has O(n) behavior, which might be unexpected for the user.

I would expect one table-lookup for this if vals contains strings, not N ifs. If you look at the example, most of them could be done with perfect hashing on a single character.

Is it possible for the compiler/template system to turn this into a switch/dictionary? Or is there something in the language/compiler that makes that impossible?

(I am not trying to be clever, I am curious)
December 16, 2013
On 12/16/13 2:38 PM, Andrej Mitrovic wrote:
> On 12/16/13, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote:
>> There's quite a bit of evidence in support of among (not as much for
>> between) in the source code of Facebook's cpplint, soon to be open
>> sourced. Here are some relevant quotes:
>
>>From first glance it seems you're always using among when needing a
> boolean result, so why does among have to return an index instead of a
> boolean true/false?

More info is better than less info, especially if it comes for free. We already use this idiom in a couple of places in Phobos.

Andrei


December 16, 2013
On 12/16/13 2:39 PM, Joseph Rushton Wakeling wrote:
> On 16/12/13 23:30, Andrei Alexandrescu wrote:
>> I guess if we need several versions of between, we could give up on it.
>
> What's wrong with having it implemented analogous to std.random.uniform
> -- taking a bounds parameter which allows for open and/or closed at
> either end, with the default being "[)" ... ?

Too much sophistication for too trivial work. One between would be fine. Four would be probably overdoing it. So I'm retracting my proposal for between.

Andrei


December 16, 2013
On 12/16/2013 2:24 PM, Jakob Ovrum wrote:
> On Monday, 16 December 2013 at 22:10:28 UTC, Walter Bright wrote:
>> Exactly, meaning I'd have to go look at the source code for it, whereas with
>> the latter I can see right away what it is. The function is a net programmer
>> time loss.
>
> Surely you would turn to the documentation, not the source code.

I hate to say it, but often when I read the Phobos library documentation I wind up having to check the source code. Yes, I should follow my own advice and do PRs.

December 16, 2013
On 12/16/2013 2:51 PM, H. S. Teoh wrote:
> Oooh, do I hear a volunteer for cleaning up iota? ;-)

I'm not giving in one iota!

December 16, 2013
On 12/16/13 2:55 PM, "Ola Fosheim Grøstad" <ola.fosheim.grostad+dlang@gmail.com>" wrote:
> On Monday, 16 December 2013 at 21:45:40 UTC, Walter Bright wrote:
>>> uint among(T, Us...)(T v, Us vals)
>>> {
>>>     foreach (i, U; Us)
>>>     {
>>>         if (v == vals[i]) return i + 1;
>>>     }
>>>     return 0;
>>> }
>>
>> This has O(n) behavior, which might be unexpected for the user.
>
> I would expect one table-lookup for this if vals contains strings, not N
> ifs. If you look at the example, most of them could be done with perfect
> hashing on a single character.
>
> Is it possible for the compiler/template system to turn this into a
> switch/dictionary? Or is there something in the language/compiler that
> makes that impossible?

It's a good idea, but unfortunately we don't have partial evaluation. We'd need to move whatever compile-time work is to be done in the template arguments area, i.e.

value.among!("foo", "bar", "baz")

as opposed to

value.among("foo", "bar", "baz")

Now that I think of it we can easily support both forms.



Andrei

December 16, 2013
Andrei Alexandrescu:

> Too much sophistication for too trivial work. One between would be fine. Four would be probably overdoing it. So I'm retracting my proposal for between.

Regarding "between", in Bugzilla I suggested to add "in" operator overloading to iota:

5 in iota(5, 12)

And elsewhere in Bugzilla I suggested to give the "[]" to iota:

5 in iota!"[]"(5, 12)

https://d.puremagic.com/issues/show_bug.cgi?id=11252
https://d.puremagic.com/issues/show_bug.cgi?id=10466

Bye,
bearophile
December 16, 2013
On Monday, 16 December 2013 at 23:01:36 UTC, Walter Bright wrote:
> I hate to say it, but often when I read the Phobos library documentation I wind up having to check the source code. Yes, I should follow my own advice and do PRs.

You should consider having direct links to html-formatted source code from the documentation. Like this:

http://webapp-improved.appspot.com/api/webapp2_extras/json.html

I find that simple "[source]"-link to be a very helpful solution. It is very difficult to make good enough documentation to cover things like assumptions that you need to know about when doing inheritance related stuff. So easy access to code is very nice, and that also means that the real documentation can be less detailed and verbose and easier to comprehend.

December 16, 2013
On Monday, 16 December 2013 at 23:50:26 UTC, Ola Fosheim Grøstad wrote:
> You should consider having direct links to html-formatted

Just to clarify: I meant "direct links to each function" not the entire source…