To be a good programer, I should Know about functional programming

## What is Scala

Scala stands for Scalable language

Scala 融合了 面向对象 和 函数式 的特性。面向对象方便构建大型系统同时有利于拓展，而函数式对于处理有趣的功能点上能够快速的完成开发。（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.）

So the approach of attempting to provide
everything in one language doesn’t scale very well. Instead, Scala allows
users to grow and adapt the language in the directions they need by defining
easy-to-use libraries that feel like native language support.

One telltale sign is that if code contains any vars, it is probably in an imperative style. If the code contains no vars at all—i.e., it contains only vals—it is probably in a functional style.

def printArgs(args: Array[String]): Unit = {
var i = 0
while (i < args.length) {
println(args(i))
i += 1
}
}


def printArgs(args: Array[String]): Unit = {
args.foreach(println)
}


The telltale sign of a function with side effects is that its result type is Unit. If a function isn’t returning any interesting value, which is what a result type of Unit means, the only way that function can make a difference in the world is through some kind of side effect.

def formatArgs(args: Array[String]) = args.mkString("\n")


Scala is not a pure functional language that forces you to program
everything in the functional style. Scala is a hybrid imperative/functional language.

### 基础问题记录

Array 是可变的

List 是不可变的

Set 和 Map 同时又可变和不可变两种

immutable set 没有 += 方法， 但是 mutable set 有 += 方法，所以不可以在immutable set上调用 .+=(...)

Function

syntax of a function literal : (x: Int, y: Int) => x + y

Tuple

Tuple 的下标是从1 (xx._1)开始的，这是出于为了和首先使用tuple的语言保持一致

scala 不存在操作符重载的概念，因为scala中并不包含操作符，只不过我们可以使用*, +, -, / 等符号作为方法名

Higher-Order Function

In functional programming, a visitor with fields is called a closure (or a higher-order function), which would be the result of applying a curried version of subst

—-A Little Java, A Few Patterns

Scala 的类型系统

Unit —> void in Java