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()};


  • if I remove new, the program returns a wrong answer.
  • Is there any way to make this code looks more professional?



closed as off-topic by yuri, Daniel, esote, Mast, Graipher Jun 19 at 15:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ Please be more specific in what you are looking for in a review. What do you mean with "looks more professional?". Also, do you understand what is happening with that map function? codereview.stackexchange.com/help/on-topic \$\endgroup\$ – dfhwze Jun 10 at 9:46
  • 2
    \$\begingroup\$ This isn't a question: "if I remove new, the program returns a wrong answer." That's a statement (and probably means you shouldn't remove new). \$\endgroup\$ – Toby Speight Jun 10 at 11:44
  • \$\begingroup\$ @TobySpeight I think it should be some kind of error comes out due to undefined behavior, right? \$\endgroup\$ – Ngọc Khánh Nguyễn Jun 12 at 3:41
  • \$\begingroup\$ @dfhwze I'm practicing Scala, It should be some kinda standard. \$\endgroup\$ – Ngọc Khánh Nguyễn Jun 12 at 3:43
  • The code, as posted, does not compile.
  • All the semicolons ; can be removed. You almost never see them in idiomatic Scala.
  • Unnecessary use of var. Use val.
  • Replace Array.range(1, 7).map(x => x.toLong) with Array.iterate(1L, 7)(_+1).
  • Replace case (x) with underscore _ when x is unused.
  • Replace case (x) => x with underscore _ when x is used immediately and once.
  • data.sortWith((x, y) => x < y) same thing as data.sorted.
  • Variable data not needed: val sorted : Array[Double] = Array.fill(length)(gen.next()).sorted
  • Unnecessary parentheses when defining diffmin and diffmax.
  • Variables max1 and max2 not needed: val d = diffmin.max max diffmax.max
  • \$\begingroup\$ Thank you very much \$\endgroup\$ – Ngọc Khánh Nguyễn Jun 12 at 3:40
  • \$\begingroup\$ @jwvh I would refrain from answering on code that does not compile. \$\endgroup\$ – dfhwze Jun 12 at 5:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.