Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 在大于向量大小的索引处拆分_Scala - Fatal编程技术网

Scala 在大于向量大小的索引处拆分

Scala 在大于向量大小的索引处拆分,scala,Scala,为什么这个表达式不抛出索引越界异常 scala> Vector(1,2,3,4).splitAt(50000000) res2: (Vector(1, 2, 3, 4),Vector()) 从splitAt的scaladoc: /** Splits this $coll into two at a given position. * Note: `c splitAt n` is equivalent to (but possibly more efficient than) *

为什么这个表达式不抛出索引越界异常

scala> Vector(1,2,3,4).splitAt(50000000)
res2: (Vector(1, 2, 3, 4),Vector())

splitAt
的scaladoc:

/** Splits this $coll into two at a given position.
  *  Note: `c splitAt n` is equivalent to (but possibly more efficient than)
  *         `(c take n, c drop n)`.
  *  $orderDependent
  *
  *  @param n the position at which to split.
  *  @return  a pair of ${coll}s consisting of the first `n`
  *           elements of this $coll, and the other elements.
  */
def splitAt(n: Int): (Repr, Repr)
用于
拖放的Scaladoc

/** Selects all elements except first ''n'' ones.
  *  $orderDependent
  *  @param  n    the number of elements to drop from this $coll.
  *  @return a $coll consisting of all elements of this $coll except the first `n` ones, or else the
  *          empty $coll, if this $coll has less than `n` elements.
  */
def drop(n: Int): Repr
对于
采取以下措施:

/** Selects first ''n'' elements.
  *  $orderDependent
  *  @param  n    the number of elements to take from this $coll.
  *  @return a $coll consisting only of the first `n` elements of this $coll,
  *          or else the whole $coll, if it has less than `n` elements.
  */
def take(n: Int): Repr

如您所见,这些方法的设计目的是不抛出任何异常

按照Java标准,不抛出异常看起来很奇怪,但出于某种原因,我更喜欢这种方式。我能想到的不抛出异常的唯一原因是,它们希望保持某种引用透明度,从而保持函数的纯洁性。@I.K.这更像是“scala”方式,以避免在“普通”情况下抛出异常。通常在
选项或
中用包装结果替换抛出异常。在
splitAt
的情况下,可以为任何参数返回合理的结果,而无需更改或包装结果类型。@I.K.-大多数高阶集合方法的原则是尽可能做好工作,而不是将错误检查与应用程序逻辑混合在一起,检查不严格需要的细节。大多数时候,如果你尽可能做最明智的事情,整个事情都会顺利进行,而不必担心边界问题。因为图书馆是为你做这件事的,通过理智,一整类恼人的错误就消失了。(取而代之的是出现的类,尽管你不使用它,但它的集合如此之大真的很重要,相比之下,它是很小的。)@RexKerr,是的,敏感性确实存在,但正如你所知,在现实世界中编程失误确实会发生。不过,这有点前后矛盾。考虑这一点:<代码> ValV=向量(1,2,3,4/<代码>),然后<代码> V(4)< /代码>,结果是代码> java. Lang.NoxOutOfFunsExpRe: 4 < /COD>。为什么在一种情况下这样做,而不是在其他情况下?