码迷,mamicode.com
首页 > 其他好文 > 详细

Option Variance

时间:2016-04-24 11:06:59      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

package option

object Variance {

  def mean(xs: Seq[Double]): Option[Double] = {
    if (xs.isEmpty) None
    else Some(xs.sum / xs.length)

  }

  def variance(xs: Seq[Double]): Option[Double] = mean(xs) flatMap (m => mean(xs map (x => math.pow(x - m, 2))))

  def main(args: Array[String]): Unit = {
    val l = List(1, 20, 3, 45, 5, 6, 7.0)
    val l1 = Nil: List[Double]
    println(mean(l))
    println(variance(l))

    println(mean(l1))
    println(variance(l1))
  }
}
Some(12.428571428571429)
Some(209.10204081632648)
None
None

 

Option Variance

标签:

原文地址:http://www.cnblogs.com/JonkeyGuan/p/5426407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!