官术网_书友最值得收藏!

Range as Sequence

When using Range<Int> or ClosedRange<Int>, in addition to others, the Standard Library provides conditional conformance to the Sequence type, as follows:

extension Range: Sequence where Bound: Strideable, Bound.Stride : SignedInteger {
public typealias Element = Bound
public typealias Iterator = IndexingIterator<Range<Bound>>
}

Extract from Swift Core source code (https://github.com/apple/swift/blob/master/stdlib/public/core/Range.swift).

This means that you can use this range as the source for iterating on it, as shown in the following example:

let doubles = (1..<10).map { $0 * 2 } // [2,4,6,8,10,12,14,16,18]

for i in 1..<10 {
// do something with i
}
Because the Sequence conformance is provided as a conditional conformance, you can also define your own types that will allow for transforming Ranges to Sequences.
主站蜘蛛池模板: 迭部县| 陈巴尔虎旗| 斗六市| 九龙城区| 平江县| 屏东市| 榆树市| 勃利县| 定安县| 朝阳县| 金寨县| 诸城市| 霞浦县| 白城市| 乌拉特中旗| 诸城市| 海林市| 乐平市| 乐平市| 平度市| 华容县| 阜南县| 临沂市| 乌兰浩特市| 枝江市| 武宣县| 辽阳县| 勐海县| 浪卡子县| 万宁市| 昆山市| 稻城县| 胶州市| 萨迦县| 霞浦县| 琼结县| 广州市| 道真| 抚州市| 榆中县| 深圳市|