q67hbq
Last Updated: February 25, 2016
·
421
· sheerun
Photo on 08.01.2013 at 04.15

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

1457
C6a74c963b46a23b03a50cc3479b8992

But, how to unit test this?

over 1 year ago ·