Reputation: 173
I have a file called sumit.scala with the following contents
object sumit {
def main(args: Array[String]) = {
val start:Double = System.nanoTime
total_select_values(1 to 15000, {e => true})
val end:Double = System.nanoTime
println("time " + (end - start)/ 1000000000.0)
println("")
}
}
def total_select_values(list: Range, selector : Int => Boolean) = {
var sum = 0
list.foreach { e =>
if (selector(e)) sum += e
}
sum
}
i'm trying to compile it on the command line
scalac sumit.scala
which compiles without error but when i run it
scala sumit
i get a bunch of errors, i'm new to scala and i'm just trying to time this code once it's compiled to see the performance difference. I've tried putting my "total_select_values" in the object and out (as shown here) with no difference.
Thanks for any help!
Updated with Scala info and the actual error
Scala version 2.11.4 Java 1.7.0_40
java.lang.NoSuchMethodException: sumit.main([Ljava.lang.String;) at java.lang.Class.getMethod(Unknown Source) at scala.reflect.internal.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:66) at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101) at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:22) at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:39) at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:29) at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:39) at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:65) at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87) at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98) at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103) at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Upvotes: 0
Views: 952
Reputation: 5296
The function
def total_select_values ...
Has to go inside an object or class. This appears to be a constraint of Scala based on the JVM; can't have true free functions.
Upvotes: 1
Reputation: 876
The code you have gives me the following error when I run scalac:
sumit.scala:11: error: expected class or object definition
def total_select_values(list: Range, selector : Int => Boolean) = {
^
one error found
But, if I change the code to put total_select_values inside the sumit object (as suggested in the comments):
object sumit {
def main(args: Array[String]) = {
val start:Double = System.nanoTime
total_select_values(1 to 15000, {e => true})
val end:Double = System.nanoTime
println("time " + (end - start)/ 1000000000.0)
println("")
}
def total_select_values(list: Range, selector : Int => Boolean) = {
var sum = 0
list.foreach { e =>
if (selector(e)) sum += e
}
sum
}
}
Then, when I run:
scalac sumit.scala
scala sumit
it produces:
time 0.003286401time 0.003286401
But, also, since scala can be run interactively. Just running:
scala sumit.scala
also works. (The scalac step can be left out.)
Upvotes: 0
Reputation: 6242
Try to run it like this
scala sumit.scala
i.e., add the extension of the file.
Upvotes: 0