I've just started practicing Functional Programming in Scala in some days. This is the code I used to perform KS-test on a random number generator.
class RNG {
def next(): Double;
}
import scala.math.sqrt;
object main {
val length: Int = 100000;
def main(args: Array[String]) {
// generate 100000 random numbers
var seed: Array[Long] = Array.range(1, 7).map(x => x.toLong);
var gen: RNG = new RNG(seed);
var data: Array[Double] = (new Array[Double](length)).map {case (x) => gen.next()};
// sort data
val sorted = data.sortWith((x, y) => x < y);
// perform ks test
val baseline: Array[Int] = Array.range(0, length);
val baselinemin: Array[Double] = baseline.map {case (x) => x.toDouble/length}
val baselinemax: Array[Double] = baseline.map {case (x) => (1+x.toDouble)/length}
val diffmin = (sorted.zip(baselinemin)).map {case (x, y) => x - y};
val diffmax = (sorted.zip(baselinemax)).map {case (x, y) => y - x};
val max1 = diffmin.max;
val max2 = diffmax.max;
val d = if (max1 > max2) max1 else max2;
println("D\t" + d);
val dbase = 2/sqrt(length);
println("D base\t" + dbase);
}
}
There is a little weird thing that in line
var data: Array[Double] = (new Array[Double](length)).map {case (x) => gen.next()};
Question:
- if I remove new, the program returns a wrong answer.
- Is there any way to make this code looks more professional?
Thanks!
mapfunction? codereview.stackexchange.com/help/on-topic \$\endgroup\$new). \$\endgroup\$