0

I'm writing a Play 2.3.2 application in Scala.

I use reactivemongo as driver for my MongoDB database. I've a collection named "recommendation.tagsSimilarity", that contain the value of the similarity between my tags, where a tag is in the form :"category:attribute".

An example of a document is like the following:

{
    "_id" : ObjectId("5440ec6e4165e71ac4b53a71"),
    "id" : "10912199495810912197116116114-10912199581091219711611611450",
    "tag1" : "10912199495810912197116116114",
    "tag1Name" : "myc1:myattr",
    "tag2" : "10912199581091219711611611450",
    "tag2Name" : "myc:myattr2",
    "eq" : 0
}

A doment represents an element of a matrix of nxn dimensions, where n is the number of tags saved.

Now I've created a collection named "recommendation.correlation" on which i save the correlation between a "category" and a tag.

For do that I'm writing a method that iterate on the elements of the TagSimilarity as a matrix.

def calculateCorrelation: Future[Boolean] = {
    def calculate(category: String, tag: String): Future[(Double, Double)] = {//calculate the correlation and return the tuple value
    }

    play.Logger.debug("Start Correlation")
  Similarity.all.toList flatMap { tagsMatch =>
    for(i <- tagsMatch) {
      val category = i.tag1Name.split(":")(0) // get the tag category
      for(j <- tagsMatch) {
        val productName = j.tag2Name //obtain the product tag
        calculate(category, productName) flatMap {value =>
          val correlation = Correlation(category, productName, value._1, value._2) //create the correlation object
          val query = Json.obj("category" -> category, "attribute" -> productName)
          Correlations.update(query, correlation, upsert = true) flatMap{status => status match {
            case LastError(ok, _, _, _, _, _, _) => Future{true}
            case _ => Future{false}
          }}
        }
      }
    }


  }

}

But the compiler gives me the following error:

[error] /Users/alberto/git/bdrim/modules/recommendation-system/app/recommendationsystem/algorithms/Pearson.scala:313: type mismatch;
[error]  found   : Unit
[error]  required: scala.concurrent.Future[Boolean]
[error]     for(i <- tagsMatch) {
[error]           ^
[error] /Users/alberto/git/bdrim/modules/recommendation-system/app/recommendationsystem/algorithms/Pearson.scala:313: type mismatch;
[error]  found   : Unit
[error]  required: scala.concurrent.Future[Boolean]
[error]     for(i <- tagsMatch) {
[error]           ^
[error] one error found

What's wrong?? I can't understand why the for statement don't return nothing. In addition to I want to ask why i can't write the code in a for comprehension in Scala for iterate two times on the list.

alberto adami
  • 729
  • 1
  • 6
  • 25

1 Answers1

1

You forgot to use yield with for:

for(i <- tagsMatch) { ... } gets translated to a foreach instruction.

Using for(i <- tagsMatch) yield { ... } it will actually translate to map/flatMap and yield a result (remember to use it on both of your fors).

Justin Kaeser
  • 5,868
  • 27
  • 46