Scala mapValues()类型不匹配
到目前为止,我在Scala有一个任务,很好。除此之外,所有内容都可以编译:Scala mapValues()类型不匹配,scala,Scala,到目前为止,我在Scala有一个任务,很好。除此之外,所有内容都可以编译: @transient val aggs = msgs.transform { rdd => val ts = rdd.map(quote => quote.ts).max() /// maximum of timestamp in the rdd rdd
@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:错误:类型不匹配
发现:(双精度,长)=>双精度
必填项:((双精度,长))=>
有人能给我指出正确的方向吗?您提供了一个有两个参数的函数,一个是
双参数,一个是长参数,而不是一个只有一个参数的函数-元组(双参数,长参数)
。如果需要元组作为参数,请使用
.mapValues { case (x: Double,y: Long) => whatever }
请注意,您需要将案例
包围在{}
中,而不是()
中,以替代slouc的答案:
您可以使用untupeld
import Function.untupled
Map.empty mapValues untupled myMethodWithMultipleArguments
无论如何,要小心mapValues,因为它只会创建一个。我想可能是因为在mapValues
中,您为输入指定了一个特定的大小写,但是如果是默认大小写,它会返回什么?不过,您不需要双括号。