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
I find that when a question asks to use an Option
to indicate an optional parameter, there's usually a more natural way to represent the missing parameter. So I'm going to change the interface a little here, and use default arguments to define the function and named parameters to call the function.
def restrict(amt:Double,
floor:Double = Double.NegativeInfinity,
cap:Double=Double.PositiveInfinity):Double =
(amt min cap) max floor
Then you can call:
restrict(6)
restrict(6, floor = 7)
restrict(6, cap = 5)
(Another example of the same principle.)