partialResult
(简称
pr
Double
值来自
100.0
直到你不舒服地接近零,在这一点上你忽略了收到的
双倍
重新计算它的“真”值。
pr.foldLeft((Map.empty[String,Double].withDefaultValue(100.0)
,List.empty[(String,Double)])){ case ((m,res),(str,dbl)) =>
if (math.abs(m(str) - dbl) < 1.1) //too close
(m, (str,m(str))::res) //use remainder
else
(m + (str -> (m(str)-dbl)), (str,dbl)::res) //update Map, build result
}._2.reverse
//res0: List[(String, Double)] = List((a,25.5), (a,25.5), (a,49.0)
// , (b,25.5), (b,25.5), (b,49.0))