gpt4 book ai didi

Scala mapValues() 类型不匹配

转载 作者:行者123 更新时间:2023-12-02 09:28:12 26 4
gpt4 key购买 nike

我有一个 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: error: type mismatch;

found : (Double, Long) => Double

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

有人能指出我正确的方向吗?

最佳答案

您提供了一个带有两个参数的函数,一个 Double 和一个 Long,而不是一个带有一个参数的函数 - 一个元组 (Double, Long)。如果您需要一个元组作为参数,请使用

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

请注意,您需要将 case 括入 {} 而不是 ()

关于Scala mapValues() 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35769852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com