0

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)

user2386854
  • 173
  • 1
  • 11
  • can you show the errors? Because it seems to run fine. And also the scala version. – ale64bit Nov 19 '14 at 01:34
  • edited to add in that info, thank you very much – user2386854 Nov 19 '14 at 01:39
  • You say that it compiles without error, but that's not possible: your program is illegal, it shouldn't compile without an error, in fact, it shouldn't compile *at all*. The only logical explanation I can see is that the file you are showing us is not the one you are compiling. Maybe you have two files in two directories and are editing one but compiling the other, or you are editing the file in your editor but forgot to save it. – Jörg W Mittag Nov 19 '14 at 11:56
  • So i feel like i must be doing something wrong. I installed scala on a linux machine i have and tried it there and got the same error. i'm navigating to the folder that holds sumit.scala and typing "scalac sumit.scala", when that finshes "scala sumit.scala" and get the above error on both machines – user2386854 Nov 19 '14 at 14:17

3 Answers3

1

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.

seand
  • 5,168
  • 1
  • 24
  • 37
0

Try to run it like this

scala sumit.scala

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

ale64bit
  • 6,232
  • 3
  • 24
  • 44
  • Same error, do i need any special permission to run these programs? – user2386854 Nov 19 '14 at 01:44
  • @user2386854 Not really, the only detail I see is that the method total_select_values must be inside the object sumit, not outside. The program compiles and runs just fine for me, using scala 2.11.1 and java 7. – ale64bit Nov 19 '14 at 01:46
  • I wonder if it has something to do with my java version, i cannot update without admin rights... Thank you very much for your help. Does it matter the order of functions inside the object... does main need to be the first? – user2386854 Nov 19 '14 at 01:51
  • The order of the objects doesn't matter. But the methods must be inside some class or object. Can you show what files you have in your folder after you compile sumit.scala? – ale64bit Nov 19 '14 at 01:53
  • Thanks again -- sumit$$anonfun$main$1.class, sumit$$anonfun$total_select_values$1.class, sumit$.class, sumit.class, sumit.scala, – user2386854 Nov 19 '14 at 02:13
0

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

Carl
  • 826
  • 1
  • 7
  • 14
  • Adding the function into the object still yields the same error. I'm wondering if it my java version – user2386854 Nov 19 '14 at 03:01
  • Are you sure that you are compiling the same file that you are editing? I don't see how your original file compiles but you said it compiles without error? – Carl Nov 19 '14 at 03:25
  • scalac sumit.scala finishes without error... i get errors after running scala sumit.scala. the files i listed below are generated from scalac sumit.scala command – user2386854 Nov 19 '14 at 03:32