Takuya71 のぶろぐ

外資系ソフトウェア会社で働いてます、認定スクラムマスター

Scala 2.10 で Stream.const が無くなっていたこと

結論

Scala 2.10 では Stream.const の代わりに Stream.continually を使えます。

発端

Scala 2.9.2 の頃に書いたプログラムの中で Stream.const を使ってました。
同じエレメントで無限配列を作りたかったので 使用していたのですが、
このプログラムを Scala 2.10.1 に上げようとした時に const が使えないのが発覚!!!

あれれ 状態になりました。

scala> Stream.const("hoge")
<console>:8: error: value const is not a member of object scala.collection.immutable.Stream
              Stream.const("hoge")
                     ^

いったん const を定義して逃げていたのですが、

勘違い発覚

ソースみたら 2.9.2 の頃すでに deprecated になっていて、
さらに continually も無いと言ったのは 私が typo していて無いと勘違い。。。

continually は使えます

scala> Stream.continually("hoge")
res5: scala.collection.immutable.Stream[String] = Stream(hoge, ?)

scala> res5 take 10 force
warning: there were 1 feature warning(s); re-run with -feature for details
res8: scala.collection.immutable.Stream[String] = Stream(hoge, hoge, hoge, hoge, hoge, hoge, hoge, hoge, hoge, hoge)

ついでにわかったこと

iterate も便利かも
ソースでは

def iterateA(f: A => A): Stream[A]

override def iterateA(f: A => A): Stream[A]

と定義されているので

scala> Stream.iterate(1){x => x} take 10 force
warning: there were 1 feature warning(s); re-run with -feature for details
res11: scala.collection.immutable.Stream[Int] = Stream(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

同じことを

scala> Stream.iterate(1, 10){x => x} force
warning: there were 1 feature warning(s); re-run with -feature for details
res14: scala.collection.immutable.Stream[Int] = Stream(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

としても出来る。
他にも

scala> Stream.iterate(1, 10){x => x * 2} force
warning: there were 1 feature warning(s); re-run with -feature for details
res18: scala.collection.immutable.Stream[Int] = Stream(1, 2, 4, 8, 16, 32, 64, 128, 256, 512)

とかも 可能

その他 from ってのもある
例えば Stream.from(3) とすると 3,4,5,6,.... という無限リストができる。

scala> Stream.from(1) take 100 force
warning: there were 1 feature warning(s); re-run with -feature for details
res5: scala.collection.immutable.Stream[Int] = Stream(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)

間隔も指定できて from(3,5) だと 3,8,13,18.... という具合。
10個間隔だと

scala> Stream.from(1,10) take 100 force
warning: there were 1 feature warning(s); re-run with -feature for details
res6: scala.collection.immutable.Stream[Int] = Stream(1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 201, 211, 221, 231, 241, 251, 261, 271, 281, 291, 301, 311, 321, 331, 341, 351, 361, 371, 381, 391, 401, 411, 421, 431, 441, 451, 461, 471, 481, 491, 501, 511, 521, 531, 541, 551, 561, 571, 581, 591, 601, 611, 621, 631, 641, 651, 661, 671, 681, 691, 701, 711, 721, 731, 741, 751, 761, 771, 781, 791, 801, 811, 821, 831, 841, 851, 861, 871, 881, 891, 901, 911, 921, 931, 941, 951, 961, 971, 981, 991)

Stream も便利ですね。