mardi 3 juillet 2018

scala - if/else in a for loop

If i use the two for loops like this i get a List[List[Int]], but how can i get a List[Int]? I dont know how i could write a if/else statement in only one for loop, can someone help me ?

def example:  (List[(Int, Int)], Int,Int) => List[Int] ={
    (list, p, counter) => 
    if (counter >=0)
        for(x<-list(i._1); if ( x._1 ==p))yield x._2
        for(x<-list(i._1); if ( x._1 !=p))yield example((x._1,x._2+i._2):: Nil,p,counter-1)
    else { ....}
}

Aucun commentaire:

Enregistrer un commentaire