Adrien
Adrien

Reputation: 433

Iterating over CompactBuffer- Spark and Scala

I am new to Scala and familiar with Python. I struggle to iterate over a Iterator[String] object ( Compact Buffer). I have tried foreach method without succes.

Here is my code :

def CCF_Iterate(pair : (String, List[Iterable[String]]) ) = {
   
   var key = pair._1
   var values_iterate = pair._2
   var min = pair._1
   val list_values = List.empty[String]

   for (value <- values_iterate) {

       if (min > value ) {
       min = value}


    list_values:+ value
}  

And I get this error message :

found :    Iterable[String]
required : String
if (min > value )
        ^

Could someone please help me iterating over this list ? Thank you very much

Upvotes: 0

Views: 199

Answers (1)

Dima
Dima

Reputation: 40510

values_iterate is a list of Iterable (another list or something), so as you iterate through it, the values are Iterable - thus the error. You need to .flatten it first.

Also list_values:+ value doesn't really do anything: List is immutable, it returns you a new copy with appended value, but you discard. Also the if statement in the loop is inconsequential too: you are looking for the minimal value in the loop, but never use it.

Another thing is that appending to a List is very inefficient (it is a linked list, so appending to it is O(n)), seeing something like this in the code is usually a red flag.

Finally, in scala we very rarely use var, and almost never use loops.

In general, to create a flat list of values, you just do val list = pair._2.flatten. And to find the minimum value in the list, you do list.min. And if you want to bound it with the value given in the parameter, then you do math.min(min, list.min)

Upvotes: 1

Related Questions