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

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.
主站蜘蛛池模板: 大竹县| 蒙自县| 全椒县| 四子王旗| 吉林省| 临猗县| 贵德县| 南江县| 奉贤区| 桃园县| 嵊州市| 巴塘县| 柘荣县| 宿迁市| 顺义区| 钟祥市| 香港| 凤翔县| 洞口县| 沙雅县| 鹤岗市| 灌云县| 临湘市| 榆中县| 白山市| 蒙阴县| 忻州市| 丰城市| 延川县| 延安市| 泸州市| 夹江县| 屏东县| 白沙| 长沙县| 区。| 右玉县| 砀山县| 尤溪县| 江门市| 阳东县|