surprising behavior in scala when using iterator over characters in file -


i'm getting inconsistent behavior when iterating on characters of text file.

the following script

import io.source val source = source.fromfile("blah") val iter = source.buffered iter.dropwhile(_.iswhitespace) for( c <- iter ) {     println("""char="%c", byte=%d, iswhitespace=%b""".format(c, c.tobyte, c.iswhitespace)) } source.close() 

reads following file (begins 3 spaces, 'a' , second line of text)

   bc de 

outputs following

char=" ", byte=32, iswhitespace=true char=" ", byte=32, iswhitespace=true char=" ", byte=32, iswhitespace=true char="a", byte=97, iswhitespace=false char=" ", byte=10, iswhitespace=true char="b", byte=98, iswhitespace=false char="c", byte=99, iswhitespace=false char=" ", byte=32, iswhitespace=true char="d", byte=100, iswhitespace=false char="e", byte=101, iswhitespace=false char=" ", byte=10, iswhitespace=true 

the dropwhile(_.iswhitespace) didn't drop 3 spaces, , yet c.iswhitespace returns true when iterating in loop after.

can shed light on me? i've opened text file in hex editor, , looks ok me (pure ascii, no utf stuff).

edit: using scala 2.9.2 on ubuntu

edit2: i'm quite confused. following repl on windows 7:

c:\projects\scratch>scala welcome scala version 2.10.2 (java hotspot(tm) 64-bit server vm, java 1.7.0_21). type in expressions have them evaluated. type :help more information.  scala> val = iterator("a", "b", "cde", "f") it: iterator[string] = non-empty iterator  scala> val it2 = it.dropwhile(_.length < 2) it2: iterator[string] = non-empty iterator  scala> println(it.next) cde  scala> println(it2.next) f 

running exact piece of code script instead produces behavior original question (the iterator not modified dropwhile).

in scala, vals immutable objects. once val set, cannot altered. when call iter.dropwhile(_.iswhitespace), new object being created, isn't stored anywhere. if want drop whitespace, should assign iter.dropwhile(_.iswhitespace) new val , call new val in expression.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -