I currently have a Java program which does something like the following:
int nvars = 10;
long vars[] = new long[nvars];
for(int i = 0; i < nvars; i++) {
vars[i] = someFunction(i);
anotherFunction(vars[i]);
}
I am converting it into Scala code and have:
val nvars: Int = 10
val vars: Array[Long] = new Array[Long](nvars)
for ( i <- 0 to nvars-1 )
vars(i) = someFunction(i)
anotherFunction(vars(i))
}
Any advice on how to make this (more) functional?
There are lots of useful constructor methods in the Array companion object, for example, for your situation here, you can use
tabulate
:If
anotherFunction
returns a result rather than just being a "side-effect" function, you can instead capture this with a call tomap
: