Counter in for comprehension

How to do Scala equivalent to this Java code:

int i = 0;
for(String x: xs) {
    for(String y : ys) {
        foo(x, y, i);
        i+=10;
    }
}

Answers


There are multiple ways of doing it:

var i = 0                                 
for(x <- xs;y <- ys) {
  foo(x,y,i)
  i = (i+10)
}

To do it functionally without using external i:

xs.foldLeft(0){(a:Int,b) =>
   ys.foldLeft(a){(c:Int, p) =>
     foo(b,p,a)
     c+10
   }
}

Need Your Help

Statistical analysis on Bell shaped (Gaussian) curve

image-processing opencv statistics plot gaussian

In my application I am getting images (captured by a high speed camera) containing projections of some light sources on the screen.

Android - String encoding failed in ListView

android string listview encoding

I'm using Android Studio lastest version on Windows.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.