[英]Scala mapValues() type mismatch
到目前為止,我在Scala中有一項任務,非常好。 除以下內容外,所有內容均可編譯:
@transient val aggs = msgs.transform { rdd =>
val ts = rdd.map(quote => quote.ts).max() /// maximum of timestamp in the rdd
rdd.map{ q =>
((q.symbol,q.ts),(q.price,ts)) /// ((String, Long), (Double, Long)) structure
}
}
.reduceByKey{ (x,y) => (x._1 + y._1, x._2 + y._2) } // returns (Double, Long)
.mapValues( (x: Double,y: Long) => (y.toDouble / x.toDouble) ) // (Double, Long) => (Double)
.map{ case ((s,t),v) => (s,t,v)}
我遇到的問題是mapValues()中的匿名函數
:95:錯誤:類型不匹配;
找到:(雙倍,長)=>雙倍
必需:(((Double,Long))=>?
誰能指出我正確的方向?
您提供了一個帶有兩個參數的函數,一個Double
和一個Long
,而不是一個帶有參數的函數-一個元組(Double, Long)
。 如果需要元組作為參數,請使用
.mapValues { case (x: Double,y: Long) => whatever }
請注意,您需要將case
包圍在{}
而不是()
。
作為slouc答案的替代方法:
您可以使用untupeld
import Function.untupled
Map.empty mapValues untupled myMethodWithMultipleArguments
無論如何,提防mapValues,因為它只會創建一個view 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.