May 14, 2010
Steven Schveighoffer wrote:

> In regex, ^ matches beginning of the line, $ matches end of the line

So far so good... :)

> So how does this look:  coll[^..$];

Speaking of regex, [^ sequence starts a set of excluded characters. :)

$ has always bugged me anyway, so how about no character at all:

coll[..n];  // beginning to n
coll[n..];  // n to end
coll[..];   // all of it

I like it! :)

Ali
May 14, 2010
Ali Çehreli:
> $ has always bugged me anyway, so how about no character at all:

The $ is not elegant, but it's a good solution to a design problem, how to represent items from the bottom of the array. In Python you write:
a[-5]
In D you write:
a[$-5]

This small one-char difference has an important effect for a system language: the presence of $ allows you to avoid a conditional each time you want to access an array item :-)
So I think it as one of the smartest details of D design :-)

Bye,
bearophile
May 14, 2010
Nick Sabalausky wrote:
> 
> I think 0 makes perfect sense for any ordered container, or, really, anything for which $ makes sense (plus some things for which $ doesn't make sense, like an right-infinite range). However, the rest of your argument convinced me.

What if you order by > instead of <, then 0 should be the end surely.

Also what if you have an array that you can access with a negative index? Then 0 is at some indeterminate point in the range.

Ok it's pretty bloody rare to have arrays like that, but I've done it a couple of times before now.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
May 15, 2010
On Fri, 14 May 2010 13:33:57 -0400, Walter Bright <newshound1@digitalmars.com> wrote:

> Steven Schveighoffer wrote:
>> So how does this look:  coll[^..$];
>
> nooooooooo <g>

Do you have specific objections, or does it just look horrendous to you :)  Would another symbol be acceptable?

>
>> Thoughts? other ideas?
>
> I'd just go with accepting the literal 0. Let's see how far that goes first.

I thought of a counter case:

auto tm = new TreeMap!(int, uint);
tm[-1] = 5;
tm[1] = 6;

What does tm[0..$] mean?  What about tm[0]?  If it is analogous to "beginning of collection" then it doesn't make any sense for a container with a key of numeric type.

Actually any map type where the indexes don't *always* start at zero are a problem.

I can make 0 work for LinkList and ArrayList, but not any of the others.  Even with TreeSet, I allow using element values as slice arguments.

I guess I should have pointed this out in my first post... sorry.

-Steve
May 15, 2010
On Fri, 14 May 2010 16:42:28 -0400, Ali Çehreli <acehreli@yahoo.com> wrote:

> Steven Schveighoffer wrote:
>
>  > In regex, ^ matches beginning of the line, $ matches end of the line
>
> So far so good... :)
>
>  > So how does this look:  coll[^..$];
>
> Speaking of regex, [^ sequence starts a set of excluded characters. :)

Yeah, that is a good counter-argument :)

> $ has always bugged me anyway, so how about no character at all:
>
> coll[..n];  // beginning to n
> coll[n..];  // n to end
> coll[..];   // all of it
>
> I like it! :)

Well, for true contiguous ranges such as arrays, you need to have ways of adding or subtracting values.  For example:

a[0..$-1];

How does that look with your version?

a[0..-1];

not good.  I think we need something to denote "end" and I would also like something to denote "beginning", and I think that can't be empty space.

-Steve
May 15, 2010
"bearophile" <bearophileHUGS@lycos.com> wrote in message news:hskd8g$2bcd$1@digitalmars.com...
> Ali Çehreli:
>> $ has always bugged me anyway, so how about no character at all:
>
> The $ is not elegant, but it's a good solution to a design problem, how to
> represent items from the bottom of the array. In Python you write:
> a[-5]
> In D you write:
> a[$-5]
>
> This small one-char difference has an important effect for a system
> language: the presence of $ allows you to avoid a conditional each time
> you want to access an array item :-)
> So I think it as one of the smartest details of D design :-)
>

Once upon a time, there was a book called "Writing Solid Code". It seemed that anyone who was an established, respectable programmer swore by it and proclaimed it should be required reading by all programmers. These days, I sometimes feel like I'm the only one who's ever heard of it (let alone read it).

So much of the book has made such an impact on me as a programmer, that from the very first time I ever heard of a language (probably Python) using "someArray[-5]" to denote an index from the end, I swear, the very first thought that popped into my head was "Candy-Machine Interface". I instantly disliked it, and still consider it a misguided design.

For anyone who doesn't see the the problem with Python's negative indicies (or anyone who wants to delve into one of the forerunners to great books like "Code Craft" or "The Pragmatic Programmer"), I *highly* recommend tracking down a copy of "Writing Solid Code" and reading "The One-Function Memory Manager" and "Wishy-Washy Inputs", both in the "Candy-Machine Interfaces" chapter.

(Although, the book did have such an impact on the programming world at the time, that many of the cautions in it sound like no-brainers today, like not using return values to indicate error codes. But even for those, it goes into much more detail on the "why" than what you usually hear.)


May 15, 2010
Steven Schveighoffer wrote:
> On Fri, 14 May 2010 13:33:57 -0400, Walter Bright <newshound1@digitalmars.com> wrote:
> 
>> Steven Schveighoffer wrote:
>>> So how does this look:  coll[^..$];
>>
>> nooooooooo <g>
> 
> Do you have specific objections, or does it just look horrendous to you :)  Would another symbol be acceptable?


The problem is D already has a lot of syntax. More syntax just makes the language more burdensome after a certain point, even if in isolation it's a good idea.

One particular problem regex has is that few can remember its syntax unless they use it every day.


>>> Thoughts? other ideas?
>>
>> I'd just go with accepting the literal 0. Let's see how far that goes first.
> 
> I thought of a counter case:
> 
> auto tm = new TreeMap!(int, uint);
> tm[-1] = 5;
> tm[1] = 6;
> 
> What does tm[0..$] mean?  What about tm[0]?  If it is analogous to "beginning of collection" then it doesn't make any sense for a container with a key of numeric type.
> 
> Actually any map type where the indexes don't *always* start at zero are a problem.

I'd question the design of a map type that has the start at something other than 0.
May 15, 2010
On May 15, 10 13:07, Walter Bright wrote:
> Steven Schveighoffer wrote:
>> On Fri, 14 May 2010 13:33:57 -0400, Walter Bright
>> <newshound1@digitalmars.com> wrote:
>>
>>> Steven Schveighoffer wrote:
>>>> So how does this look: coll[^..$];
>>>
>>> nooooooooo <g>
>>
>> Do you have specific objections, or does it just look horrendous to
>> you :) Would another symbol be acceptable?
>
>
> The problem is D already has a lot of syntax. More syntax just makes the
> language more burdensome after a certain point, even if in isolation
> it's a good idea.
>
> One particular problem regex has is that few can remember its syntax
> unless they use it every day.
>
>
>>>> Thoughts? other ideas?
>>>
>>> I'd just go with accepting the literal 0. Let's see how far that goes
>>> first.
>>
>> I thought of a counter case:
>>
>> auto tm = new TreeMap!(int, uint);
>> tm[-1] = 5;
>> tm[1] = 6;
>>
>> What does tm[0..$] mean? What about tm[0]? If it is analogous to
>> "beginning of collection" then it doesn't make any sense for a
>> container with a key of numeric type.
>>
>> Actually any map type where the indexes don't *always* start at zero
>> are a problem.
>
> I'd question the design of a map type that has the start at something
> other than 0.

Why a map type (sorted associative array)'s key must start at zero?
May 15, 2010
KennyTM~ wrote:
> Why a map type (sorted associative array)'s key must start at zero?

You can special case the [0..$], or simply use [] to represent the entire range.
May 15, 2010
On 05/14/2010 10:42 PM, Ali Çehreli wrote:
> $ has always bugged me anyway, so how about no character at all:
>
> coll[..n]; // beginning to n
> coll[n..]; // n to end
> coll[..]; // all of it
>
> I like it! :)
>
> Ali

coll[uniform(0,$)]; // <-- awesome.