为什么选择Scala ?

1.The name Scala stands for “scalable language.” The language is so named because it was designed to grow with the demands of its users

Scala代表可伸缩的语言。之所以这样命名是因为它被设计成可随着用户的需求扩展的语言


2.Technically, Scala is a blend of object-oriented and functional programming concepts in a statically typed language.

从技术角度讲,Scala在一个静态语言中,实现了面向对象和函数式 编程的融合


3.Scala’s functional programming constructs make it easy to build interesting things quickly from simple parts. Its object-oriented constructs make it easy to structure larger systems and to adapt them to new demands. The combination of both styles in Scala makes it possible to express new kinds of programming patterns and component abstractions. It also leads to a legible and concise programming style. And because it is so malleable,programming in Scala can be a lot of fun.

函数式让我们能够快速从简单的部分入手编写有意思的代码

面向对象使得构建大型系统和复用代码变得简单

两种编程风格结合使表达新的编程模式和组件抽象成为可能,并且代码简洁易读

它是如此有伸缩性,用Scala写程序一定会很有趣


4. 环境搭建

Scala官网下载eclipse IDE


5.Map示例

object TestMain {
  def main(args: Array[String]) {
    var seasons = Map(
      "Spring" -> "春天",
      "Summer" -> "夏天",
      "Fall" -> "秋天",
      "Winter" -> "冬天")

    println(seasons("Spring"))
    
    seasons += ("Autumn" -> "秋天")
    println(seasons("Autumn"))  
  }

}

6.阶乘函数

	def fact(x: BigInt): BigInt = 
	  if (x == 0) 1
	  else x * fact(x-1)

7.直接使用Java的BigInteger重写阶乘函数

package chapter1

import java.math.BigInteger
object TestMain {
  def main(args: Array[String]) {
	def fact(x: BigInteger): BigInteger = 
	  if (x == BigInteger.ZERO)  BigInteger.ONE
	  else  x.multiply(fact(x.subtract(BigInteger.ONE)))
	  
	println(fact(BigInteger.valueOf(300)))  
  }

}

8. 更加容易组装对象

Scala is more advanced than most other languages when it comes to composing objects. An example is Scala’s traits. Traits are like interfaces in Java, but they can also have method implementations and even fields. Objects are constructed by mixin composition, which takes the members of a class and adds the members of a number of traits to them. In this way, different aspects of classes can be encapsulated in different traits. This looks a bit like multiple inheritance, but differs when it comes to the details. Unlike a class, a trait can add some new functionality to an unspecified superclass. This makes traits more “pluggable” than classes.


9. 函数式编程的两个主要思想

Functional programming is guided by two main ideas. The first idea is that functions are first-class values. In a functional language, a function is a value of the same status as, say, an integer or a string. You can pass functions as arguments to other functions, return them as results from functions,or store them in variables. You can also define a function inside another function, just as you can define an integer value inside a function

函数是第一类值

the operations of a program should map input values to output values rather than change data in place.

Another way of stating this second idea of functional programming is that methods should not have any side effects. They should communicate with their environment only by taking arguments and returning results.

一个程序应该把输入值映射为输出值,一个函数也应该这样。或者说,函数式编程中的方法不应有任何副作用,也就是说函数与环境的唯一交互就是获取参数


10. Scala 比Java更加high-level。例如 查找字符串是否出现大写

package chapter1;

public class TestMain {
	public static void main(String[] args) {
		String names = "albert Ding";
		boolean nameHasUpperCase = false;
		for (int i= 0; i < names.length(); i++) {
			if (Character.isUpperCase(names.charAt(i))) {
				nameHasUpperCase = true;
			}
		}
		System.out.println(nameHasUpperCase);  // true
	}

}
package chapter1

import java.math.BigInteger
object TestMain {
  def main(args: Array[String]) {
	  val name = "albert Ding"
	  val nameHasUpperCase = name.exists(_.isUpper)
	  println(nameHasUpperCase) // true
  }

}

下划线在Scala中可谓“万能替换”


11. 与众不同的静态类型语言

A static type system classifies variables and expressions according to the kinds of values they hold and compute.

静态类型系统中每一个变量每一个语句都是有类型的

Scala是一个出色的静态系统,以至于你都感觉不到它是静态类型的

Scala之前,人们对静态系统的评价是:static types are that they make programs too verbose,prevent programmers from expressing themselves as they wish

静态类型会使程序过于冗长,程序员的思想会被类型标识符掩盖。 

但是Scala改变了这种看法,在Scala中你可以不带任何类型声明地写很多代码

12. 静态类型系统有很多优势,比如可以在编译时检查出动态类型在运行时才能发现的错误


13. Scala’s design has been influenced by many programming languages and ideas in programming language research.

Scala的设计受到很多编程语言的影响,还有编程语言研究中的思想的影响

Its uniform object model was pioneered by Smalltalk and taken up subsequently by Ruby

它的统一对象模型的先驱者是: Smalltalk 和Ruby

Its idea of universal nesting (almost every construct in Scala can be nested inside any other construct) is also present in Algol, Simula, and, more recently in Beta and gbeta.

全局嵌套的思想(你可以将任何东西嵌套于任何东西)也在Algol, Beta, gbeta这些语言中出现

Its uniform access principle for method invocation and field selection comes from Eiffel

方法调用和属性选择的统一获取原理来自Eiffel

Its approach to functional programming is quite similar in spirit to the ML family of languages, which has SML, OCaml, and F# as prominent members. 

函数式编程的实现受到ML语言家族的影响

Many higher-order functions in Scala’s standard library are also present in ML or Haskell

Scala标准库中的很多高阶函数中ML和Haskell 中也有出现

Scala’s implicit parameters were motivated by Haskell’s type classes

Scala的类型参数受Haskell的类型类的启发

Scala’s actor-based concurrency library was heavily inspired by Erlang.

Scala基于Actor的并非库受到Erlang的很大影响


14. Scala使用 variable: Type格式的解释

The major deviation from Java concerns the syntax for type annotations—it’s

“variable: Type” instead of “Type variable” in Java. Scala’s postfix type syntax resembles Pascal, Modula-2, or Eiffel. The main reason for this deviation has to do with type

inference, which often lets you omit the type of a variable or the return type of a method.

Using the “variable: Type” syntax this is easy—just leave out the colon and the type. But

in C-style “Type variable” syntax you cannot simply leave off the type—there would be no

marker to start the definition anymore. You’d need some alternative keyword to be a placeholder for a missing type (C# 3.0, which does some type inference, uses var for this purpose).

Such an alternative keyword feels more ad-hoc and less regular than Scala’s approach

这样写能更方便地省去类型