-1
\$\begingroup\$

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!

\$\endgroup\$
4
  • 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\$ Commented Jun 10, 2019 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\$ Commented Jun 10, 2019 at 11:44
  • \$\begingroup\$ @TobySpeight I think it should be some kind of error comes out due to undefined behavior, right? \$\endgroup\$ Commented Jun 12, 2019 at 3:41
  • \$\begingroup\$ @dfhwze I'm practicing Scala, It should be some kinda standard. \$\endgroup\$ Commented Jun 12, 2019 at 3:43

1 Answer 1

3
\$\begingroup\$
  • 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
\$\endgroup\$
1
  • \$\begingroup\$ @jwvh I would refrain from answering on code that does not compile. \$\endgroup\$ Commented Jun 12, 2019 at 5:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.