Scala 的 Range 类的独占结尾 [英] Exclusive end in Scala's Range class

查看:44
本文介绍了Scala 的 Range 类的独占结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Scala 文档,该方法Range.end,它返回范围的唯一结尾".那么为什么它为 tountil 符号返回相同的值呢?例如:

According to the Scala documentation for the method Range.end, it returns "the exclusive end of the range." So why does it return the same value for both the to and the until notations? For example:

Welcome to Scala version 2.9.2 (Java HotSpot(TM) Server VM, Java 1.7.0).
Type in expressions to have them evaluated.
Type :help for more information.

scala> (1 to 10).end
res0: Int = 10

scala> (1 until 10).end
res1: Int = 10

不应该res0 == 11吗?

推荐答案

tountil 导致相关但不同的 Range 类:Range.InclusiveRange,分别;Range.Inclusive IS-A Range.

to and until result in related but different Range classes: Range.Inclusive and Range, respectively; Range.Inclusive IS-A Range.

  • isInclusive 将区分两种类类型.

end 或上限,在排除或包含范围的上下文中解释.上限是范围规范中提供的第二个数字,在原始问题的两个示例案例中都是 10.是的,如果范围从高到低(即 1 到 -10),这个上限"可能小于下限".

end, or the upper bound, is interpreted in the context of the range being exclusive or inclusive. The upper bound is the second number provided in the range specification, which was 10 in both of the sample cases in the original question. And yes, this "upper bound" may be less than the "lower bound", if the range goes from high to low (i.e. 1 until -10).

last 将返回范围内的最后一个值,这可能是您试图从 end 获取的值.

last will return the last value in the range, which may be what you were trying to obtain from end.

这篇关于Scala 的 Range 类的独占结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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