區(qū)間表達(dá)式由rangeTo
函數(shù)和..
操作符輔以in
和!in
構(gòu)成。可以為任意可比較的類型定義區(qū)間拳喻,但是對于整形這種原生類型,區(qū)間的實(shí)現(xiàn)已經(jīng)被優(yōu)化了猪腕。如下是一個使用區(qū)間的例子:
if (i in 1..10) { // equivalent of 1 <= i && i <= 10
println(i)
}
整形區(qū)間(IntRange, LongRange, CharRange
)有一個額外特性:它們可以被迭代冗澈。編譯器負(fù)責(zé)將其轉(zhuǎn)換為類似Java的基于索引的for循環(huán)而無額外開銷。
for (i in 1..4) print(i) // prints "1234"
for (i in 4..1) print(i) // prints nothing
如果你想倒序迭代數(shù)字呢陋葡?也肯簡單亚亲,可以使用標(biāo)準(zhǔn)庫中定義的downTo()
函數(shù):
for (i in 4 downTo 1) print(i) // prints "4321"
能否以不為1的任意步長迭代數(shù)字?當(dāng)然可以脖岛,step()
函數(shù)可以實(shí)現(xiàn)該功能:
for (i in 1..4 step 2) print(i) // prints "13"
for (i in 4 downTo 1 step 2) print(i) // prints "42"
如果要創(chuàng)建一個不包含結(jié)束元素的區(qū)間朵栖,則可以使用unti
l函數(shù):
for (i in 1 until 10) { // i in [1, 10), 10 is excluded
println(i)
}
它如何工作(How it works)
區(qū)間實(shí)現(xiàn)了庫中的一個公共接口:ClosedRange<T>
。
這個ClosedRange<T>
是為可比較類型定義的柴梆,在數(shù)學(xué)意義上表示一個閉區(qū)間陨溅。它有兩個端點(diǎn)start
和endInclusive
,并且這兩個端點(diǎn)都被區(qū)間包含绍在。區(qū)間的主要操作是contains
门扇,通常使用in
或!in
的形式。
整形數(shù)列(IntProgression, LongProgression, CharProgression
)表示一個等差數(shù)列偿渡。數(shù)列由首元素first
臼寄、末尾元素last
和非0的公差step
確定。末尾元素last總會被迭代命中溜宽,除非該數(shù)列是空的吉拳。
數(shù)列是Iterable<N>
的子類型,其中N可以是Int
适揉,Long
或Char
留攒,因此數(shù)列可以被用于for
循環(huán)和像map
煤惩,filter
等函數(shù)中。數(shù)列的迭代相當(dāng)于Java/JavaScript
中的基于索引的for循環(huán):
for (int i = first; i != last; i += step) {
// ...
}
對于整數(shù)類型炼邀,..
操作符創(chuàng)建了一個同時實(shí)現(xiàn)ClosedRange<T>
和*Progression
的對象魄揉。例如,IntRange
實(shí)現(xiàn)了ClosedRange<Int>
接口拭宁,且繼承自IntProgression
洛退,因此IntRange
可以使用所有定義在IntProgression
中的操作。downTo()
和step()
函數(shù)的結(jié)果總是一個*Progression
杰标。
數(shù)列由在其伴生對象中定義的fromClosedRange
函數(shù)構(gòu)造:
IntProgression.fromClosedRange(start, end, step)
數(shù)列的last
元素的計算方法是:若step是正數(shù)兵怯,則計算不大于end值的最大值;若step是負(fù)數(shù)在旱,則計算不小于end值的最小值摇零;且最大值和最小值需要滿足:(last - first) % step == 0
使用函數(shù)(Utility functions)
rangeTo()
整形類型的rangeTo()
操作符只是調(diào)用*Range
類的構(gòu)造器,如:
class Int {
//...
operator fun rangeTo(other: Long): LongRange = LongRange(this, other)
//...
operator fun rangeTo(other: Int): IntRange = IntRange(this, other)
//...
}
浮點(diǎn)數(shù)(Double,Float
)沒有定義rangeTo
操作符桶蝎,但是由標(biāo)準(zhǔn)庫提供了一個支持泛型的Comparable
來代替:
public operator fun <T: Comparable<T>> T.rangeTo(that: T): ClosedRange<T>
由此函數(shù)返回的區(qū)間不能用于迭代驻仅。
downTo()
擴(kuò)展函數(shù)downTo()
是為整數(shù)類型定義的,這有兩個例子:
fun Long.downTo(other: Int): LongProgression {
return LongProgression.fromClosedRange(this, other.toLong(), -1L)
}
fun Byte.downTo(other: Int): IntProgression {
return IntProgression.fromClosedRange(this.toInt(), other, -1)
}
reversed()
擴(kuò)展函數(shù)reversed()
是為每一個*Progression
定義的登渣,用于返回一個倒序序列:
fun IntProgression.reversed(): IntProgression {
return IntProgression.fromClosedRange(last, first, -step)
}
step()
擴(kuò)展函數(shù)step()
是為每一個*Progression
定義的噪服,返回的數(shù)列的步進(jìn)值(該函數(shù)的參數(shù))已經(jīng)被修改。步進(jìn)值要求總是正的胜茧,因此該函數(shù)不會改變迭代的方向:
fun IntProgression.step(step: Int): IntProgression {
if (step <= 0) throw IllegalArgumentException("Step must be positive, was: $step")
return IntProgression.fromClosedRange(first, last, if (this.step > 0) step else -step)
}
fun CharProgression.step(step: Int): CharProgression {
if (step <= 0) throw IllegalArgumentException("Step must be positive, was: $step")
return CharProgression.fromClosedRange(first, last, if (this.step > 0) step else -step)
}
注意:為了保證(last - first) % step == 0
的成立粘优,該函數(shù)返回的數(shù)列的末尾值可能與原數(shù)列的末尾值不同,例子如下:
(1..12 step 2).last == 11 // progression with values [1, 3, 5, 7, 9, 11]
(1..12 step 3).last == 10 // progression with values [1, 4, 7, 10]
(1..12 step 4).last == 9 // progression with values [1, 5, 9]