setting up scala for jupyter notebook - scala

Hi i'm running trouble setting up scala on jupyter notebook.
I already created the environment on conda's command prompt but unlike python or julia environments i prepared before , this one is returning "Intitializing Scala interpreter ...".
reading logs i think there's an interpreter finder called when this happens and it's failing to find interpreter.
Ideas on how to circumvent this issue ?
tyvm
result when executing simple scala code
val x:Int =1
val y:Int =7
var z:Int = x+y
Intitializing Scala interpreter ...

Do you have scala exe in your path? Check if you can just run the scala interpreter from the command prompt.

Related

How to setup different Scala versions on the same machine?

I want to follow the book on Scala[1] but it uses Scala 3 and I have Scala 2 installed. I want to use both the versions, something on the lines of python2 and python3.
I tried installing Scala3 on my local using the official source but I could only grasp the project-level working directory. The sbt prompt does not work like a REPL would and I can only open REPL using Scala 2 (I checked the version everytime).
How do I open the REPL of Scala3 given I cannot uninstall Scala2?
The sbt prompt does not work like a REPL
If you execute sbt console from within project directory it will drop you into REPL version corresponding to the project's scalaVersion. For example, executing sbt console within project created with sbt new lampepfl/dotty.g8 would start Scala 3 REPL.
but I could only grasp the project-level working directory
For system-wide installation first install coursier and then execute cs install scala3-repl. This will install Scala 3 REPL alongside the Scala 2 one. Now Scala 3 REPL can be started with scala3-repl command whilst Scala 2 REPL simply with scala command.

Running scala in cmd makes i look like I am missing 'build.sbt'

I'm trying to run Scala in my command line.
I checked my java, went to the Scala website, downloaded and installed it, updated my environment variables.
So far the only thing different from guides online is that the folder where sbt is installed does not include a "lib" folder.
I then run sbt command in my prompt, and I get this message:
It looks like I'm missing a file called build.sbt, what is this? and do i need it?
Edit:
If I press 'continue' on the picture above, I get
sbt:scalaproj>
Which looks fine, but if i type some code, like this:
sbt:scalaproj> var a : Int = 12;
Then it returns errors:
[error] Expected ';'
[error] var a : Int = 12
What in the world is going wrong? can someone point me to a guide for writing Scala in the prompt that is not too old to work?
Let's first understand the terminology. Scala is the language you are writing. SBT is an acronym for Scala Build Tool. Both of them have REPL.
When you call sbt in the command line, you initiate the REPL of sbt. The commands you can run there, are all commands sbt supports. You can find here the common commands. For example, if you run compile, it will compile the build.sbt located at the directory where you called the sbt command. Anyway, Scala commands WILL NOT work here. Scala commands are not sbt commands.
In order to run Scala REPL, you need to type console in the sbt REPL. You can find here the Scala REPL documentation. Within the Scala REPL you can run Scala commands.
P.S.
You can find the Scala download page here.

How to run Scala REPL commands in ammonite REPL/SHELL?

I mean commands like :t :type to check type of expressions or any other kinds of commands.
The idea behind ammonite really attracts me and now I'm trying to use it to get more familiar with scala.
These commands are helpful to me as a beginner, but are syntax errors in amm shell.
I've gone through the documentation of ammonite.io but cann't find anything related mentioned.
Is it possible to run these kind of commands in ammonite shell/repl ?
You cannot run Scala REPL commands in Ammonite, because they are Scala REPL commands, not Ammonite commands.
Ammonite is a completely different program from the Scala REPL, its command language is different. This is like trying to run Haskell code in a JavaScript REPL.
In particular, Ammonite prefers using Scala over a magic separate command language, so in Ammonite commands to the REPL are issued as normal Scala method calls.
There are two objects that are imported by default, repl and interp, that allow you to interact with the API of the REPL and the interpreter. For example, for your question about how to get the type of an object, you would use the ReplAPI.typeOf[T: WeakTypeTag](t: => T): Type method:
repl.typeOf("3" + 2)
//=> res: reflect.runtime.package.universe.Type = TypeRef(ThisType(package lang), class String, List())

How can I get scala to work in the command line?

I run Windows 7.
I have java (a current enough version to run scala) and scala downloaded on my computer. I've set PATH so that when I type "scala" into the command prompt it sends me to the proper interface:
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51)
Type in expressions to have them evaluated.
Type :help for more information
However I can't execute the command "scala" or "scalac" on my test files.
scala> scala Hello
(console):8: error: object Hello is not a member of package scala
This makes me think I'm in the wrong directory. The file Hello.scala is saved in the home directory that I set PATH to.
However I get a different issue when I try to compile code.
scala> scalac Hello.scala
(console):1: error: ';' expected but '.' found.
I actually got my test file to work at one point... but I wasn't actually IN scala.
C:\scala-2.9.1.final\bin> scala Hello.scala
Hello world!
I'm not really sure how to proceed from here. If anyone has any ideas of what may be wrong I would greatly appreciate input.
It appears that you're trying to run & compile programs from within the Scala REPL (read-evaluate-print loop - a kind of Scala interpreter) and you cannot do that in the REPL. The REPL allows you to type in Scala statements and see them execute immediately. (If you're not sure how you entered the REPL, you probably just entered the command scala from the command line.) The REPL is useful for testing ideas, and for experimenting with Scala. For example:
C:\some\path> scala
Welcome to Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_91).
Type in expressions for evaluation. Or try :help.
scala> println ("Hello!")
Hello!
scala> val x = 10
x: Int = 10
scala> val y = x * 5
y: Int = 50
scala> sys.exit
C:\some\path>
However, the REPL isn't what you would use to compile & run Scala programs - you need to do that from the command line (or from a tool such as sbt). If you want to run your program directly from the command line, without using the REPL (that is, without being in Scala, as you put it) then you would need to do the following:
Firstly, compile your program using scalac:
C:\some\path> scalac Hello.scala
If that succeeds, you can then run the program with the scala command (which looks for a Hello.class file):
C:\some\path> scala Hello
(Here C:\some\path is the location of the files Hello.scala & Hello.class.)
Alternatively, as you have already discovered, you can run your Scala program as a script in the REPL. You can do this from the command line as follows (note the addition of the filetype .scala after Hello compared to the command above):
C:\some\path> scala Hello.scala
or from within the REPL:
scala> :load Hello.scala
Hope this helps!
You don't have to issue scala command when you're inside REPL. If you want to execute code from that file, load it:
here is what I have in Foo.scala
println("I'm foo")
Now I'm starting the REPL (and as you can see scala> is a sign that you're ALREADY into REPL and can start execute raw scala code):
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :load Foo.scala
Loading Foo.scala...
I'm foo

Does Scala having an interpreter give Scala projects the option to execute them either compiled or interpreted? [duplicate]

This question already has answers here:
The difference between scala script and application
(3 answers)
Closed 8 years ago.
I'm aware that scala has an interpreter and scala is statically typed. So I'm wondering if it is possible to execute scala projects in both Java and PHP style ?
Maybe you just need an interpreter to test your code? Then type scala to get interpreter and use :load command to load scala file.
I don't know exactly what is the PHP style, but yes, you can execute scala interactively, static typing is not a big issue here. If you need to exectue simple script that's easy (code from "Getting started in Scala"):
#!/bin/sh
exec scala "$0" "$#"
!#
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world! " + args.toList)
}
}
HelloWorld.main(args)
If you have .jar dependencies in your script than things got more complicated,because you need to pass this jar dependencies to the scala interpreter. here is example
As of current date this method doesn't allow you to modularize scripts into the multiple files, but here is workaround
If you have sbt project you can type console from sbt shell to get a scala interpreter with correct classpath and dependencies. Also sbt itself has a 'script' mode which works quite like groovy's embedded dependency menegment.
Also scala compiler is embeddable) This project helps to dynamically compile/recompile scala files and load them into the jvm.