-
Notifications
You must be signed in to change notification settings - Fork 2
Scala
🚧 NEW PAGE!
Scala is a Scalable Language. It is a functional object-oriented language intended to be compiled into Java bytecode (.class files).
Scala requirest two things: Java and [Apache Ant](Apache Ant). I've got instructions for both. Use them. There's also a Ruby gem, but let's not worry about that right now. [curl and tar](curl and tar) are required.
Initially, I wanted to folow the "quick build" instructions from Scala's Github. It seemed simple enough. Clone it, go to the scala folder then run ant because there was a build.xml file right? WRONG! Aparently despite there being a JAR file called scala/lib/ant/ant-contrib.jar, build.xml was written poorly and could not find the file that existed.
Using SBT, the Simple Build Tool, that Scala uses, I though maybe I could download and install Scala that way as it was suggested in the #scala chat room on Freenode, right? ALSO WRONG! To get any of that working you needed SBT to build SBT. That would sound great if we were doing and upgrade, but is is uselless if we are trying to install.
Fortunately, someone else came up with an idea for installing Scala and SBT for Raspberry Pi, and another person came up with more refined instructions, of which I'm adding my own twist to these instructions.
I've decided to modify some of the instructions of the Scala and SBT installations namely to conform with how we have Java, Ruby, and JRuby set up. Scala and SBT wanted you to write scripts in /usr/bin, which is improper. What we will do is write the script, but we will use soft links.
$ cd ~/Software
$ mkdir scala # We want to put all of our scala stuff in a folder called scala
$ cd scala
$ curl -SL http://downloads.lightbend.com/scala/2.11.8/scala-2.11.8.tgz | tar xvz
$ cd .. # Go back to ~/Software
$ sudo mv scala /opt/
$ cd /opt/scala
$ cd scala-2.11.8/
$ sudo ln -s /opt/scala/scala-2.11.8/bin/scalac /usr/local/bin/scalac
$ sudo ln -s /opt/scala/scala-2.11.8/bin/scala /usr/local/bin/scala
Let's see. If everything looks like this
$ whereis scalac
scalac: /usr/local/bin/scalac
$ which scalac
/usr/local/bin/scalac
$ whereis scala
scala: /usr/local/bin/scala
$ which scala
/usr/local/bin/scala
$ scalac -version
Scala compiler version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL
$ scala -version
Scala code runner version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL
Then yeah it should be ready to go. Let's try a couple examples.
$ cd ~/Sandbox
$ mkdir scala
$ cd scala
Then in Vim or your favorite text editor, let's write HelloWorld.scala.
/** File: HelloWorld.scala
* Info: Hello World example for Scala
*/
object HelloWord {
def main(args: Array[String]) {
println("Hello, world!")
}
}
And compile it and running. I've added a couple of ls commands to show what is generated.
$ ls
HelloWorld.scala
$ scalac hello.scala
$ ls
HelloWorld.class HelloWorld$.class HelloWorld.scala
$ scala HelloWorld
Hello, world!
See how very similar Scala is to Java? There a big difference between Java and Scala. Scala has a REPL, meaning you can test that code with scala. Note, the pipes are added by the REPL for indentation.
$ scala
Welcome to Scala 2.11.8 (Java HotSpot(TM) Client VM, Java 1.8.0_73).
Type in expressions for evaluation or try :help.
scala> object HelloWorld {
| def main(args: Array[String]) {
| println("Hello, world!")
| }
| }
defined object HelloWorld
scala> HelloWorld.main(null)
Hello, world!
scala> :q
$
Pretty cool, right? Well, I though it was.
But wait, there's more! You can run Scala in a Bash script!
#!/bin/bash
# File: HelloWorld.sh
exec scala "$0" "$@"
!#
object HelloWord {
def main(args: Array[String]) {
println("Hello, world! " + args.toList)
}
}
HelloWorld.main(args)
Then in bash
$ chmod u+x HelloWorld.sh
$ ./HelloWorld.sh
Hello, World! List()
Hmm...something's not right here. Let's try that println line again without args.toList. Basically use the original line we had from our origninal HelloWorld.scala
#!/bin/bash
# File: HelloWorld.sh
exec scala "$0" "$@"
!#
object HelloWord {
def main(args: Array[String]) {
println("Hello, world!")
}
}
HelloWorld.main(args)
OK, let's give it anothe shot.
$ ./HelloWorld.sh
Hello, World!
Much better.
Scaladoc generally follows the same style as Javadoc from Java. If you want to know more about that click here
For some reason, some versions of this Hello World example add : Unit = in between the closing parenthesis of the main function and the opening curly bracket. You don't need that. Also, like with Python or Ruby there are no semicolons at the end of the line
Also, if you want to put your generated class file into some other location, you should use the -d option.
$ scalac -d classes HelloWorld.scala
And for bytecode, use of the -classpath or -cp options are available.
$ scala -classpath classes HelloWorld
One more thing, Some tutorials have instead of def main(args: Array[String]) to define a main method, and extension of the App class.
/** File: HelloWorld2.scala
* Info: A better Hello, World!
*/
object HelloWorld2 extends App {
println("Hello, World!")
}
This is probably a much more convient way to define your main module, though when compiling
$ scalac HelloWorld2.scala
Three files are created.
$ ls
HelloWorld2.class HelloWorld2$.class HelloWorld2$delayedInit$body.class HelloWorld2.scala
I put this off to last because I had so much trouble with this. And like with Scala, you just can't get it from the Github.
The other problem was I kept getting this "security certificates error", which I had enough of that nonsense. If it gives you that, add the -k option to the curl command, like below.
$ cd /opt/scala
$ curl -kSL https://dl.bintray.com/sbt/native-packages/sbt/0.13.11/sbt-0.13.11.tgz | tar xvz
$ cd sbt
Here's where is get's complicated, we need to write a script. The SBT guys want us to put it in ~/bin, but if you want everyone to actually use it, you'll want to write it in your home directory then move it to /usr/local/bin. This script will be called sbt.
#!/bin/bash
java -server -Xmx512M -jar `dirname $0`/sbt-launch.jar "$@"
Save it and then make it executable.
$ chmod +x sbt
$ sudo mv sbt /usr/local/bin
$ sbt
When you first run sbt the first time around, it will take some time, especially since it is downloading everything it needs to ~/.sbt/. Use sbt where there is a build.sbt file much like build.xml is needed for Apache Ant.
Alvin Alexander has a script for setting up an SBT project that can be structured for Github. I'm thinking of adding an improved version with some better Bash expressions.
- http://www.scala-lang.org/
- https://github.com/scala/scala/
- http://www.scala-sbt.org/
- https://github.com/sbt/sbt/
- http://alvinalexander.com/photos/how-install-scala-scala-scalac-raspberry-pi-system
- https://pavelfatin.com/install-scala-on-raspberry-pi
- http://alvinalexander.com/sbtmkdirs
- https://github.com/harrah/xsbt/wiki/
- http://www.scala-lang.org/old/node/166.html
Setup
- [Assemble the Hardware](Assemble the Hardware)
- [Install the Software](Install the Software)
- SD Card Formatting and Image Mounting
- ❤️ [TLDR version](Install the Software#tldr)
- 🆕 [Windows version](Install the Software#doing-this-in-windows)
- 🆙 [Setup your Raspberry Pi](Setup your Raspberry Pi)
- [Download the Missing Parts](Download the Missing Parts)
- Clone this repo
- 🆕 [Install these first](Install these first)
-
Python(It's taken care of) - 🆙 CMake
- 🆙 Node
- 🆕 Expect (Tcl/Tk is not dead!)
- 🆙 tmux and ncurses
- 🆙 htop
- 🆙 wavemon
- Vim
- 🆙 Ruby
- 🆙 weechat
- 🆕 urxvt 256 color terminal!
- 🆕 i3 A better window manager
- 🆕 Ranger
- 🆙 Qt5
- 🆕 Chromium
wicd-curses- 📻 Software Defined Radio
- 🎮 [Linux Toys](Linux Toys)
Typical Utilities
- [Downloading and extracting with curl and tar](curl and tar)
- [Browsing with ls and cat](ls and cat)
- [Searching with grep and find](grep and find)
- [Filtering with sed and awk](sed and awk)
- [Piping with less, pv, and tee](less, pv, and tee)
- Monitor your system with htop
- Multiplex with tmux