2

I have an assignment in Scala, so far, so good. Everything compiles except this:

@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)}

The piece I'm stuck on is the anonymous function in mapValues()

:95: error: type mismatch;

found : (Double, Long) => Double

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

Can anyone point me in the right direction ?

Community
  • 1
  • 1
Exie
  • 466
  • 5
  • 16
  • I think maybe because in `mapValues` you gave a specific case for your input, but what does it returns if it's the default case? – Avihoo Mamka Mar 03 '16 at 10:57

2 Answers2

4

You provided a function with two arguments, one Double and one Long, instead of a function with one argument - a tuple (Double, Long). If you need a tuple as an argument, use

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

Note that you need to surround the case into {} instead of ().

slouc
  • 9,508
  • 3
  • 16
  • 41
1

As an alternative to slouc's answer:
You can use untupeld

import Function.untupled

Map.empty mapValues untupled myMethodWithMultipleArguments

Anyway, beware of mapValues, because it just creates a view.

Community
  • 1
  • 1
Nabil A.
  • 3,270
  • 17
  • 29