2016-03-03 144 views
2

我在Scala中有一個任務,到目前爲止,這麼好。一切編譯除了這一點:Scala mapValues()類型不匹配

@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: error: type mismatch;

found : (Double, Long) => Double

required: ((Double, Long)) => ?

任何人都可以點我在正確的方向?

+0

我想也許是因爲'mapValues'你給了你的輸入一個特定的情況,但是如果它是默認情況,它會返回什麼? –

回答

4

您提供了一個帶有兩個參數的函數,一個參數爲Double和一個參數爲Long,而不是帶有一個參數的函數 - 一個元組(Double, Long)。如果你需要一個元組作爲參數,使用

.mapValues { case (x: Double,y: Long) => whatever } 

請注意,你需要圍繞case{},而不是()

+0

但是,您不需要雙括號。 –

+0

啊對,固定。 – slouc

1

作爲替代slouc的回答是:
您可以使用untupeld

import Function.untupled 

Map.empty mapValues untupled myMethodWithMultipleArguments 

總之,提防mapValues的,因爲它只是創建了一個view