score:0

you could do something cleaner in my own opinion using yield :

def createarray(n:int):array[int] =
  (for(i: int <- 0 to n-1) yield scala.util.random.nextint(n)).toarray

this will make a "one lined function"

score:2

yet another one,

def createarray(n: int): array[int] = array.fill(n) { scala.util.random.nextint(n) }

then, for instance

val x: array[int] = createarray(10)

score:10

your for comprehension will be converted into something like:

0.to(x.length - 1).foreach(i => x(i) = scala.util.random.nextint(i))

since foreach returns (), the result of your for comprehension is (), so the result of the entire function is () since it is the last expression.

you need to return the array x instead:

for(i <- 0 to x.length-1)
        x(i) = scala.util.random.nextint(n)
x

Related Query