开发者

Functional style for this Scala code

开发者 https://www.devze.com 2023-03-27 20:28 出处:网络
A friend of mine is learning Scala and wrote this simple code to keep track of the longest line in a file:

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 to map(line => line.size), returns a new iterator of the line lengths
  • max 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消