I have a curry-able function for comparing Doubles
def almostEqual(tolerance:Double)(x:Double, y:Double):Boolean = {
( x == y) || abs(x - y ) < tolerance * max(abs(x),abs(y))
}
I often call the function like almostEqual(0.01) to return me a binary function for testing whether two given Doubles are within 1 percent of each other.
In several cases I have Option[Double] rather than Double. So I wrote the following function, so I can use ae(0.01) to give me a function of type (Option[Double],Option[Double])=>Boolean which will be a function to determine whether the two underlying numbers are within 1 percent of each other.
My gut feel is that there is a much easier way to do this. What is the best way to write the function which takes almostEqual as argument and returns something isomorphic to ae ?
def optionAlmostEqual(test:(Double,Double)=>Boolean):(Option[Double],Option[Double])=>Boolean = {
(op1,op2) => {
(op1,op2) match {
case (Some(a),Some(b)) => test(a,b)
case _ => false
}
}
}
def ae(delta:Double):(Option[Double],Option[Double])=>Boolean = {
optionAlmostEqual(almostEqual(delta))
}