Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no difference between [..] and [...] for array?

Tags:

coffeescript

Edit: made a github issue, it got closed a day later by jashkenas. So the takeaway is "working as intended" essentially.

coffee> arr
[ 0,
  1,
  2,
  3,
  'A',
  'K' ]
coffee> arr[...]
[ 0,
  1,
  2,
  3,
  'A',
  'K' ]
coffee> arr[..]
[ 0,
  1,
  2,
  3,
  'A',
  'K' ]

According to the docs, those should be different.

With two dots (3..6), the range is inclusive (3, 4, 5, 6); with three dots (3...6), the range excludes the end (3, 4, 5).

The two slice statements that are produced are the same. Seems to me that .. should produce .slice(0) and ... should produce .slice(0, -1) Am I missing something or seeing a bug?

1.7.1

like image 512
jcollum Avatar asked Nov 27 '25 07:11

jcollum


1 Answers

The documentation then goes on to say:

Slices indices have useful defaults. An omitted first index defaults to zero and an omitted second index defaults to the size of the array.

This is consistent with what you're seeing. The length of your array is 6 so:

  • [..] is equivalent to [0..6] which would compile to .slice(0,7)
  • [...] is equivalent to [0...6] which would compile to .slice(0,6)

With an array of length 6, both .slice(0,6) and .slice(0,7) return all elements and so both are equivalent to .slice(0), which is what both [..] and [...] compile to.

What you are expecting would be the case if an omitted second index defaulted to the size of the array minus 1, but this is not the case.

like image 74
nicolaskruchten Avatar answered Dec 02 '25 06:12

nicolaskruchten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!