scala学习1

主要内容:

1. scala原理

 1.1 A language that grows on you

  1.1.1 Scala gives you this fine-grained control if you need it, because maps in Scala are not languagesyntax. They are library abstractions that you can extend and adapt.

  1.1.2 Growing new types。Scala defines such a type in library class scala.BigInt.

  1.1.3 Growing new control constructs。Scala’s API for “actor-based” concurrent programming.

  1.2 What makes Scala scalable?

  1.2.1 Scala is object-oriented。The most straightforward way to do this is to put data and operations into some form of containers. By contrast, Scala is an object-oriented language in pure form: every value is an object and every operation is a method call. 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。In particular, it avoids the classical “diamond inheritance” problems of multiple inheritance, which arisewhen the same class is inherited via several different paths.

  1.2.2 Scala is functional。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. This generalization of functions provides great expressiveness, which often leads to very legible and concise programs.

    The second main idea of functional programming is that the operations of a program should map input values to output values rather than change data in place. To see the difference, consider the implementation of strings

in Ruby and in Java.

   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.

 1.3 Why Scala?

  1.3.1 Scala is compatible。Scala programs compile to JVM bytecodes. Their run-time performance is usually on par with Java programs. Scala code can call Java methods, access Java fields, inherit from Java classes, and implement Java interfaces. None of this requires special syntax, explicit interface descriptions, or glue code. In fact, almost all Scala code makes heavy use of Java libraries, often without programmers being aware of this fact. Scala not only re-uses Java’s types, but also “dresses them up” to make them nicer.

  1.3.2 Scala is concise

  1.3.3 Scala is high-level。val nameHasUpperCase = name.exists(_.isUpperCase)

  1.3.4 Scala is statically typed。In fact, it addresses nicely two of the usual concerns about static typing: verbosity is avoided through type inference and flexibility is gained through pattern matching and several new ways to write

and compose types. benefit: Verifiable properties,Safe refactorings,Documentation

 

 

2. scala基础语法

2.1 函数定义

def max(x: Int, y: Int): Int = {
  if (x > y) x
  else y
}

scala学习1_第1张图片

 

2.2 匿名函数

>cat anonomous_function.scala
object Timer2{
  def oncePerSecond(callback : () => Unit){
    while(true){
      callback()
    Thread sleep 1000
    }
  }
  def main(args: Array[String]) {
    oncePerSecond( () => println("time flies like an arrow..."))
  }
}

> cat Makefile
.PHONY: all clean
SRC=$(wildcard *.scala)
CLASS=$(patsubst %.scala, %.class, $(SRC))

all: $(CLASS)

$(CLASS):%.class:%.scala
scalac $<

clean:
rm -rf *.class

 

run

>scala -classpath . Timer2

 

 

reference:

prgramming in scala

你可能感兴趣的:(scala)