一、什么是scala
Scala is a language that fuses functional and object-oriented programming in a practical package. It interoperates seamlessly with Java and its tools. Scala is now used in a rapidly increasing number of open source projects and companies. It provides the core infrastructure for sites such as Twitter, LinkedIn, Foursquare, Tumblr, and Klout.(from:https://www.coursera.org/course/progfun)
scala是一种融合了面向对象和函数式编程的语言。它可以与java语言和java工具进行无缝的交互操作。scala现在被快速地应用与开源工程及开源公司中,它为Twitter, LinkedIn, Foursquare, Tumblr, and Klout等公司提供了核心的基础构建。
二、使用准备
工具配置
为了在计算机中运行相关scala程序,你需要进行如下的安装配置:
- JDK, the Java Development Kit, version 1.6 or 1.7
- Sbt, a build tool for Scala, version 0.12.0
- The Scala IDE for Eclipse and the Scala Worksheet
Please follow the instructions on this page carefully. You can also watch one of the following videos that explain how to set up your machine:
1.安装JDK
—Linux
-
Ubuntu, Debian: 在terminal中执行如下语句
sudo apt-get install openjdk-7-jdk
-
Fedora, Oracle, Red Had:
su -c "yum install java-1.7.0-openjdk-devel"
-
手动方法: To install the JDK manually on a Linux system, follow these steps:
- 下载
.tar.gz
文件(使用oracle官方网站中的jdk)
: http://www.oracle.com/technetwork/java/javase/downloads/jdk7u7-downloads-1836413.html - 解压缩文件
- 添加
环境变量
. 打开文件(在terminal中运行) “sudo gedit~/.bashrc”
并添加如下 :export PATH=/PATH/TO/YOUR/jdk1.7.0-VERSION/bin:$PATH
- 最后在terminal中运行 source ~/.bashrc 保存更改
- 验证设置是否正确: 在terminal中键入:
java -version 查看java版本
—Mac OS X
Mac OS X either comes with a pre-installed JDK, or installs it automatically.
To verify your JDK installation, open the Terminal
application in /Applications/Utilities/
and type java -version
. If the JDK is not yet installed, the system will ask you if you would like to download and install it.
—Windows
- Download the JDK installer for Windows from http://www.oracle.com/technetwork/java/javase/downloads/jdk7u7-downloads-1836413.html
- Run the installer
- Add the
bin\
directory of the installed JDK to thePATH
environment variable, as described here: http://www.java.com/en/download/help/path.xml
To verify the JDK installation, open the Command Prompt and type java -version
. If you have problems installing the JDK, ask for help on the forums.
2.安装sbt
—Linux
- 下载 sbt : http://scalasbt.artifactoryonline.com/scalasbt/sbt-native-packages/org/scala-sbt/sbt/0.12.0/sbt.tgz
- 解压缩文件
- 将文件的bin路径添加到环境变量中(与上文中的java部分相同的操作).
export PATH=/PATH/TO/YOUR/sbt/bin:$PATH
- 通过键入 sbt -h 来验证是否安装成功
Mac OS X
If you use the homebrew package manager, simply type brew update
and then brew install sbt
in a Terminal prompt.
Otherwise, install sbt by following these steps:
- Download sbt from here: http://scalasbt.artifactoryonline.com/scalasbt/sbt-native-packages/org/scala-sbt/sbt/0.12.0/sbt.tgz
- Unpack the archive to a directory of your choice
- Add the
bin/
directory to thePATH
environment variable. Open the file~/.bash_profile
in an editor (create it if it doesn’t exist) and add the following lineexport PATH=/PATH/TO/YOUR/sbt/bin:$PATH
Verify that sbt is installed correctly: Open a new terminal (to apply the changed .bash_profile
) using the Terminal
application in /Applications/Utilities/
and type sbt -h
, you should see a help message from sbt. If you have problems installing sbt, ask for help on the forums.
Windows
- Download the sbt installer from here: http://scalasbt.artifactoryonline.com/scalasbt/sbt-native-packages/org/scala-sbt/sbt/0.12.0/sbt.msi
- Run the installer
Verify that sbt is installed correctly: open the Command Prompt and type sbt sbt-version
, you should see the version number of sbt (the first time you run it, sbt will download libraries from the internet). If you have problems installing sbt, ask for help on the forums.
3.安装Scala IDE for Eclipse (Linux / Mac OS X / Windows)
在eclipse中进行自动安装
You can download the Scala IDE for eclipse with the Scala Worksheet pre-installed from the following URL:
http://typesafe.com/stack/scala_ide_download
After downloading the archive for your operating system, simply unpack it start eclipse. Eclipse requires you to select a workspace on startup. We recommmend you create one workspace directory for this class and use it for all assignments.
演示:
Hello World: Scala IDE and the Scala Worksheet
To familiarize yourself with the Scala IDE, create a small “Hello World” project in eclipse:
- Go to “File” - “New” - “Other…” and select “Scala Project” from the folder “Scala Wizards”
- Chose a project name and select “Finish”
- Select “File” - “New” - “Scala Object” to create a new object
- Enter
Hello
as the name for the object and putgreeter
as the package name above - Change the source code to the one given below [1]
- Save the file and select “Run” - “Run” from the menu. Chose to run as “Scala Application”
You should see a the hello world output in the Eclipse console.
[1] Source code
package greeter
object Hello extends App {
println("Hello, World!")
}
Creating a Scala Worksheet
Creating a Scala Worksheet is very easy:
- Right-click on the package
greeter
in the hello world project that you just created - Select “New” - “Scala Worksheet”
- Choose a name for your worksheet
Now you can type some Scala code into the worksheet. Every time you save the file, the content of the worksheet will be evaluated. Copy the following code into the object of your worksheet:
val x = 1 //> x : Int = 1
def increase(i: Int) = i + 1 //> increase: (i: Int)Int
increase(x) //> res0: Int = 2