Scala functional programming gymnastics

后端 未结 13 856
悲&欢浪女
悲&欢浪女 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:34

    I like the initial solution with the match-case most - beside the fact, that I didn't understand that amt means amount (in Germany, 'amt' means 'office') and I only knew cap as something I wear on my head ...

    Now here is a really uninspired solution, using an inner method:

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

提交回复
热议问题