Manju
Manju

Reputation: 1

Scala Type MisMatch Error in spark

I have created the following function:

 def mapToPers(inTuple: (String, String, String), 
               v: ((Double, Double, Double, Double, Double),
                   Map[Double,
                       (Double, Double, Double, Double, Double)])) = {

      val (prod: String, market: String, week: String) = inTuple
      val result = for (cumePeriod <- cumePeriods) yield {
        val (per, weekList) = cumePeriod
        if (weekList.contains(week)) ((prod, market, per), v)        
  }
 result
}

When I call it, it gives error of type mismatch:

Description Resource Path Location Type type mismatch; found : ((String, String, String), ((Double, Double, Double, Double, Double), Map[Double,(Double, Double, Double, Double, Double)])) => scala.collection.immutable.Iterable[Any] required: (((String, String, String), ((Double, Double, Double, Double, Double), Map[Double,(Double, Double, Double, Double, Double)]))) => TraversableOnce[?]

Upvotes: 0

Views: 586

Answers (1)

Daniel Langdon
Daniel Langdon

Reputation: 5999

You clearly have a problem with nested tuples. Check the parentheses:

found :

(
  (String, String, String), 
  (
    (Double, Double, Double, Double, Double), 
    Map[Double,(Double, Double, Double, Double, Double)]
  )
) => scala.collection.immutable.Iterable[Any] 

required:

(
  (
    (String, String, String), 
    (
      (Double, Double, Double, Double, Double), 
      Map[Double,(Double, Double, Double, Double, Double)]
    )
  )
) => TraversableOnce[?]

So I see one more level of nesting. Hard to spot given the very confusing types, I would recommend you use some case classes here.

Edit: BTW, I Spotted something else. The if you are using does not have an else, so the for comprehension will not know what to yield exactly. You might want to yield only after checking:

val result = for { 
  (per, week_list) <- cumePeriods
  if week_list.contains(week)
}
  yield ((prod, market, per), v)

Upvotes: 3

Related Questions