什么是“间隔"?在快速范围内? [英] What are "intervals" in swift ranges?

查看:35
本文介绍了什么是“间隔"?在快速范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有 3 种类型的范围:范围、步幅和间隔.

I know there are 3 types of ranges: Range, Strides and Intervals.

var closed:ClosedInterval = 1.2...5.0

var half_open:HalfOpenInterval = 1.2..<5.0

swift 中的间隔是什么?它们的使用示例是什么?

What are intervals in swift? and what is one example of their use?

http://en.wikipedia.org/wiki/Interval_(mathematics)

这是 beta 5 xcode 6 发行说明所说的:

This is what the beta 5 xcode 6 release notes says:

• 可比较值上的间隔,可以有效地检查包含情况.间隔是用于 switch 语句和 ~= 运算符中的模式匹配.

• Intervals over Comparable values, which can efficiently check for containment. Intervals are used for pattern matching in switch statements and by the ~= operator.

推荐答案

从 Swift 3(使用 Xcode 8)开始,Interval 类型不再存在.现在 Range<T> 类型家族包括了之前的 range 和 interval 类型的功能,另外还符合 集合类型和索引的新模型.

As of Swift 3 (with Xcode 8), the Interval types are no more. Now the family of Range<T> types include the functionality of both the former range and interval types, and additionally conform to the new model for collection types and indices.

在 Swift 2.x 及更早版本中...范围用于迭代,而间隔用于模式匹配.

In Swift 2.x and older... Ranges are for iterating, and Intervals are for pattern matching.

func testNum(num: Int) {
    let interval: HalfOpenInterval = 0..<10
    let range = 10..<20
    switch num {
    case interval:    // this works
        break
    case range:       // error "does not conform to protocol IntervalType"
        break
    default:
        break
    }
}

Range 类型针对生成在范围内递增的值进行了优化,并适用于可计数和递增的类型.

A Range type is optimized for generating values that increment through the range, and works with types that can be counted and incremented.

Interval 类型被优化用于测试给定值是否在区间内.它适用于不一定需要递增概念的类型,并提供诸如将一个范围限制到另一个范围的操作(例如 (0..<10).clamp(5..<15) 产生 5..<10),这对复杂的模式匹配很有用.

An Interval type is optimized for testing whether a given value lies within the interval. It works with types that don't necessarily need a notion of incrementing, and provides operations like clamping one range to another (e.g. (0..<10).clamp(5..<15) yields 5..<10) that are useful for complex pattern matching.

因为 ..<... 运算符各有两种形式——一种返回 Range,另一种返回Interval--type 推断会根据上下文自动使用正确的推断.所以,如果你在 switch 语句的 case 标签中写 0..<10,Swift 会自动构造一个 HalfOpenInterval 因为 switch 语句需要 Interval 类型.

Because the ..< and ... operators have two forms each--one that returns a Range and one that returns an Interval--type inference automatically uses the right one based on context. So, if you write 0..<10 in a case label of a switch statement, Swift automatically constructs a HalfOpenInterval because a switch statement requires an Interval type.

~= 运算符是一种在没有 switch 语句的情况下对间隔进行测试的方法.写interval ~= value 等价于interval.contains(value).

The ~= operator is a way to do one test on an interval without a switch statement. Writing interval ~= value is equivalent to interval.contains(value).

值得注意的是,通过查看标准库接口及其注释,您可以发现很多这样的东西:在操场上写一个类似 HalfOpenInterval 的类型名称,然后单击命令跳转到它的定义.

It's worth noting that you can find out many of these things by looking at the standard library interface and its comments: write a type name like HalfOpenInterval in a playground, then command-click to jump to its definition.

这篇关于什么是“间隔"?在快速范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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