Created
July 24, 2011 23:58
-
-
Save kmizu/1103265 to your computer and use it in GitHub Desktop.
Stream usage not to consume O(n) memory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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