thanaselvan
thanaselvan

Reputation: 63

Scala,type mismatch; found : (Double, Option[String]) ⇒ Double required: ((Double, Option[String]))

Code for creating the match case

       object IrisSepalLengthOption {
       def sepallen(sep_len: Double,species: Option[String]):Double = species 
       match{
       case Some("Iris-setosa") => sep_len * 2
       case Some("Iris-virginica") => sep_len * 3
       case Some("Iris-versicolor") => sep_len * 4
       case _ => 0.0
     }

Main method to read the data from the csv file to parse the data and to apply the above funtion

def main(args: Array[String]){
    println(sepallen(4.0,Some("Iris-setosa"))) // This one works fine


    val source = Source.fromFile("E:\\MI_Dataset\\Iris.csv").getLines().drop(1).toArray
    val sepcol = source.map { line =>
      val str = line.split(",")
      val sep_len = str(1).toDouble
      val speceies = str(5).toString
      (sep_len,Option(speceies))
    }
    sepcol.take(5).foreach(println)  //This one prints the output correctly
    val p = sepcol.map(_._1)
    val s = sepcol.map(_._2)
    val result = sepcol.foreach(sepallen) //Here i am getting the mismatch error
  }

} 

When i call the function i am getting the type mistmatch error like(type mismatch; found : (Double, Option[String]) ⇒ Double required: ((Double, Option[String])) ⇒ ?

How to solve this problem.If anybody clarify this it would be great helpful to me

The sample data is as follows :

Upvotes: 0

Views: 1473

Answers (1)

C4stor
C4stor

Reputation: 8026

Notice the subtle difference between the expected and actual types, and most importantly the parentheses :

type mismatch; found : (Double, Option[String]) ⇒ Double required: ((Double, Option[String])) ⇒ ?

The sepallen function, expects two arguments, a Double and an Option[String], but the syntax sepcol.foreach(func) expects func to be a function of a single argument : a Tuple (whose elements are a Double and an Option[String])

You can fix it by exploding your tuple to feed it to sepallen, for example like this :

val result = sepcol.foreach{ case (sep_len, species) => sepallen(sep_len, species) }

Upvotes: 2

Related Questions