www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Why I Use D

reply bearophile <bearophileHUGS lycos.com> writes:
Scott S. McCoy:
 I'll take a second to plug my thoughts on array slicing.
 array[..] should be synonymous with array[0..$]

You can already do that: array[]
 array[5..] should be synonymous with array[5..$]/array[5..length]
 array[..5] should be synonymous with array[0..5].

That's equal to Python, where you can write: array[5:] array[:5] Something very close is possible in Haskell too. Bye, bearophile
Mar 30 2008
next sibling parent reply "Scott S. McCoy" <tag cpan.org> writes:
Content-Type: text/plain
Content-Transfer-Encoding: 7bit


On Sun, 2008-03-30 at 07:48 -0400, bearophile wrote:

 
 That's equal to Python, where you can write: array[5:] array[:5]
 Something very close is possible in Haskell too.
 

Right, I just think it's nicer than array[5..length] :-) array[5..] and array[..5] seem to make perfect sense to me and get rid of kludge like $.
Mar 30 2008
next sibling parent reply Leandro Lucarella <llucax gmail.com> writes:
Janice Caron, el 30 de marzo a las 20:11 me escribiste:
 On 30/03/2008, Scott S. McCoy <tag cpan.org> wrote:
  array[5..] and array[..5] seem to make perfect sense to me and get rid of
 kludge like $.

But those constructions don't get rid of $ entirely. Sure, you'd be able to write array[5..] instead of array[5..$]. But you still wouldn't be able to write array[5..-1] instead of array[5..$-1]. (Not so in Python, I believe).

In python this is array[5:-1], which seems a lot nicer to me... I hope D some can do array[5..-1], array[..-4], array[..], etc. a la Python.
 So it only buys you the luxury of not having to type $ in one very
 special case. In my view, that makes keeping it more consistent than
 dropping it. (And come on - it's only one character!)

Yes, only 1 character but it looks like an ugly hack to me. '$' has no meaning except in array slices, and what not necessary anyway. I know it's hard (impossible?) to get rid of it now, but I wanted to say it =) -- Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/ ---------------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------------- 22% of the time a pizza will arrive faster than an ambulance in Great-Britain
Mar 30 2008
parent reply bearophile <bearophileHUGS lycos.com> writes:
Leandro Lucarella:
 In python this is array[5:-1], which seems a lot nicer to me...

I know many languages, and in many situations I think Python has a good syntax (like Python list comprehensions and generators, two things that I think D may enjoy a LOT). But in this case I think D has chosen a better solution: if you allow a syntax like [5:-1], then in every access to an array you have to compute a modulus, or a sum, for the wrap around. ShedSkin does this, but in tight loops on arrays this slows down the code (in ShedSkin there is a compilation option to disable that, to increase running speed). For Python that's not a big problem, because Python is designed to be the best for the programmer first, and for the CPU then, but D is supposed to be fast almost as C, and just adding a single $ character it avoids that slowdown, so I think this is a really good compromise (that future languages will probably copy from D). The syntax array[..5] / array[5..] looks acceptable, but it allows you to avoid just one char, so it's far from the top of the list of things I'd like to see in D :-) Bye, bearophile
Mar 30 2008
parent reply Leandro Lucarella <llucax gmail.com> writes:
bearophile, el 30 de marzo a las 16:03 me escribiste:
 Leandro Lucarella:
 In python this is array[5:-1], which seems a lot nicer to me...

I know many languages, and in many situations I think Python has a good syntax (like Python list comprehensions and generators, two things that I think D may enjoy a LOT). But in this case I think D has chosen a better solution: if you allow a syntax like [5:-1], then in every access to an array you have to compute a modulus, or a sum, for the wrap around. ShedSkin does this, but in tight loops on arrays this slows down the code (in ShedSkin there is a compilation option to disable that, to increase running speed). For Python that's not a big problem, because Python is designed to be the best for the programmer first, and for the CPU then, but D is supposed to be fast almost as C, and just adding a single $ character it avoids that slowdown, so I think this is a really good compromise (that future languages will probably copy from D).

I really can't see any overhead in array[5..-1] being syntax sugar for array[5..$-1]. I'm not talking about array[5..-1] returning an empty array if the array.lenght is 3 for example (which is really nice too, but I agree that the overhead could be ... polemic, so it might better suite in the standard library). -- Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/ ---------------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------------- Una mujer en bicicleta, con sombrero de paja, es la más flagrante violación a las leyes de la aerodinamia. -- Ricardo Vaporeso. 21 de Septiembre de 1917.
Mar 30 2008
next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Leandro Lucarella wrote:
 bearophile, el 30 de marzo a las 16:03 me escribiste:
 Leandro Lucarella:
 In python this is array[5:-1], which seems a lot nicer to me...


I really can't see any overhead in array[5..-1] being syntax sugar for array[5..$-1]. I'm not talking about array[5..-1] returning an empty array if the array.lenght is 3 for example (which is really nice too, but I agree that the overhead could be ... polemic, so it might better suite in the standard library).

Yeh, array[5..-1] wouldn't necessarily have any overhead, but how about array[5..a]? --bb
Mar 30 2008
prev sibling parent Christopher Wright <dhasenan gmail.com> writes:
Leandro Lucarella wrote:
 bearophile, el 30 de marzo a las 16:03 me escribiste:
 Leandro Lucarella:
 In python this is array[5:-1], which seems a lot nicer to me...


I really can't see any overhead in array[5..-1] being syntax sugar for array[5..$-1]. I'm not talking about array[5..-1] returning an empty array if the array.lenght is 3 for example (which is really nice too, but I agree that the overhead could be ... polemic, so it might better suite in the standard library).

Except the mental overhead of interpreting -1 as array.length - 1.
Mar 30 2008
prev sibling parent reply Walter Bright <newshound1 digitalmars.com> writes:
Scott S. McCoy wrote:
 array[5..] and array[..5] seem to make perfect sense to me and get rid 
 of kludge like $.

If $ were implicit, you couldn't do things like: array[5 .. $-3]
Mar 30 2008
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Sun, 30 Mar 2008 22:43:11 +0200, Walter Bright  
<newshound1 digitalmars.com> wrote:

 Scott S. McCoy wrote:
 array[5..] and array[..5] seem to make perfect sense to me and get rid  
 of kludge like $.

If $ were implicit, you couldn't do things like: array[5 .. $-3]

You could if it were only optional. --Simen
Mar 30 2008
next sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Mon, 31 Mar 2008 00:26:10 +0200, Scott S. McCoy <tag cpan.org> wrote:

 You also could if array[5 .. -3] implied length -3.

 Similarly. array[-3] could imply array[array.length -3]...

 But someone suggested against that, atleast for non-compile time
 checking.  I would agree, for the non-literal instance that a negative
 integer of -5 shouldn't result in magical figuring out if the element
 needs to be pulled from the front or the back.  But the literal case
 adds expressiveness, I think.

T[int] foo; What is foo[-3]? And user-defined types with overloaded opIndex and opSlice. Should [-X] work differently for them? -- Simen
Mar 30 2008
parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Fri, 11 Apr 2008 09:45:26 +0200, Scott S. McCoy <tag cpan.org> wrote:

 The same thing foo[$-3] is now.

 Whatever the heck that is.

 :-)


 On Mon, 2008-03-31 at 01:55 +0200, Simen Kjaeraas wrote:
 On Mon, 31 Mar 2008 00:26:10 +0200, Scott S. McCoy <tag cpan.org> wrote:

 You also could if array[5 .. -3] implied length -3.

 Similarly. array[-3] could imply array[array.length -3]...

 But someone suggested against that, atleast for non-compile time
 checking.  I would agree, for the non-literal instance that a negative
 integer of -5 shouldn't result in magical figuring out if the element
 needs to be pulled from the front or the back.  But the literal case
 adds expressiveness, I think.

T[int] foo; What is foo[-3]? And user-defined types with overloaded opIndex and opSlice. Should [-X] work differently for them? -- Simen


I see. So I have an associative array foo, with indices possibly being negative (int is signed), and I can't reach those due to negative indices being wrapped around. Hardly a good thing, IMO. My point is, there may be reasons to want to use negative indices, and implicit $ will make that hard. Unless you suggest that to avoid that, I should use [$-$-3]? -- Simen
Apr 11 2008
prev sibling next sibling parent Walter Bright <newshound1 digitalmars.com> writes:
Scott S. McCoy wrote:
 You also could if array[5 .. -3] implied length -3.

How would I do: array[5 .. foo($)] ?
Mar 30 2008
prev sibling parent Georg Wrede <georg nospam.org> writes:
Scott S. McCoy wrote:
 You also could if array[5 .. -3] implied length -3.
 
 Similarly. array[-3] could imply array[array.length -3]...

I guess the problem you (and I guess quite a lot of people) ultimately have is, the $ looks like a after-thought, sticks in the eye in a language that otherwise doesn't use the $ too much, and if one is also used to some interpreted language it looks clumsy in this context. Before $ got this role in D, there were months of (lively) discussion about the alternatives. Ultimately, this is short to type, has expressive power, is totally obvious as to the intent, and it is easy to spot a typo within the brackets. So, it's not pretty, but to figure out a better alternative would need both hard work and luck. :-) And even then, one would have a hard sell in demonstrating the advantages, which would have to be significant. It's not like the $ issue is the easiest one to get really reopened.
Mar 31 2008
prev sibling next sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 30/03/2008, Scott S. McCoy <tag cpan.org> wrote:
  array[5..] and array[..5] seem to make perfect sense to me and get rid of
 kludge like $.

But those constructions don't get rid of $ entirely. Sure, you'd be able to write array[5..] instead of array[5..$]. But you still wouldn't be able to write array[5..-1] instead of array[5..$-1]. (Not so in Python, I believe). So it only buys you the luxury of not having to type $ in one very special case. In my view, that makes keeping it more consistent than dropping it. (And come on - it's only one character!)
Mar 30 2008
prev sibling next sibling parent "Scott S. McCoy" <tag cpan.org> writes:
Content-Type: text/plain
Content-Transfer-Encoding: 7bit


On Sun, 2008-03-30 at 16:41 -0300, Leandro Lucarella wrote:

 
 In python this is array[5:-1], which seems a lot nicer to me...
 
 I hope D some can do array[5..-1], array[..-4], array[..], etc. a la
 Python.

Fwiw, this is a feature Python borrowed from Perl. In Perl, a negative index means "array length - Y". But 5..-1 doesn't seem as pretty as 5.. I mean, if I didn't tell it where to go, where else is it going to go? Inference is nice, we seem to all agree...I don't hear anyone complaining about not having to specify their types in for-each loops...so why not infer the beginning or end of the array, I mean... if i say array[..5], where was I expecting to start other than the beginning?
 
 So it only buys you the luxury of not having to type $ in one very
 special case. In my view, that makes keeping it more consistent than
 dropping it. (And come on - it's only one character!)

Yes, only 1 character but it looks like an ugly hack to me. '$' has no meaning except in array slices, and what not necessary anyway. I know it's hard (impossible?) to get rid of it now, but I wanted to say it =)

I wouldn't think it'd be impossible. D 2.0 is considered unstable. Walter seems to be being careful not to make people too angry by breaking their code. My thoughts on that is "If the code you were writing was important, it shouldn't have been in D 2 anyway since D 2 isn't done." Although I appreciate that digital mars provides a functional implementation of a totally in-development language specification. That's pretty cool beans. People should be glad, not bitch about stuff breaking. You rarely get that type of luxury in other languages. Cheers, Scott S. McCoy
Mar 30 2008
prev sibling next sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 30/03/2008, Leandro Lucarella <llucax gmail.com> wrote:
  I hope D some can do array[5..-1], array[..-4], array[..], etc. a la
  Python.

No thanks. I don't want a runtime check slowing down all my array accesses. array[5..$-1] can be identified as end-relative at compile-time. Not so array[5..-1]. Worse, array[5..-1] might actually /mean/ "from 5 to minus one" (i.e. it might be a bug). If so, under the current regime, the bug gets found at bounds-checking time (in a debug build). If you suddenly make it legal, the bug slips through.
Mar 30 2008
prev sibling next sibling parent reply "Scott S. McCoy" <tag cpan.org> writes:
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

You also could if array[5 .. -3] implied length -3.

Similarly. array[-3] could imply array[array.length -3]...

But someone suggested against that, atleast for non-compile time
checking.  I would agree, for the non-literal instance that a negative
integer of -5 shouldn't result in magical figuring out if the element
needs to be pulled from the front or the back.  But the literal case
adds expressiveness, I think.

Cheers,
    Scott S. McCoy

On Sun, 2008-03-30 at 22:54 +0200, Simen Kjaeraas wrote:

 On Sun, 30 Mar 2008 22:43:11 +0200, Walter Bright  
 <newshound1 digitalmars.com> wrote:
 
 Scott S. McCoy wrote:
 array[5..] and array[..5] seem to make perfect sense to me and get rid  
 of kludge like $.

If $ were implicit, you couldn't do things like: array[5 .. $-3]

You could if it were only optional. --Simen

Mar 30 2008
parent BCS <ao pathlink.com> writes:
Reply to Scott,

 You also could if array[5 .. -3] implied length -3.
 
 Similarly. array[-3] could imply array[array.length -3]...
 

what about "array[$/2]" or "Fn(array[0..$/2]) + Fn(array[$/2..$])"
Mar 30 2008
prev sibling next sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 31/03/2008, Leandro Lucarella <llucax gmail.com> wrote:
 I really can't see any overhead in array[5..-1] being syntax sugar for
  array[5..$-1].

You can't? Well, I can. Let me show you. struct MyArray { MyArray opSlice(int lower, int upper) { ... } } MyArray array; auto slice = array[5..-1]; This calls MyArray.opSlice(5, -1). Are you suggesting it should instead call MyArray.opSlice(5, __dollar-1)? Can you not just see the bug reports flying in? And here's a second problem int[] array; int n = -1; slice1 = array[5..-1]; slice2 = array[5..n]; You'd expect slice1 and slice2 to be the same, but in fact array[5..n] will throw in a debug build, and cause a segfault in a release build. Not good.
Mar 31 2008
prev sibling next sibling parent "Scott S. McCoy" <tag cpan.org> writes:
I don't think that's any fair, partially because I would have never
assumed that would even work.

you would say:

array[ 5 .. foo(array.length) ] naturally, and at the office in a code
review if I saw someone doing foo($) I would explicitly ask them to
change it to foo(array.length) anyway.

On Sun, 2008-03-30 at 19:04 -0700, Walter Bright wrote:
 Scott S. McCoy wrote:
 You also could if array[5 .. -3] implied length -3.

How would I do: array[5 .. foo($)] ?

Apr 11 2008
prev sibling parent "Scott S. McCoy" <tag cpan.org> writes:
The same thing foo[$-3] is now.

Whatever the heck that is.

:-)


On Mon, 2008-03-31 at 01:55 +0200, Simen Kjaeraas wrote:
 On Mon, 31 Mar 2008 00:26:10 +0200, Scott S. McCoy <tag cpan.org> wrote:
 
 You also could if array[5 .. -3] implied length -3.

 Similarly. array[-3] could imply array[array.length -3]...

 But someone suggested against that, atleast for non-compile time
 checking.  I would agree, for the non-literal instance that a negative
 integer of -5 shouldn't result in magical figuring out if the element
 needs to be pulled from the front or the back.  But the literal case
 adds expressiveness, I think.

T[int] foo; What is foo[-3]? And user-defined types with overloaded opIndex and opSlice. Should [-X] work differently for them? -- Simen

Apr 11 2008