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
Written by Adam Stankiewicz
Related protips
1 Response
But, how to unit test this?
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Scala
Authors
Related Tags
#scala
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#