How to compile this Scala code

954 views Asked by At

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)

3

There are 3 answers

0
seand On

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.

5
ale64bit On

Try to run it like this

scala sumit.scala

i.e., add the extension of the file.

3
Carl On

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.)