Negative indices for arrays

classic Classic list List threaded Threaded
31 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Negative indices for arrays

Dmitry Soshnikov
On 12.11.2010 1:21, Allen Wirfs-Brock wrote:
>> -----Original Message-----
>> From: Peter van der Zee [mailto:[hidden email]]
> ...
>> I guess I would like -n to map to length-n, but I'm not sure whether it's worth the
>> cost described above. After all, it's just sugar.
>>
> Like Oliver also said.

Like everybody on this list understands _what_ it is.

>    This isn't just sugar, it is a deep semantic change to the interpretation of property names

As I see it:

1. if to consider only for arrays -- it's a minimal semantics changes
and minimal backward compats.
2. if to make it generic, we need another syntax.

What do you suggest?

Dmitry.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Negative indices for arrays

Dmitry Soshnikov
In reply to this post by Dave Herman
On 12.11.2010 2:25, David Herman wrote:
>> If harmony would introduce this syntax "guarded" under a new script type, there
>> would at least be no danger of breaking the web (existing scripts).
> That sounds like an interop nightmare -- you're talking about forking the Array type between language versions. Keep in mind that non-Harmony and Harmony code will be able to interact in the same page.
>

Agreed, it will be very hard to combine different semantics on the same
page; it's not the case.

> Dave
>

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Negative indices for arrays

Erik Corry-2
In reply to this post by Dmitry Soshnikov
2010/11/12 Dmitry A. Soshnikov <[hidden email]>:

> What do you suggest?

I suggest you monkey patch a get method on the Array prototype and
forget trying to get the language semantics changed.  The people who
implement the language have made their opinions clear and as of this
post that includes V8.

--
Erik Corry
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Negative indices for arrays

Dmitry Soshnikov
In reply to this post by Brendan Eich-3
On 12.11.2010 2:40, Brendan Eich wrote:
> I agree with Dave, Allen, and Oliver

That's OK.

I consider several variants also. As Allen mentioned, if it will be a
generic thing, then `foo[-n]` may break an old code (and then we need
another syntax e.g. a[* - 1] -- if need at all then!). OTOH, if only for
arrays -- I don't see any big _real practical_ issues.

> that we should not just change indexing under Harmony script-type opt-in.
>
> Note also that Python, at least, has a more elaborate system of slicing that has evolved over the years. I added slice in the Netscape 4 era, which made it into ES3 and has the desired negative index behavior, but other additions from that era (some from Perl!) do not match, and anyway, the array-index-is-a-property-name ship had sailed in 1995.

Of course it had sailed. But the thing is nobody (?) on that ship used
negative-array-index in old code -- in a some more-less serious _real
practical_ application. So it may be re-borrowed and put on the next
ship. However, not necessary in this view -- a[-1]. If generic, -- with
another syntax.

> If we were to develop a generic slice proposal with new syntax, then I think it would be crazy not to follow Python's (and ES3-5's slice method's) lead and support negative indexing. This would not break existing code because it would happen only with new syntax and new functions or methods.
>

Absolutely.

> The obvious slice syntax is a[i:j] for a slice of a from index i up to but not including index j, with negative indexes supported as from the end, and with indexes on either side of : optional, defaulting as in Python.
>
> If : is problematic we could try CoffeeScript's .., but .. and ... as well as . in the language may be too much. The syntax is not as important as the semantics at this point, but one more syntax observation:
>
> The [:] syntax fits in Python, where a[-1] is the last element of the sequence a.

> But since we don't want to make that change to JS arrays,

So, can we stop at this point? I mean -- if we really don't want this
changes, then the question may be closed, I guess. Just acknowledge it.

>   this slice syntax is not so attractive. It will mislead Python folks into using negative indexes with property accesses as well as with slices.
>

Yeah, true. Though, maybe nevertheless to completely port Python's
slicing for arrays? ;P I liked this idea. But -- only for arrays, not
generic then. Unfortunatelly, it may break current generic slice --
`[].slice.call({0:1, 1:2, length:2}, -1)[0]` // 2.

> Ruby does not dedicate syntax, using methods instead (Smalltalk-y, as one would hope).
>

How that? Coffee borrows its .. (including higher index) and ...
(excluding higher index) from Ruby:

Ruby:

a = [1, 2, 3, 4]
a[-1] # 4

a[0..2] # [1,2,3]
a[0...2] # [1,2]

(need to say, that for me .. and ... should be logically inverted: i.e.
... should include, and .. -- exclude).

> This seems much more promising for Harmony than new slice or from-the-end indexing syntax, since we already have Array and String slice methods that do the right thing with negative indexes.

Yes, regarding slice -- yes. And moreover, they are generic.

>   We have String charAt/charCodeAt but part of the appeal of slicing and negative indexing is how they work on all sequence types in Python.
>
> An Array and String get method that takes an index, which can be negative, would complement slice. Array would want a set or put method too. Names need tuning, but they ought to be short.

Absolutely.


> /be
>
> On Nov 11, 2010, at 3:25 PM, David Herman wrote:
>
>>> If harmony would introduce this syntax "guarded" under a new script type, there
>>> would at least be no danger of breaking the web (existing scripts).
>> That sounds like an interop nightmare -- you're talking about forking the Array type between language versions. Keep in mind that non-Harmony and Harmony code will be able to interact in the same page.
>>
>> Dave
>>
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Negative indices for arrays

Dmitry Soshnikov
In reply to this post by Erik Corry-2
On 12.11.2010 12:01, Erik Corry wrote:
> 2010/11/12 Dmitry A. Soshnikov<[hidden email]>:
>
>> What do you suggest?
> I suggest you monkey patch a get method on the Array prototype and
> forget trying to get the language semantics changed.  The people who
> implement the language have made their opinions clear and as of this
> post that includes V8.
>

Sure. And already have done (as mentioned in the initial message --
http://bit.ly/9KBxzl) -- with using proxies it's very easy to implement.
Unfortunately (or fortunately -- it depends), not on `Array.prototype`
(since I cannot replace it with a proxy, because of [[Configurable]] /
[[Writable]] false), but with using `Array.new` semantics. It may be
even used. Though, without a big wide-spreading (as in other langs), it
will be just an academic curiosity without practical usage.

Dmitry.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Negative indices for arrays

Brendan Eich-3
In reply to this post by Dmitry Soshnikov
On Nov 12, 2010, at 1:01 AM, Dmitry A. Soshnikov wrote:

> I consider several variants also. As Allen mentioned, if it will be a generic thing, then `foo[-n]` may break an old code (and then we need another syntax e.g. a[* - 1] -- if need at all then!). OTOH, if only for arrays -- I don't see any big _real practical_ issues.

It is dangerous to make assertions based on wishes, instead of gathering data (see below).


>> that we should not just change indexing under Harmony script-type opt-in.
>>
>> Note also that Python, at least, has a more elaborate system of slicing that has evolved over the years. I added slice in the Netscape 4 era, which made it into ES3 and has the desired negative index behavior, but other additions from that era (some from Perl!) do not match, and anyway, the array-index-is-a-property-name ship had sailed in 1995.
>
> Of course it had sailed. But the thing is nobody (?) on that ship used negative-array-index in old code -- in a some more-less serious _real practical_ application.

You do keep asserting that. :-|


> Though, maybe nevertheless to completely port Python's slicing for arrays? ;P I liked this idea. But -- only for arrays, not generic then. Unfortunatelly, it may break current generic slice -- `[].slice.call({0:1, 1:2, length:2}, -1)[0]` // 2.


>> Ruby does not dedicate syntax, using methods instead (Smalltalk-y, as one would hope).
>
> How that?

I was referring to slice.

Ok, data: I added logging code to detect whenever an array is accessed via a negative index to my custom http://hg.mozilla.org/tracemonkey Firefox build. Here after a few minutes of browsing is the  log file, piped through sort -u (there are many repeated entries):

file:///Users/brendaneich/Hacking/hg.mozilla.org/tracemonkey/OBJ/dist/MinefieldDebug.app/Contents/MacOS/components/nsSessionStore.js:1762:-1
http://i.i.com.com/cnwk.1d/html/rb/js/tron/oreo.moo.rb.combined.js:6:-1
http://i.zdnet.com/js/mootools-core-more.js:3:-1
http://l.yimg.com/a/combo?metro/tuc/tuc_outboxlite_0.0.29.js&metro/contentcarousel/contentcarousel_0.1.40.js&metro/contentcarousel/contentcarouseladserved_0.1.6.js&metro/uiplugins/toolbar_bridge_service_0.1.18.js&metro/uiplugins/tooltip_service_0.1.47.js&metro/uiplugins/sortable_service_0.1.20.js&metro/pa/pautil_0.1.4.js&metro/pa/palist_0.1.23.js&metro/pa/pa_0.1.218.js&metro2/multimedia/multimedia_0.0.6.js&metro/batchload/batchload_0.1.7.js:7:-1

I'll keep this browser running for a bit, but it is clear "somebody" used negative array indexes. And it would be no fun finding such dynamic occurrences when porting if we made negative indexes work as in Python in Harmony.

In general, taking ES5 strict code into Harmony should not require porting, other than removing any dependencies on global variable vs global object property aliasing.

/be

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Negative indices for arrays

Dmitry Soshnikov
Brendan, thanks for the analysis and tests, possibly it's useful. Also,
I think that a proposal can look more like a proposal (and not like I'm
very want to include something into the language, and other members
resist it), I'm not sure we need to prove something in this case. If
everybody on the lists have decided that this is not needed for
ECMAScript -- that's completely OK.

(Below some small additions)

On 12.11.2010 18:40, Brendan Eich wrote:
> On Nov 12, 2010, at 1:01 AM, Dmitry A. Soshnikov wrote:
>
>> I consider several variants also. As Allen mentioned, if it will be a generic thing, then `foo[-n]` may break an old code (and then we need another syntax e.g. a[* - 1] -- if need at all then!). OTOH, if only for arrays -- I don't see any big _real practical_ issues.
> It is dangerous to make assertions based on wishes, instead of gathering data (see below).
>
>

Sure.

>>> that we should not just change indexing under Harmony script-type opt-in.
>>>
>>> Note also that Python, at least, has a more elaborate system of slicing that has evolved over the years. I added slice in the Netscape 4 era, which made it into ES3 and has the desired negative index behavior, but other additions from that era (some from Perl!) do not match, and anyway, the array-index-is-a-property-name ship had sailed in 1995.
>> Of course it had sailed. But the thing is nobody (?) on that ship used negative-array-index in old code -- in a some more-less serious _real practical_ application.
> You do keep asserting that. :-|
>
>

Of course. Though, we may say that neither Allen (including Erick from
V8), nor Oliver didn't even asserted. Just some abstract "dramatic",
"critical" breaking of the old code. But in real, I didn't see it. Your
tests with logging maybe prove something, but hardly it's an absolute
and real results. Anyway, it's something.

>> Though, maybe nevertheless to completely port Python's slicing for arrays? ;P I liked this idea. But -- only for arrays, not generic then. Unfortunatelly, it may break current generic slice -- `[].slice.call({0:1, 1:2, length:2}, -1)[0]` // 2.
>
>>> Ruby does not dedicate syntax, using methods instead (Smalltalk-y, as one would hope).
>> How that?
> I was referring to slice.
>

Hm... These are slices:

a = [1, 2, 3, 4]
a[0..2] # 1,2,3
a[1..-1] # 2,3,4

a[-1..-1] # 4
a[-1] # the same, 4

Btw, in ES, slice being able to slice:

a.slice(-2, -1); // [3]

unable to slice:

a.slice(-2, -1); // []

and requires:

a.slice(-1)[0]; // 4

but this is -- JFTR.


> Ok, data: I added logging code to detect whenever an array is accessed via a negative index to my custom http://hg.mozilla.org/tracemonkey Firefox build. Here after a few minutes of browsing is the  log file, piped through sort -u (there are many repeated entries):
>
> file:///Users/brendaneich/Hacking/hg.mozilla.org/tracemonkey/OBJ/dist/MinefieldDebug.app/Contents/MacOS/components/nsSessionStore.js:1762:-1
> http://i.i.com.com/cnwk.1d/html/rb/js/tron/oreo.moo.rb.combined.js:6:-1
> http://i.zdnet.com/js/mootools-core-more.js:3:-1
> http://l.yimg.com/a/combo?metro/tuc/tuc_outboxlite_0.0.29.js&metro/contentcarousel/contentcarousel_0.1.40.js&metro/contentcarousel/contentcarouseladserved_0.1.6.js&metro/uiplugins/toolbar_bridge_service_0.1.18.js&metro/uiplugins/tooltip_service_0.1.47.js&metro/uiplugins/sortable_service_0.1.20.js&metro/pa/pautil_0.1.4.js&metro/pa/palist_0.1.23.js&metro/pa/pa_0.1.218.js&metro2/multimedia/multimedia_0.0.6.js&metro/batchload/batchload_0.1.7.js:7:-1
>
> I'll keep this browser running for a bit, but it is clear "somebody" used negative array indexes. And it would be no fun finding such dynamic occurrences when porting if we made negative indexes work as in Python in Harmony.
>

Maybe they tried to apply exactly Python's semantics while were testing?
:D But seriously, yep, the test may say something.

> In general, taking ES5 strict code into Harmony should not require porting, other than removing any dependencies on global variable vs global object property aliasing.
>

OK, so since all are against, I think the proposal (if is not required
hard) may be canceled.

Thanks again.

Dmitry.

> /be
>

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Negative indices for arrays

Dmitry Soshnikov
On 13.11.2010 0:25, Dmitry A. Soshnikov wrote:
> unable to slice:
>
> a.slice(-2, -1); // []
Sorry, typo; meant a.slice(-1, -1); []

Dmitry.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Negative indices for arrays

Felix-54
the D language handles this by having the names '$' and 'length' be the
length of the array, within the scope of a [] subscript, so you can say
something like a[$-1].  '$' is already too widely used in js, but maybe
something like '_' would work.  say, if there's no declaration of _ in
scope, then _ within [] is the length of the array.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Negative indices for arrays

Dmitry Soshnikov
On 13.11.2010 0:52, felix wrote:
> the D language handles this by having the names '$' and 'length' be
> the length of the array, within the scope of a [] subscript, so you
> can say something like a[$-1].  '$' is already too widely used in js,
> but maybe something like '_' would work.  say, if there's no
> declaration of _ in scope, then _ within [] is the length of the array.
>

$ and _ -- both are valid identifiers which may already have values.
Though, $ is used in regexp and isn't confused with the same name
identifier. With the same success any other could be accepted. But, as
said -- everyone on this list do not see the need.

Dmitry.

> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Negative indices for arrays

Brendan Eich-3
In reply to this post by Dmitry Soshnikov
I would have liked negative indexes for bracket-indexing from the end, and I suspect others who knew "in their bones" that it's too incompatible of a change would too -- we just knew it was not going to be a non-breaking change.

It's very hard to change subtle features that the Web comes to depend on. I've only seen building better mousetraps, leading people away from the old trap, work over time. It's not a speedy process, especially if the subtle feature is not a misfeature (but people disagree on this difference -- 'with' is still with us, some people actually defend and use it).

/be

On Nov 12, 2010, at 1:25 PM, Dmitry A. Soshnikov wrote:

> Brendan, thanks for the analysis and tests, possibly it's useful. Also, I think that a proposal can look more like a proposal (and not like I'm very want to include something into the language, and other members resist it), I'm not sure we need to prove something in this case. If everybody on the lists have decided that this is not needed for ECMAScript -- that's completely OK.
>
> (Below some small additions)
>
> On 12.11.2010 18:40, Brendan Eich wrote:
>> On Nov 12, 2010, at 1:01 AM, Dmitry A. Soshnikov wrote:
>>
>>> I consider several variants also. As Allen mentioned, if it will be a generic thing, then `foo[-n]` may break an old code (and then we need another syntax e.g. a[* - 1] -- if need at all then!). OTOH, if only for arrays -- I don't see any big _real practical_ issues.
>> It is dangerous to make assertions based on wishes, instead of gathering data (see below).
>>
>>
>
> Sure.
>
>>>> that we should not just change indexing under Harmony script-type opt-in.
>>>>
>>>> Note also that Python, at least, has a more elaborate system of slicing that has evolved over the years. I added slice in the Netscape 4 era, which made it into ES3 and has the desired negative index behavior, but other additions from that era (some from Perl!) do not match, and anyway, the array-index-is-a-property-name ship had sailed in 1995.
>>> Of course it had sailed. But the thing is nobody (?) on that ship used negative-array-index in old code -- in a some more-less serious _real practical_ application.
>> You do keep asserting that. :-|
>>
>>
>
> Of course. Though, we may say that neither Allen (including Erick from V8), nor Oliver didn't even asserted. Just some abstract "dramatic", "critical" breaking of the old code. But in real, I didn't see it. Your tests with logging maybe prove something, but hardly it's an absolute and real results. Anyway, it's something.
>
>>> Though, maybe nevertheless to completely port Python's slicing for arrays? ;P I liked this idea. But -- only for arrays, not generic then. Unfortunatelly, it may break current generic slice -- `[].slice.call({0:1, 1:2, length:2}, -1)[0]` // 2.
>>
>>>> Ruby does not dedicate syntax, using methods instead (Smalltalk-y, as one would hope).
>>> How that?
>> I was referring to slice.
>>
>
> Hm... These are slices:
>
> a = [1, 2, 3, 4]
> a[0..2] # 1,2,3
> a[1..-1] # 2,3,4
>
> a[-1..-1] # 4
> a[-1] # the same, 4
>
> Btw, in ES, slice being able to slice:
>
> a.slice(-2, -1); // [3]
>
> unable to slice:
>
> a.slice(-2, -1); // []
>
> and requires:
>
> a.slice(-1)[0]; // 4
>
> but this is -- JFTR.
>
>
>> Ok, data: I added logging code to detect whenever an array is accessed via a negative index to my custom http://hg.mozilla.org/tracemonkey Firefox build. Here after a few minutes of browsing is the  log file, piped through sort -u (there are many repeated entries):
>>
>> file:///Users/brendaneich/Hacking/hg.mozilla.org/tracemonkey/OBJ/dist/MinefieldDebug.app/Contents/MacOS/components/nsSessionStore.js:1762:-1
>> http://i.i.com.com/cnwk.1d/html/rb/js/tron/oreo.moo.rb.combined.js:6:-1
>> http://i.zdnet.com/js/mootools-core-more.js:3:-1
>> http://l.yimg.com/a/combo?metro/tuc/tuc_outboxlite_0.0.29.js&metro/contentcarousel/contentcarousel_0.1.40.js&metro/contentcarousel/contentcarouseladserved_0.1.6.js&metro/uiplugins/toolbar_bridge_service_0.1.18.js&metro/uiplugins/tooltip_service_0.1.47.js&metro/uiplugins/sortable_service_0.1.20.js&metro/pa/pautil_0.1.4.js&metro/pa/palist_0.1.23.js&metro/pa/pa_0.1.218.js&metro2/multimedia/multimedia_0.0.6.js&metro/batchload/batchload_0.1.7.js:7:-1
>>
>> I'll keep this browser running for a bit, but it is clear "somebody" used negative array indexes. And it would be no fun finding such dynamic occurrences when porting if we made negative indexes work as in Python in Harmony.
>>
>
> Maybe they tried to apply exactly Python's semantics while were testing? :D But seriously, yep, the test may say something.
>
>> In general, taking ES5 strict code into Harmony should not require porting, other than removing any dependencies on global variable vs global object property aliasing.
>>
>
> OK, so since all are against, I think the proposal (if is not required hard) may be canceled.
>
> Thanks again.
>
> Dmitry.
>
>> /be
>>
>

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
12