| Thread overview | |||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 30, 2008 Re: Why I Use D | ||||
|---|---|---|---|---|
| ||||
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 | ||||
March 30, 2008 Re: Why I Use D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile Attachments:
|
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 $.
| |||
March 30, 2008 Re: Why I Use D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Scott S. McCoy | 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!)
| |||
March 30, 2008 Re: Why I Use D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | 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 | |||
March 30, 2008 Re: Why I Use D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Leandro Lucarella Attachments:
| 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 | |||
March 30, 2008 Re: Why I Use D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Leandro Lucarella | 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
| |||
March 30, 2008 Re: Why I Use D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Scott S. McCoy | 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]
| |||
March 30, 2008 Re: Why I Use D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Leandro Lucarella | 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.
| |||
March 30, 2008 Re: Why I Use D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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
| |||
March 30, 2008 Re: Why I Use D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas Attachments:
| 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply