Re: Add an easier method for slicing and stepping strings and arrays

2017-07-26 Thread Isiah Meadows
That, I could support.

On Wed, Jul 26, 2017, 16:38 Alexander Craggs 
wrote:

> In which case,  perhaps change this proposal to .slice(start, end, step)
> ___
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Re: Add an easier method for slicing and stepping strings and arrays

2017-07-24 Thread Jordan Harband
Array slice already allows negative indexes. `arr.slice(-1)[0]` works fine
for a quick and dirty "last item".

On Mon, Jul 24, 2017 at 3:19 PM, Tab Atkins Jr. 
wrote:

> On Sun, Jul 23, 2017 at 12:36 AM, Darien Valentine
>  wrote:
> > Here are some related threads from the past:
> >
> > https://esdiscuss.org/topic/negative-indices-for-arrays
> > https://esdiscuss.org/topic/array-slice-syntax
> > https://esdiscuss.org/topic/javascript-language-feature-idea
> >
> > I think I’ve seen it mentioned a few other times too.
> >
> > The `arr[-1]` syntax is a non-starter I’m afraid, cause it’s just
> property
> > access. An array can already have a property with the key "-1".
> >
> > Personally I’d find it weird for slicing to be singled out for special
> > syntax. It’s just a method. However I’d agree that it’s quite awkward to
> do
> > things like `arr[arr.length - 1]` or `arr.slice().pop()` etc to access
> from
> > the right. I think in one of those threads someone proposed a method like
> > `Array.prototype.nth()` which would accept negative indices, though this
> > creates an odd asymmetry since (presumably) we would not want to use -0
> for
> > the last index.
>
> Yeah, -1 is the last index; it just does one round of "underflow"
> basically.
>
> > In general I think the functionality you’re describing could be useful
> but
> > that it could be served better by adding methods rather than syntax.
>
> Agreed. Slicing via Array#slice() can likely be extended to allow
> negative indexes, and even a third argument for steps to match Python
> (yay for the -1 step value, to get a slice in reverse!).  Adding an
> nth() that just takes a single positive or negative index would be
> nice.  That we still have to do `arr[arr.length -1]` is barbaric. ^_^
>
> (Only downside of negative indexes is that, when using it on
> iterators, you have to consume the entire iterator before you can
> return the desired value.)
>
> ~TJ
> ___
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Re: Add an easier method for slicing and stepping strings and arrays

2017-07-24 Thread Tab Atkins Jr.
On Sun, Jul 23, 2017 at 12:36 AM, Darien Valentine
 wrote:
> Here are some related threads from the past:
>
> https://esdiscuss.org/topic/negative-indices-for-arrays
> https://esdiscuss.org/topic/array-slice-syntax
> https://esdiscuss.org/topic/javascript-language-feature-idea
>
> I think I’ve seen it mentioned a few other times too.
>
> The `arr[-1]` syntax is a non-starter I’m afraid, cause it’s just property
> access. An array can already have a property with the key "-1".
>
> Personally I’d find it weird for slicing to be singled out for special
> syntax. It’s just a method. However I’d agree that it’s quite awkward to do
> things like `arr[arr.length - 1]` or `arr.slice().pop()` etc to access from
> the right. I think in one of those threads someone proposed a method like
> `Array.prototype.nth()` which would accept negative indices, though this
> creates an odd asymmetry since (presumably) we would not want to use -0 for
> the last index.

Yeah, -1 is the last index; it just does one round of "underflow" basically.

> In general I think the functionality you’re describing could be useful but
> that it could be served better by adding methods rather than syntax.

Agreed. Slicing via Array#slice() can likely be extended to allow
negative indexes, and even a third argument for steps to match Python
(yay for the -1 step value, to get a slice in reverse!).  Adding an
nth() that just takes a single positive or negative index would be
nice.  That we still have to do `arr[arr.length -1]` is barbaric. ^_^

(Only downside of negative indexes is that, when using it on
iterators, you have to consume the entire iterator before you can
return the desired value.)

~TJ
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Re: Add an easier method for slicing and stepping strings and arrays

2017-07-23 Thread Dante Federici
This feels like an attempt to get toward python's style of slice
. Which
I'm not opposed to, and would like to hear how it's better than just the
method.
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Re: Add an easier method for slicing and stepping strings and arrays

2017-07-23 Thread Darien Valentine
Here are some related threads from the past:

https://esdiscuss.org/topic/negative-indices-for-arrays
https://esdiscuss.org/topic/array-slice-syntax
https://esdiscuss.org/topic/javascript-language-feature-idea

I think I’ve seen it mentioned a few other times too.

The `arr[-1]` syntax is a non-starter I’m afraid, cause it’s just property
access. An array can already have a property with the key "-1".

Personally I’d find it weird for slicing to be singled out for special
syntax. It’s just a method. However I’d agree that it’s quite awkward to do
things like `arr[arr.length - 1]` or `arr.slice().pop()` etc to access from
the right. I think in one of those threads someone proposed a method like
`Array.prototype.nth()` which would accept negative indices, though this
creates an odd asymmetry since (presumably) we would not want to use -0 for
the last index.

In general I think the functionality you’re describing could be useful but
that it could be served better by adding methods rather than syntax.
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Add an easier method for slicing and stepping strings and arrays

2017-07-22 Thread Alexander Craggs
I always found the Python methodology of selecting characters from strings (or 
items from arrays) to be the most natural I've found among a number of language 
solutions.  Was considering whether adding the syntax would be beneficial for 
JavaScript.  For those who do not know, the syntax is as so:

```js
const str = 'example'
const arr = ['e', 'x', 'a', 'm', 'p', 'l', 'e']
// Single elements are selected as in JS:
str[1] // 'x'
arr[2] // 'a'
// Multiple elements are selected by providing two numbers, delimited by a colon
// The first number is inclusive, the last number is exclusive
str[1:3] // 'xa'
arr[3:6] // ['m', 'p', 'l']
// These numbers can also be negative, resulting in indexing from the end of 
the string/array
str[-1] // 'e'
str[-3:-1] // 'pl'
arr[-5:-3] // ['a', 'm']
arr[0:-5] // ['e', 'x']
// There is also a third number, which steps through the selection chosen by 
the first two
str[0:5:2] // 'eap'
// This number can be negative, which results in stepping backwards through the 
string/array
str[::-1] // 'elpmaxe'
// An empty item represents one of the following based upon it's position: 
[0:length:1]
```

The most official documentation I could find on this topic is from the Python 
tutorial [https://docs.python.org/3.6/tutorial/introduction.html#strings], a 
more useful reference however might be the Stack Overflow post on the topic 
[https://stackoverflow.com/questions/509211/explain-slice-notation].___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss