Scala functional programming gymnastics

后端 未结 13 711
悲&欢浪女
悲&欢浪女 2021-02-01 09:36

I am trying to do the following in as little code as possible and as functionally as possible:

def restrict(floor : Option[Double], cap : Option[Double], amt : D         


        
13条回答
  •  长发绾君心
    2021-02-01 10:26

    This is another way to fix Landei's first answer

    def restrict(floor : Option[Double], cap : Option[Double], amt : Double) : Double = {
      val chopBottom = (floor.getOrElse(amt) max amt) 
      chopBottom min cap.getOrElse(chopBottom)
    }
    

提交回复
热议问题