如何比较两个数字。任何数字。与Int
和Float
一样?我不想与复杂的数字或类似的东西进行比较。我只想比较那些可比的。 Float和Int是。
假设你有:
def compareTwoNumbers[???](array:Array[???], number:???) = {
array(0) > number // this has to compile
}
我写的是什么,而不是???
?
到目前为止我尝试过的事情:
Number
。T <: Number
Numeric
(抱歉,我不明白在这种情况下如何使用它,没有示例/文档太差)。答案 0 :(得分:4)
只需使用订购类型类:
def cmp[A](arr: Array[A], number: A)(implicit ord: Ordering[A]) =
ord.gt(arr(0), number)
// or
def cmp[A](arr: Array[A], number: A)(implicit ord: Ordering[A]) = {
import ord._
arr(0) > number
}
scala> cmp(Array(4), 2)
res9: Boolean = true
scala> cmp(Array(BigInt(4)), BigInt(2))
res15: Boolean = true
scala> cmp(Array(4), 2.0)
<console>:9: error: type mismatch;
found : Array[Int]
required: Array[AnyVal]
scala> cmp(Array(4.0), 2)
<console>:9: error: type mismatch;
found : Array[Double]
required: Array[AnyVal]
答案 1 :(得分:4)
我认为主要问题是数字类型的转换。所以我们编码:
trait NumericConversion[X, Y] {
def convert(x: X): Y
}
当然,必须指定抽象概念:(例如)
implicit object Int2IntNumericConversion extends NumericConversion[Int, Int] {
def convert(i: Int): Int = i
}
implicit object Double2DoubleNumericConversion extends NumericConversion[Double, Double] {
def convert(d: Double): Double = d
}
implicit object Int2DoubleNumericConversion extends NumericConversion[Int, Double] {
def convert(i: Int): Double = i.toDouble
}
现在比较方法如下:
def compareTwoNumbers1[N1, N2, N3](n1: N1, n2: N2)
(implicit conv1: NumericConversion[N1, N3],
conv2: NumericConversion[N2, N3],
ord: Ordering[N3]): Int = {
ord compare (conv1 convert n1, conv2 convert n2)
}
用法:
compareTwoNumbers1[Int, Double, Double](3, 8D) // -1
多么可怜,我们必须明确说明类型参数,所以我试过:
def compareTwoNumbers2[N3] = new {
def apply[N1, N2](n1: N1, n2: N2)(implicit conv1: NumericConversion[N1, N3],
conv2: NumericConversion[N2, N3],
ord: Ordering[N3]): Int = {
ord compare (conv1 convert n1, conv2 convert n2)
}
}
这减少为一种类型的论点:
compareTwoNumbers2[Double](3, 8D) // -1
不满意,所以我尝试了这个:
trait NumericUpperBound[Num1, Num2, UpperBound]
implicit object NumericUpperBoundIDD extends NumericUpperBound[Int, Double, Double]
implicit object NumericUpperBoundDID extends NumericUpperBound[Double, Int, Double]
使用新的比较方法:
def compareTwoNumbers3[N1, N2, N3](n1: N1, n2: N2)
(implicit nub: NumericUpperBound[N1, N2, N3],
conv1: NumericConversion[N1, N3],
conv2: NumericConversion[N2, N3],
ord: Ordering[N3]): Int = {
ord compare (conv1 convert n1, conv2 convert n2)
}
现在可行:
compareTwoNumbers3(3, 8D) // -1
当然,必须创建所有基元的类型类。但是稍后可以灵活地将其扩展到BigInt
等。
修改强>
@ wvxvw的评论提到了NumericUpperBounds
矩阵激励我绕过矩阵,这是一个正在运行的例子(目前不包括Byte
和Short
):
trait ==>[X, Y] extends (X => Y)
object ==> {
def apply[X, Y](f: X => Y): X ==> Y = {
new (X ==> Y) {
def apply(x: X): Y = f(x)
}
}
}
implicit val Int2LongNumericConversion = ==> { x: Int => x.toLong }
implicit val Int2FloatNumericConversion = ==> { x: Int => x.toFloat }
implicit val Int2DoubleNumericConversion = ==> { x: Int => x.toDouble }
implicit val Long2FloatNumericConversion = ==> { x: Long => x.toFloat }
implicit val Long2DoubleNumericConversion = ==> { x: Long => x.toDouble }
implicit val Float2DoubleNumericConversion = ==> { x: Float => x.toDouble }
implicit def reflexiveNumericConversion[X]: X ==> X = new (X ==> X) { def apply(x: X): X = x }
trait NumericUpperBound[Num1, Num2, UpperBound]
implicit def reflexiveNumericUpperBound[X]: NumericUpperBound[X, X, X] = new NumericUpperBound[X, X, X] {}
implicit def inductiveNumericUpperBound1[X, Y](implicit ev: X ==> Y): NumericUpperBound[Y, X, Y] = new NumericUpperBound[Y, X, Y] {}
implicit def inductiveNumericUpperBound2[X, Y](implicit ev: X ==> Y): NumericUpperBound[X, Y, Y] = new NumericUpperBound[X, Y, Y] {}
def compareTwoNumbers[N1, N2, N3](n1: N1, n2: N2)
(implicit nub: NumericUpperBound[N1, N2, N3],
conv1: N1 ==> N3,
conv2: N2 ==> N3,
ord: Ordering[N3]): Int = {
ord compare (n1, n2)
}
compareTwoNumbers(9L, 13) // -1
答案 2 :(得分:4)
我目前正在学习Scala,所以不要太认真地对待我的回答,但我认为查看边界或类型约束可以帮助您。 View Bound允许您比较任何标准数字,因为它们之间存在隐式视图,但它不会更进一步,因为没有从BigInt到BigDecimal的隐式视图。
def compareTwoNumbers[A <% Double, B <% Double](array:Array[A], number:B) = array(0) > number
scala> compareTwoNumbers(Array(1, 2, 0), 0.99)
res1: Boolean = true
scala> compareTwoNumbers(Array(1.0, 2, 0), 0.99)
res2: Boolean = true
scala> compareTwoNumbers(Array(1.0, 2, 0), 1)
res3: Boolean = false
我很想知道是否可以支持BigInt和BigDecimal并知道我的解决方案有什么缺点。
答案 3 :(得分:2)
这是:
def cmp[T1, T2](arr: Array[T1], num: T2)
(implicit v12: T1 => T2 = null, v21: T2 => T1 = null,
ord1: Ordering[T1], ord2: Ordering[T2]): Boolean = v12 match {
case null => ord1.gt(arr(0), num)
case _ => ord2.gt(arr(0), num)
}
一些用例:
scala> cmp(Array(1,2), 0.1) //T1 = Int, T2 = Double
res3: Boolean = true
scala> cmp(Array(1.2, 2.3), 1) //T1 = Double, T2 = Int
res4: Boolean = true
scala> cmp(Array(1,2), BigInt(100)) //T1 = Int, T2 = BigInt
res5: Boolean = false
scala> cmp(Array(123.5 ,2233.9), BigDecimal(100)) //T1 = Double, T2 = BigDecimal
res6: Boolean = true
scala> cmp(Array(123.5 ,2233.9), 200.toByte)
res7: Boolean = true
答案 4 :(得分:1)
def compareTwoNumbers(a: Number, b: Number) = {
a.floatValue() > b.floatValue() // this has to compile
}
答案 5 :(得分:0)
采用这段代码并不难:
def gt[A : Numeric, B: Numeric](first: A, second: B) = {
val a = implicitly[Numeric[A]].toDouble(first)
val b = implicitly[Numeric[B]].toDouble(second)
a > b
}
应该使用不同类型的数字:
scala> gt(4, 4.0)
res3: Boolean = false
scala> gt(4, 3.0)
res4: Boolean = true
scala> gt(3L, 4.0)
res5: Boolean = false