数组的[..]和[...]之间没有区别? [英] no difference between [..] and [...] for array?

查看:198
本文介绍了数组的[..]和[...]之间没有区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:发布了github问题,已关闭一天后由jashkenas。所以外卖是基本上按预期工作。

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' ]

根据docs ,那些应该是不同的。

According to the docs, those should be different.


有两个点(3..6),范围包括在内(3,4,5,6);有三个点(3 ... 6),范围不包括结束(3,4,5)。

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).

产生的两个 slice 语句是相同的。看起来 .. 应该产生 .slice(0) ... 应该产生 .slice(0,-1)我缺少什么或看到错误?

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

推荐答案

文档然后继续说:


切片索引具有有用的默认值。省略的第一个索引将
默认为零,而省略的第二个索引默认为数组的大小。

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

与你看到的。数组的长度为6,因此:

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


  • [..] [0..6] ,它将编译为 .slice(0,7)

  • [...] 相当于 [0 ... 6] .slice(0,6)

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

.slice(0,6) .slice(0,7)返回所有元素,到 .slice(0),这是 [..] ..] 编译为

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.

如果省略的第二个索引默认为数组大小减1 ,但事实并非如此。

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.

这篇关于数组的[..]和[...]之间没有区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆