scala 方法来定义接受不同数字类型列表的函数

发布于 2024-08-20 17:20:26 字数 562 浏览 4 评论 0原文

我有以下问题:我有一个函数,它接受 List[Double] 作为参数,对列表的元素执行一些算术运算,然后返回结果。我希望该函数也接受 List[Int]。这是一个示例:

def f(l: List[Double]) = {
    var s = 0.0 
    for (i <- l)
        s += i
    s
}

val l1 = List(1.0, 2.0, 3.0)
val l2 = List(1, 2, 3)

println(f(l1))
println(f(l2))

当然,第二个 println 失败,因为 f 需要 List[Double] 而不是 List[Int]。

另请注意 f 函数内求和的非 scala 风格公式,以证明需要在函数本身内使用 0 (或其他常量)(如果我求和 Int 值,我必须将 s 初始化为 0 而不是 0.0。

这是让函数在 Double 和 Int 上工作的最佳方法(更少的代码)?

(我已经看到一些关于 2.8 Numeric 特征的东西,我不太确定如何使用它......)

感谢大家的帮助。

I have the following problem: I have a function which takes a List[Double] as parameter, performs some arithmetic operations on the elements of the list and than return the result. I would like the function also to accept List[Int]. Here is an example:

def f(l: List[Double]) = {
    var s = 0.0 
    for (i <- l)
        s += i
    s
}

val l1 = List(1.0, 2.0, 3.0)
val l2 = List(1, 2, 3)

println(f(l1))
println(f(l2))

Of course the second println fails since f requires List[Double] and not List[Int].

Also note the non scala style formulation of the sum within the f function in order to evidence the need to use 0 (or other constants) within the function itself (if i sum Int values I have to init s to 0 not 0.0.

Which is the best way (less code) to get the function work on both Double and Int?

(I have seen something about 2.8 Numeric trait by I'm not so sure how to use it...)

Thanks everybody for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝色星空 2024-08-27 17:20:26

使用 scala 2.8 并使用数字组合来隐式转换,您的示例可以编写为:

import Numeric._
def f[T](l: List[T])(implicit n: Numeric[T]):T = {
    var s = n.zero
    for (i <- l)
        s = n.plus(s, i)
    s
}

val l1 = List(1.0, 2.0, 3.0)
val l2 = List(1, 2, 3)

println(f(l1))
println(f(l2))

//or
def f2[T](l: List[T])(implicit n: Numeric[T]):T = {
 import n._
 var s = zero
 for (i <- l)
   s += i
 s
}
println(f2(l1))
println(f2(l2))

现在另一个示例以更 scala 的方式进行求和:

def sum[T](l:List[T])(implicit n: Numeric[T]):T = {
 import n._
 l.foldLeft(zero)(_ + _)
}

println(sum(l1))
println(sum(l2))

//or since 2.8 Seq include already a sum function
def sum[T](l:List[T])(implicit n: Numeric[T]):T = l.sum

println(sum(l1))
println(sum(l2))

With scala 2.8 and using Numeric combine to implicit conversion your example could be written as :

import Numeric._
def f[T](l: List[T])(implicit n: Numeric[T]):T = {
    var s = n.zero
    for (i <- l)
        s = n.plus(s, i)
    s
}

val l1 = List(1.0, 2.0, 3.0)
val l2 = List(1, 2, 3)

println(f(l1))
println(f(l2))

//or
def f2[T](l: List[T])(implicit n: Numeric[T]):T = {
 import n._
 var s = zero
 for (i <- l)
   s += i
 s
}
println(f2(l1))
println(f2(l2))

Now another example doing the sum in a more scala way:

def sum[T](l:List[T])(implicit n: Numeric[T]):T = {
 import n._
 l.foldLeft(zero)(_ + _)
}

println(sum(l1))
println(sum(l2))

//or since 2.8 Seq include already a sum function
def sum[T](l:List[T])(implicit n: Numeric[T]):T = l.sum

println(sum(l1))
println(sum(l2))
美男兮 2024-08-27 17:20:26

此答案使用数字特征。

import Numeric._
def f[A](l: List[A])(implicit numeric: Numeric[A]) = 
  l reduceLeft ((l,r) => numeric.plus(l, r))

或者使用上下文边界:

def f[A : Numeric](l: List[A]) =
   l.reduceLeft((l,r) => implicitly[Numeric[A]].plus(l, r))

This answer uses the Numeric trait.

import Numeric._
def f[A](l: List[A])(implicit numeric: Numeric[A]) = 
  l reduceLeft ((l,r) => numeric.plus(l, r))

Or using context bounds:

def f[A : Numeric](l: List[A]) =
   l.reduceLeft((l,r) => implicitly[Numeric[A]].plus(l, r))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文