Reputation: 173
I need define some case classes like the following one:
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 don't like 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 that not working :(
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 pass a operator as a function in scala?
Upvotes: 6
Views: 761
Reputation: 4618
I'm not familiar with Scala, it does seem to have support for anonymous functions/lambdas: http://www.scala-lang.org/node/133
Upvotes: 0
Reputation: 170745
(a, b) => a > b
works fine. Your problem is with the types.
What are V
and E
in evalute[V, E]
supposed to be?
You pass it (a, b) => a > b
as the parameter cp: (Ordered[_], Ordered[_]) => Boolean
. So you have a: Ordered[_]
and b: Ordered[_]
. Which is the same as a: Ordered[X] forSome {type X}
and b: Ordered[Y] forSome {type Y}
. With these types, a > b
doesn't make sense.
Upvotes: 6
Reputation: 38978
In Scala, those are not operators, but methods. You can lift any method to a function by putting an underscore after it. e.g.
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) Client VM, Java 1.6.0_21).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val f: (Int => Boolean) = 1 <= _
f: (Int) => Boolean = <function1>
scala> (0 to 2).map(f)
res0: scala.collection.immutable.IndexedSeq[Boolean] = Vector(false, true, true)
Upvotes: 0