user3117639
user3117639

Reputation:

Scala Double error when compiling in SPARK-REPL

I am learning Scala at the moment

   <pre>
     scala> val sample = similarities.filter(m => {
     |       val movies = m._1
     |       (movieNames(movies._1).contains("Star Wars (1977)"))
     |     })
     </pre>

sample: org.apache.spark.rdd.RDD[((Int, Int), Double)] = FilteredRDD[25] at filter at :36

The sample compiled just fine

But when I tried to call sample again in the next command

<pre>
scala> val result = sample.map(v => {
     |       val m1 = v._1._1
     |       val m2 = v._1._2
     |       val correl = v._2._1
     |       //val rcorr = v._2._2
     |      // val cos = v._2._3
     |       //val j = v._2._4
     |       (movieNames(m1), movieNames(m2), correl)
     |     })
<console>:41: error: value _1 is not a member of Double
             val correl = v._2._1
</pre>

Can someone please help me.Thanks in advance

Upvotes: 1

Views: 97

Answers (2)

elm
elm

Reputation: 20415

Given the amount of indexing on the composed tuple, consider wrapping ((Int, Int), Double) onto a case class and defining an implicit on it as follows,

case class Movie(m1: Int, m2: Int, correl: Double)

implicit def RichMovie(v: ((Int,Int),Double) ) = Movie(v._1._1, v._1._2, v._2)

Thus, given an instance of a composed tuple

scala> val m = ( (1,2), 3.5)
m: ((Int, Int), Double) = ((1,2),3.5)

we can access its members as follows,

scala> m.m1
res0: Int = 1

scala> m.m2
res1: Int = 2

scala> m.correl
res2: Double = 3.5

Upvotes: 1

Soumya Simanta
Soumya Simanta

Reputation: 11741

val correl = v._2._1 

should be just

val correl = v._2 

because it is part of the second element in the tuple ((Int, Int), Double)

Upvotes: 1

Related Questions