Scala functional programming gymnastics

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

    Straightforward solution with plain Scala and anonymous lambda, without any mappings, folds, Double.{Min/Max}Value, and so on:

    def restrict(floor : Option[Double], cap : Option[Double], amt : Double) : Double =
      ((x:Double) => x min cap.getOrElse(x))(amt max floor.getOrElse(amt))
    

提交回复
热议问题