scala初體驗


一、什么是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

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

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:

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

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:

  1. Go to “File” - “New” - “Other…” and select “Scala Project” from the folder “Scala Wizards”
  2. Chose a project name and select “Finish”
  3. Select “File” - “New” - “Scala Object” to create a new object
  4. Enter Hello as the name for the object and put greeter as the package name above
  5. Change the source code to the one given below [1]
  6. 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:

  1. Right-click on the package greeter in the hello world project that you just created
  2. Select “New” - “Scala Worksheet”
  3. 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM