scala quirky in this while loop code -
yesterday, piece of code caused me headache. fixed reading file line line. ideas ?
the while loop never seems executed though no of lines in file greater 1.
val lines = source.fromfile( new file("file.txt") ).getlines; println( "total lines:"+lines.size ); var starti = 1; while( starti < lines.size ){ val nexti = math.min( starti + 10, lines.size ); println( "batch ("+starti+", "+nexti+") total:" + lines.size ) val linessub = lines.slice(starti, nexti) //do linessub starti = nexti }
this indeed tricky, , it's bug in iterator
. getlines
returns iterator
proceeds lazily. seems happen if ask lines.size
iterator goes through whole file count lines. afterwards, it's "exhausted":
scala> val lines = io.source.fromfile(new java.io.file("....txt")).getlines lines: iterator[string] = non-empty iterator scala> lines.size res4: int = 15 scala> lines.size res5: int = 0 scala> lines.hasnext res6: boolean = false
you see, when execute size
twice, result zero.
there 2 solutions, either force iterator 'stable', lines.toseq
. or forget size
, "normal" iteration:
while(lines.hasnext) { val linessub = lines.take(10) println("batch:" + linessub.size) // linessub }
Comments
Post a Comment