Last Updated: February 25, 2016
·
638
· sheerun

Default argument as function of other argument

Just use currying and an implicit argument:

def smartFloor(x: Double)(
  implicit precision: Double = math.log10(x).floor
) = x - (x % math.pow(10, precision))

Usage:

scala> smartFloor(1234)
res: Double = 1000.0

scala> smartFloor(0.542)
res: Double = 0.5

scala> smartFloor(0.542)(-2)
res: Double = 0.54

1 Response
Add your response

But, how to unit test this?

over 1 year ago ·