我正在尝试查找行集合中最长行的长度。reduceLeft为什么会抱怨类型不匹配?
val lines = Source.fromFile(args(0)).getLines() //a collection of strings
val longestLine = lines.reduceLeft((a,b) =>
if(a.length > b.length) a.length else b.length)
但是,这将导致以下错误:
/home/jesvin/dev/scala/readfile.scala:11: error: type mismatch;
found : Int
required: String
if(a.length > b.length) a.length else b.length)
^
/home/jesvin/dev/scala/readfile.scala:11: error: type mismatch;
found : Int
required: String
if(a.length > b.length) a.length else b.length)
^
two errors found
我试图在某些地区仍有明确的返回语句和类型转换,但没有奏效。
我使用reduceLeft
错误吗?
我在第三章的末尾写了这个程序,却没有看原版,找到了我的学习空白。原来,在这种情况下,我对减少和折叠操作不够了解。 – aitchnyu 2012-03-31 18:15:16