Is Scala idiomatic coding style just a cool trap for writing inefficient code?

后端 未结 10 731
借酒劲吻你
借酒劲吻你 2020-12-22 17:19

I sense that the Scala community has a little big obsession with writing \"concise\", \"cool\", \"scala idiomatic\", \"one-liner\" -if possible- code. This

10条回答
  •  春和景丽
    2020-12-22 17:34

    What about this?

    def removeMax(xs: List[Int]) = {
      val buf = xs.toBuffer
      buf -= (buf.max)
    }
    

    A bit more ugly, but faster:

    def removeMax(xs: List[Int]) = {
      var max = xs.head
      for ( x <- xs.tail ) 
      yield {
        if (x > max) { val result = max; max = x; result}
        else x
      }
    }
    

提交回复
热议问题