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

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.
主站蜘蛛池模板: 台南县| 集安市| 东方市| 磐石市| 万安县| 仁化县| 宁乡县| 皋兰县| 乌什县| 朝阳区| 陈巴尔虎旗| 雷州市| 永定县| 临海市| 清徐县| 石柱| 中方县| 六安市| 水富县| 南汇区| 美姑县| 左云县| 建平县| 奉节县| 中超| 临沂市| 会泽县| 合江县| 会东县| 沙田区| 耿马| 凤山市| 精河县| 肥乡县| 天祝| 化德县| 皋兰县| 轮台县| 彭州市| 张家界市| 屯昌县|