Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created July 24, 2011 23:58
Show Gist options
  • Save kmizu/1103265 to your computer and use it in GitHub Desktop.
Save kmizu/1103265 to your computer and use it in GitHub Desktop.
Stream usage not to consume O(n) memory.
object RightUsageOfStream {
def main(args: Array[String]) {
//Stream.continuallyで作成したStreamのheadをローカル変数で
//参照していないので入力サイズに比例したメモリ領域を食わない
//ただし、readLine()を生で使って巨大ファイル読むと時間かかるので注意
Stream.continually(readLine()) foreach {line =>
//nothing to do
}
//OutOfMemoryErrorで死ぬ例(標準入力から読み込んだサイズが大きい場合)
/*
val head = Stream.continually(readLine())
head map (":" +_) foreach {line =>
//nothing to do
}
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment