Skip to content

Latest commit

 

History

History
150 lines (121 loc) · 4.82 KB

ranges.md

File metadata and controls

150 lines (121 loc) · 4.82 KB
layout title
reference
範囲(Range)と数列(Progression)

範囲(Range)と数列(Progression)

Kotlinはある範囲に渡る値を.rangeTo()関数 と.rangeUntil()関数で簡単に作る事が出来ます。 これらの関数はkotlin.rangesパッケージにあります。

  • 終端を含む範囲を作成するには、..演算子を使って.rangeTo()関数を呼びます
  • 終端を含まない範囲を作成するには、..<演算子を使って.rangeUntil()関数を呼びます

例を挙げましょう:

{% capture close-open-range %} fun main() { //sampleStart // 終端を含む範囲 println(4 in 1..4) // true

// 終端を含まない範囲
println(4 in 1..<4)
// false

//sampleEnd } {% endcapture %} {% include kotlin_quote.html body=close-open-range %}

範囲はforループでその上に渡ってイテレートしたい時に特に便利です:

{% capture range-for %} fun main() { //sampleStart for (i in 1..4) print(i) // 1234 //sampleEnd } {% endcapture %} {% include kotlin_quote.html body=range-for %}

数字を逆順にイテレートしたければ、..の代わりにdownTo関数を使います.

{% capture range-downto %} fun main() { //sampleStart for (i in 4 downTo 1) print(i) // 4321 //sampleEnd } {% endcapture %} {% include kotlin_quote.html body=range-downto %}

数字を1以外の間隔でイテレートする事も可能です。 それにはstep関数を使います。

{% capture range-step %} fun main() { //sampleStart for (i in 0..8 step 2) print(i) println() // 02468 for (i in 0..<8 step 2) print(i) println() // 0246 for (i in 8 downTo 0 step 2) print(i) // 86420 //sampleEnd } {% endcapture %} {% include kotlin_quote.html body=range-step %}

数列

(訳注:Progression)

整数の型、IntLongCharの範囲は、等差数列として扱う事が出来ます。 Kotlinでは、これらの数列は特別な型として定義されます:IntProgressionLongProgressionCharProgressionです。

数列には3つの重要なプロパティがあります: first要素、last要素、そして非ゼロのstepです。 最初の要素はfirstです。以後それに続く要素は前の要素にstepを足したものです。 正のステップの数列をイテレートする事は、Java/JavaScriptのインデックスを使ったforループと同等です。

for (int i = first; i <= last; i += step) {
  // ...
}

範囲をイテレートする事で暗黙のうちに数列を作る場合、 その数列のfirstlastは範囲の両終端で、stepは1となっています。

{% capture range-as-progressiono %} fun main() { //sampleStart for (i in 1..10) print(i) // 12345678910 //sampleEnd } {% endcapture %} {% include kotlin_quote.html body=range-as-progressiono %}

カスタムな差分による数列を定義したければ、step関数を範囲に使います。

{% capture range-step-progression %} fun main() { //sampleStart for (i in 1..8 step 2) print(i) // 1357 //sampleEnd } {% endcapture %} {% include kotlin_quote.html body=range-step-progression %}

数列のlastの要素は以下のように計算されます:

  • 正のステップの時:以下を満たす中で終端を越えない最大の値 (last - first) % step == 0
  • 負のステップの時:以下を満たす中で終端の値を下回らない最小の値 (last - first) % step == 0

つまり、last要素は指定された値と必ずしも等しくはなりません。

{% capture last-value-calc %} fun main() { //sampleStart for (i in 1..9 step 3) print(i) // last要素は7 // 147 //sampleEnd } {% endcapture %} {% include kotlin_quote.html body=last-value-calc %}

数列はIterable<N>を実装しています、ここでNはそれぞれIntLongCharです。 だから、mapfilterなどの様々なコレクションの関数で使う事が出来ます。

{% capture progression-col %} fun main() { //sampleStart println((1..10).filter { it % 2 == 0 }) // [2, 4, 6, 8, 10] //sampleEnd } {% endcapture %} {% include kotlin_quote.html body=progression-col %}