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
Source: stackoverflow.com
Related Query
- Scala - Type Mismatch Found Unit : required Array[Int]
- Scala type mismatch problem (expected Map, found scala.collection.mutable.HashMap)
- scala type mismatch error, GenTraversableOnce[?] required
- Scala multi-partition a map - type mismatch; Found (A,B) => Boolean required (A,B) => Boolean?
- scala: type mismatch error - found T, required String
- Scala - type mismatch; found Int, required String
- Type mismatch found: scala function => Unit required: java.util.Consumer
- scala found Unit required Unit.type
- Scala Type Mismatch Unit Instead of Int
- scala type mismatch. found type, required _$1
- Type mismatch error: found SortedSet[Int], required Set[Int]
- found : Unit required: List[Int] type mismatch error for for loop
- Type mismatch error when folding a List[Int] with scala.math.pow: found Double, required Int
- Type Mismatch Unit and String in Scala
- type mismatch found : Unit expected : RDD[XYZ...]
- Type mismatch in scala quasiquote of macro definition: "type mismatch; found : field.NameType required: c.universe.TermName"
- Type mismatch found Unit, required Future[Customer] on flatmap
- Scala type mismatch: found Future[MyType] required Future[Option[MyType]]
- Type mismatch, found Unit, required Int. Using pattern matching, Scala with Java libraries
- scala type mismatch found Future[A] expected Future[B]
- Scala for comprehension returns type mismatch; found : Unit
- Play Scala Form type mismatch between Option and Unit
- Scala - Error type mismatch found : List[String] required: scala.collection.IterableOnce[Nothing]
- Scala GADT Fantom Type interpreter error: found Any Required A
- Scala type mismatch - found: Unit required: Array[String]
- Scala Spark type missmatch found Unit, required rdd.RDD
- map foldLeft type mismatch found Int required String
- type mismatch; found point.type, required T | Scala
- Type mismatch error. found Array[String] requires Seq[?] in Scala Spark
- Scala Error: type mismatch found :(Int, Int) => Int required: Ordering[Int]
More Query from same tag
- Append Map(key, Set[value]) in a Map to existing Key in Scala 2.11
- Convert keys into columns names an values into rows (Map)
- How to filter columns in one table based on the same columns in another table using Spark
- Create an actor
- Scala Spark - Discard empty keys
- How Can I submit multiple jobs in Spark Standalone cluster?
- Iteration and filtering of a Scala Map
- Explain why encode method returns empty Vector
- sbt docbook plugin
- How to override a setting to a new value in a task or configuration?
- In Scala, how to get the ANSI display length of a String?
- Dependency injection and Scala's Traits
- How to parse a case class which has fields as another case class using "scopt" command line argument?
- How to force lazy value computation
- Scala SBT and scala-library.jar file inclusion
- Scala Ordered by multiple fields
- Filtering on Scala Option[Set]
- Clear session value in scala
- Add Java source folder containing Symlinks to build.sbt file
- Implementing Foldable[List[A]]
- How to iterate a list of Pair with Scala?
- Why's type lattice in Scala?
- Is there any benefit of working with an Iterator over a List
- Connection pooling in slick?
- Java IO: Reading a file that is still being written
- Scala scope, initialization
- Why do these similar looking statements yield objects of different types?
- How to pass remote references between Akka actors?
- transpose UDF in pyspark
- Why does foreach operator fail with NullPointerException when using Dataset?