Is there a way to run pure scala code in a script tag - scala.js

Using the scalatags script tag I know I can define the following:
script("console.log('Running javascript code')")
But is there a way I can rather pass pure scala code instead? So something like the following:
script(println("Running scala code now"))

No, that is not possible.
You can achieve the same effect in a different way, by exporting a top-level function with the Scala code you want to run, then generate a script that calls that function:
object Exports {
#JSExportTopLevel("dynamicScriptCode")
def code(): Unit =
println("Running Scala code now")
}
...
script("dynamicScriptCode();")

Related

Is it possible to execute string as code in scala

So for spark sql you have do something like this:
val query = s"select *from table...."
val temp = sqlContext.sql(query)
Is there anything similar to this for just regular spark code? Like in python they have this exec(string) function that take string and run it as code.
This is the link: Click Me
Gatling application is doing a similar trick, they allow you to write an arbitrary Scala code for load testing and then run it with their framework.
As far as I remember they basically write Scala code into a file, prepend/append it with something like:
class MyClass {
def foo() = {
// Code goes here
}
}
and execute Scala compiler to compile this code and then load this code as a regular class.

Does Scala have a global object or class?

I know programmers are supposed to wrap their code in an application object:
object Hello extends App {
println("Hello, World")
}
It is required in Eclipse, if I ever want to get any output. However, when I tried to write some code (very casually) in Emacs, I write like this:
class Pair[+T](val first: T, val second: T)
trait Friend[-T] {
def befriend(someone: T)
}
def makeFriendWith(s: Student, f: Friend[Student]) {
f.befriend(s)
}
It seems like there is no universal object or class that wraps over the function makeFriendWith. Is Scala like JavaScript, everything is attached to a global object? If not, what is this function attached to?
Also why can this work in console (I complied it with scala command and it worked) but does not work in Eclipse? What's the use of the Application object?
Scala doesn't have top-level defs, but your script can be run by either the REPL or the scala script runner.
The precise behavior of your script depends on which way you run it.
The REPL can run scripts line-by-line or whole hog. (Compare :paste and :paste -raw versus :load or -i init.script and the future option -I init.script.)
There is an issue about sensitive scripting. The script runner should realize if you're trying to run an App.
There is another effort to make scripting a compiler phase that is easily customized. Scroll to Scripter.scala for code comments about its current heuristics.
In short, your defs must be wrapped in a top-level entity, but exactly how that happens is context-dependent.
There was a recent effort to make an alternative baked-in wrapping scheme available for the REPL.
None of this is mandated by the language spec, any more than special rules pertaining to sbt build files are defined by the language.
You can define methods like this only in the console, which (behind the scenes) automatically wraps them in an anonymous class for you.
Outside of the console, there's no such luxury.
As a JVM language, Scala cannot truly create any top-level entities other than classes and interfaces.
It does, however, have the notion of a "package object" which creates the illusion of value entites (val, var and def) not enclosed in a class or trait.
See http://www.scala-lang.org/docu/files/packageobjects/packageobjects.html for information on package objects.
You can run code like this directly in Eclipse: use Scala worksheet. IntelliJ IDEA Scala plugin supports it as well.

The difference between scala script and application

What is the difference between a scala script and scala application? Please provide an example
The book I am reading says that a script must always end in a result expression whereas the application ends in a definition. Unfortunately no clear example is shown.
Please help clarify this for me
I think that what the author means is that a regular scala file needs to define a class or an object in order to work/be useful, you can't use top-level expressions (because the entry-points to a compiled file are pre-defined). For example:
println("foo")
object Bar {
// Some code
}
The println statement is invalid in the top-level of a .scala file, because the only logical interpretation would be to run it at compile time, which doesn't really make sense.
Scala scripts in contrast can contain expressions on the top-level, because those are executed when the script is run, which makes sense again. If a Scala script file only contains definitions on the other hand, it would be useless as well, because the script wouldn't know what to do with the definitions. If you'd use the definitions in some way, however, that'd be okay again, e.g.:
object Foo {
def bar = "test"
}
println(Foo.bar)
The latter is valid as a scala script, because the last statement is an expression using the previous definition, but not a definition itself.
Comparison
Features of scripts:
Like applications, scripts get compiled before running. Actually, the compiler translates scripts to applications before compiling, as shown below.
No need to run the compiler yourself - scala does it for you when you run your script.
Feeling is very similar to script languages like bash, python, or ruby - you directly see the results of your edits, and get a very quick debug cycle.
You don't need to provide a main method, as the compiler will add one for you.
Scala scripts tend to be useful for smaller tasks that can be implemented in a single file.
Scala applications on the other hand, are much better suited when your projects start to grow more complex. They allow to split tasks into different files and namespaces, which is important for maintaining clarity.
Example
If you write the following script:
#!/usr/bin/env scala
println("foo")
Scala 2.11.1 compiler will pretend (source on github) you had written:
object Main {
def main(args: Array[String]): Unit =
new AnyRef {
println("foo")
}
}
Well, I always thought this is a Scala script:
$ cat script
#!/usr/bin/scala
!#
println("Hello, World!")
Running with simple:
$ ./script
An application on the other hand has to be compiled to .class and executed explicitly using java runtime.

Can I execute a scala code file as a part of my code?

I woud like my application to execute external scripts written in a Scala-based DSL i've developed. That's why I need something like this to work:
...
val a = evaluate("~/myextcode.scala")
...
myextcode.scala:
return 123
Can I reach some thing like this in Scala?
You can do that with Eval from twitter util. Check its Scaladocs here.

Scala searches for an obscure Main class

I have what should be a simple scala script that looks as follows:
object SaveTaggedSenseTask {
def main(args: Array[String]) {
val reader:SenseEvalAllWordsDocumentReader = new SenseEvalAllWordsDocumentReader()
reader.parse(args(0))
println(reader.sentences)
reader.sentences()
}
}
The SenseEvalAllWordsDocumentReader is a java defined class that is just a wrapper for a SAX parser. Calling sentences should simply return a java List of of another java defined class called Sentence. If i run this code using
scala -cp jar-with-everything.jar SaveTaggedSenseTask.scala path/to/file.xml
I get the following horrible mess of an output:
java.lang.ClassNotFoundException: Main (args = /home/stevens35/senseEval/senseEval3-allwords/english-all-words.xml, classpath = /tmp/scalascript7300484879512233483.tmp:/usr/java/jdk1.6.0_26/jre/lib/resources.jar:/usr/java/jdk1.6.0_26/jre/lib/rt.jar:/usr/java/jdk1.6.0_26/jre/lib/jsse.jar:/usr/java/jdk1.6.0_26/jre/lib/jce.jar:/usr/java/jdk1.6.0_26/jre/lib/charsets.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/jline.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scala-compiler.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scala-dbc.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scala-library.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scalap.jar:/home/stevens35/devel/src/scala-2.9.1.final/lib/scala-swing.jar:/usr/java/jdk1.6.0_26/jre/lib/ext/sunjce_provider.jar:/usr/java/jdk1.6.0_26/jre/lib/ext/sunpkcs11.jar:/usr/java/jdk1.6.0_26/jre/lib/ext/localedata.jar:/usr/java/jdk1.6.0_26/jre/lib/ext/dnsns.jar:/home/stevens35/devel/C-Cat/wordnet/.:/home/stevens35/devel/C-Cat/wordnet/target/extendOntology-wordnet-1.0-jar-with-dependencies.jar:/home/stevens35/devel/C-Cat/wordnet/../data/target/extendOntology-data-1.0.jar)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:103)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
at scala.tools.nsc.ScriptRunner.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:171)
at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)
at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply$mcZ$sp(ScriptRunner.scala:157)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)
at scala.tools.nsc.util.package$.waitingForThreads(package.scala:26)
at scala.tools.nsc.ScriptRunner.withCompiledScript(ScriptRunner.scala:130)
at scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:188)
at scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:201)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:58)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
I'm assuming that this means scala is looking for some class named Main and can't find it, but I can't figure out why it would even think of looking for this. Furthermore, if i delete the reader.sentences() line, or do something like call size() on it, the problem goes away. I can only guess that scala is somehow inferring that a class named Main should exist due to this call, but I don't see any obvious workaround.
Thoughts? Any help is greatly appreciated.
You are conflating Scala script and scala program. When using object and def main, you should compile the program with scalac, and then call it with scala passing the name of the object that contains the main method.
When calling as a script, you should remove object and def main, and just put your program. The arguments will still be in args.
Now, one of the features of Scala 2.9.1 is that one could mix script and program invocation, but it is obviously not working here for some reason. I suggest you pick one way of doing things, and stick to that.
Your script defines the object SaveTaggedSenseTask, but doesn't execute anything.
Just add
SaveTaggedSenseTask.main(args)
to the end of the script file. Or you can remove the object and def statements, like #daniel-c-sobral suggested.