Functional style for this Scala code
A friend of mine is learning Scala and wrote this simple code to keep track of the longest line in a file:
val longest = (filename:String) => {
val is = new FileInputStream(filename)
val buf = new Array[Byte](1024)
var long开发者_C百科est=0 //keep track of the longest line
var lastPos=0
var read=0
try {
read = is.read(buf)
while (read > 0) {
for (i<-0 until read) {
if (buf(i) == '\n') {
val size=i-lastPos-1
lastPos=i
if (size>longest) {
longest=size
}
}
}
lastPos-=buf.length
read=is.read(buf)
}
} finally {
is.close()
}
longest
}
I'm new to Scala too, but I'm pretty sure there's a lot of room for flatMaps and other functions in this code.
Could someone post a functional version of this?
An alternative implementation:
def longest(filename: String) =
Source.fromFile(filename).getLines.map(_.size).max
Brief explanation:
getLines
returns an iterator of the lines in the file;map(_.size)
, equivalent tomap(line => line.size)
, returns a new iterator of the line lengthsmax
returns the greatest line length.
val longest = (filename: String) =>
io.Source.fromFile(filename).getLines.maxBy(_.length).length
Yes, this code is painfully imperative. In Scala the rough equivalent would be (!):
def longest(fileName: String) =
Source.fromFile(fileName).getLines().max(Ordering.fromLessThan[String](_.size < _.size)).size
Guess it wouldn't hurt to provide some explanation:
def longest(fileName: String) = Source.
fromFile(fileName). //file contents abstraction
getLines(). //iterator over lines
max( //find the max element in iterated elements
Ordering.fromLessThan[String](_.size < _.size) //however, use custom comparator by line size
).size //max() will return the line, we want the line length
Of course TMTOWTDI in Scala.
精彩评论