- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 120字
- 2021-07-02 14:45:09
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.
推薦閱讀
- MySQL基礎教程
- Learn Unity ML-Agents:Fundamentals of Unity Machine Learning
- Dependency Injection with AngularJS
- INSTANT Cytoscape Complex Network Analysis How-to
- 大數據技術入門
- 科研統計思維與方法:SPSS實戰
- 數據庫應用系統開發實例
- 新手學會計(2013-2014實戰升級版)
- SQL Server 2012實施與管理實戰指南
- 大數據分析:R基礎及應用
- Cognitive Computing with IBM Watson
- 大數據用戶行為畫像分析實操指南
- Oracle數據庫性能優化的藝術
- AutoCAD基礎與應用精品教程(2008版)
- 零基礎學SQL