在Swift中,什么是'progression'?
In Swift, what is a 'progression'?
根据 Swift 语言指南 中的 Control Flow 部分,
The for-in
loop performs a set of statements for each item in a range, sequence, collection, or progression.
我很确定我知道其中三个是什么:
- 范围:用range operators、
...
或..<
定义的东西
- sequence:符合
SequenceType
协议的东西(文档不是很明显,但是很多人都有 reverse-engineered 它)
- 集合:Swiftcollection types中的任一个,即
Array
和Dictionary
(我注意到 #3 可能是多余的,因为 Array
和 Dictionary
似乎都符合 SequenceType
。)
但是 "progression" 是什么?是第四件事,还是作者只是罗嗦?
ETA: 我看到还有一个 CollectionType
协议,所以这解释了 #3.
您可能正在寻找的是这种循环(for in stride)
for i in stride(from: 1, to: 10, by: 2) {
println(i)
}
这是
的新语法替换
for var i = 1; i < 10; i += 2 {
println(i)
}
除了 for-in 文档之外,我第一次看到 "progression" 是在 swift 框架的注释中,其中定义了 stride 方法。
func stride<T : Strideable>(from start: T, to end: T, by stride: T.Stride) -> StrideTo<T>
Return the sequence of values (start
, start + stride
, start + stride + stride
, ... last) where last is the last value in the progression that is less than end
.
func stride<T : Strideable>(from start: T, through end: T, by stride: T.Stride) -> StrideThrough<T>
Return the sequence of values (start
, start + stride
, start + stride + stride
, ... last) where last is the last value in the progression less than or equal to end
. Note:: There is no guarantee that end
is an element of the sequence.
简而言之,"progression" 指的是 Strideable
协议,类似于 "collection" 指的是 CollectionType
协议 以及符合它的 类 和结构。
数字类型(Int、Double、Float、UnsafePointer、Bit 等)倾向于符合此协议,因此它们可能 incremented/decremented 用于 for in
循环。找到 Strideable
协议的完整继承图 here。
这道题的答案在于数学。
数学有两种级数。
Arithmetic Progression and Geometric Progression
In mathematics, an arithmetic progression (AP) or arithmetic sequence
is a sequence of numbers such that the difference between the
consecutive terms is constant. For instance, the sequence 5, 7, 9, 11,
13, 15 … is an arithmetic progression with common difference of 2.
In mathematics, a geometric progression, also known as a geometric
sequence, is a sequence of numbers where each term after the first is
found by multiplying the previous one by a fixed, non-zero number
called the common ratio. For example, the sequence 2, 6, 18, 54, ...
is a geometric progression with common ratio 3. Similarly 10, 5, 2.5,
1.25, ... is a geometric sequence with common ratio 1/2.
根据 Swift 语言指南 中的 Control Flow 部分,
The
for-in
loop performs a set of statements for each item in a range, sequence, collection, or progression.
我很确定我知道其中三个是什么:
- 范围:用range operators、
...
或..<
定义的东西
- sequence:符合
SequenceType
协议的东西(文档不是很明显,但是很多人都有 reverse-engineered 它) - 集合:Swiftcollection types中的任一个,即
Array
和Dictionary
(我注意到 #3 可能是多余的,因为 Array
和 Dictionary
似乎都符合 SequenceType
。)
但是 "progression" 是什么?是第四件事,还是作者只是罗嗦?
ETA: 我看到还有一个 CollectionType
协议,所以这解释了 #3.
您可能正在寻找的是这种循环(for in stride)
for i in stride(from: 1, to: 10, by: 2) {
println(i)
}
这是
的新语法替换for var i = 1; i < 10; i += 2 {
println(i)
}
除了 for-in 文档之外,我第一次看到 "progression" 是在 swift 框架的注释中,其中定义了 stride 方法。
func stride<T : Strideable>(from start: T, to end: T, by stride: T.Stride) -> StrideTo<T>
Return the sequence of values (
start
,start + stride
,start + stride + stride
, ... last) where last is the last value in the progression that is less thanend
.
func stride<T : Strideable>(from start: T, through end: T, by stride: T.Stride) -> StrideThrough<T>
Return the sequence of values (
start
,start + stride
,start + stride + stride
, ... last) where last is the last value in the progression less than or equal toend
. Note:: There is no guarantee thatend
is an element of the sequence.
简而言之,"progression" 指的是 Strideable
协议,类似于 "collection" 指的是 CollectionType
协议 以及符合它的 类 和结构。
数字类型(Int、Double、Float、UnsafePointer、Bit 等)倾向于符合此协议,因此它们可能 incremented/decremented 用于 for in
循环。找到 Strideable
协议的完整继承图 here。
这道题的答案在于数学。
数学有两种级数。
Arithmetic Progression and Geometric Progression
In mathematics, an arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15 … is an arithmetic progression with common difference of 2.
In mathematics, a geometric progression, also known as a geometric sequence, is a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio. For example, the sequence 2, 6, 18, 54, ... is a geometric progression with common ratio 3. Similarly 10, 5, 2.5, 1.25, ... is a geometric sequence with common ratio 1/2.