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

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.
主站蜘蛛池模板: 永定县| 高密市| 卢龙县| 汝州市| 六枝特区| 德庆县| 遂溪县| 望奎县| 长沙市| 鹤山市| 健康| 辛集市| 台安县| 新巴尔虎右旗| 华安县| 镶黄旗| 象州县| 茌平县| 丹凤县| 平乡县| 五华县| 大余县| 武夷山市| 柞水县| 永泰县| 阿图什市| 美姑县| 饶河县| 甘谷县| 金湖县| 玉溪市| 塔城市| 会同县| 疏附县| 河北省| 娱乐| 金乡县| 嘉义县| 庄河市| 汉沽区| 银川市|