I need to define some case classes, for example the following:
case class Gt(key: String, value: Any) extends Expression { def evalute[V, E](f: String => Any) = { def compare(v: Any): Boolean = { v match { case x: Number => x.doubleValue > value.asInstanceOf[Number].doubleValue case x: Array[_] => x.forall(a => compare(a)) case x => x.toString > value.toString } } compare(f(key)) } }
I do not like to repeat that for> <> = and <=
I also tried this:
trait Expression { def evalute[V, E](f: String => Any) = true def compare(v: Any, value: Any, cp: (Ordered[_], Ordered[_]) => Boolean): Boolean = { v match { case x: Number => cp(x.doubleValue, value.asInstanceOf[Number].doubleValue) case x: Array[_] => x.forall(a => compare(a, value, cp)) case x => cp(x.toString, value.toString) } } } case class Gt(key: String, value: Any) extends Expression { def evalute[V, E](f: String => Any) = { compare(f(key), value, ((a, b) => a > b)) } }
but this does not work :(
error: could not find implicit value for parameter ord: scala.math.Ordering[scala.math.Ordered[_ >: _$1 with _$2]] compare(f(key), value, ((a, b) => a > b))
Is there a way that passes an operator as a function in scala?
source share