Introduction to Scala

Scala is a general-purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way.

Scala supports both Object-oriented programming and Functional programming.

Scala can be used as a drop-in replacement for Java(Mixed Scala/Java Project).
Uses existing Java libraries.
Uses existing Java tools(Ant, Maven, JUnit, etc).
Has IDE support(NetBeans, Eclipse, Intellij).

Scala Basic Syntax

When considering a Scala program, it can be defined as a collection of objects that communicate via invoking each other's methods.

  • Object: same as in Java
  • Class: same as in Java
  • Methods: same as in Java
  • Fields: Each object has its unique set of instance variables, which are called fields. An object's state is created by the values assigned to these fields.
  • Traits: Like Java interface. A trait encapsulates method and field definitions, which can then be reused by mixing them into classes.
  • Closure: A closure is a function, whose return value depends on the value of one or more variables declared outside this function.

Closure = function + environment

Scala is object-oriented, and is based on Java's model.
An object is a singleton object.

  • Variables and methods in an object are somewhat similar to Java's static variables and methods.
  • Reference to an object's variables and methods have the syntax ObjectName.methodOrVariableName.

A class may take parameters, and may describe any number of objects.

  • Tha class body is the constructor, but you can have additional constructors.
  • Write correct use of val or var, Scala provides getters and setters for class parameters.

Scala is statically typed.
You don't have to specify a type in a class.
Type Interface:

val sum = 1 + 2 + 3
val nums = List(1 ,2, 3)
val map = Map("abc" -> List(1,2,3))

Scala Constructor


Introduction to Scala_第1张图片
Scala Constructor

Variables: values stored can be changed.

var foo = "foo"
foo = "bar"

Values: immutable variable.

var foo = "foo"
foo = "bar" //nope

Scala is pure object-oriented

  • Every value is an object.
toString
  • Every operation is a method call.
1 + 2 + 3 -> (1).+(2).+(3)
  • Can omit . and ( )
 "abc" charAt(1) -> abc.charAt(1)
  • Classes and abstract class like Java
abstract class Language(val name:String){
  override def toString = name
}
  • Example implementations.
Class Scala extends Language("Scala")
  • Anonymous class
val scala = new Language("Scala"){/*empty*/}

Scala Traits

trait JVM{
  override def toString = super.toString() + "runs on JVM"
}

class Scala extends Language with JVM{
  val name = "Scala"
}
print(new Scala)     //Scala runs on JVM

Scala is functional
First-class Functions. Functions are treated like objects.

  • passing functions as arguements to other functions.
  • returning functions as the values from other functions.
  • assigning functions to variables or storing them in data structures.
//Lightweight anonymous functions
(x:Int) => x + 1
//Calling anonymous functions
val plusone = (x:Int) => x + 1
plusone (5) -> 6

Closures: a function whose return value depends on the value of one or more variables declared outside this function.

var foo = 1
val plusFoo = (x:Int) => x + foo

Higher order functions
A function that does at least one of the following

  • takes one or more functions as arguements.
  • returns a function as its result.
val plusOne = (x:Int) => x + 1
val num = List(1,2,3)
num.map(plusOne)
num.map(x=>x+1)
num.map(_+1)

val nums = List(1,2,3,4)
nums.exists(_==2)
nums.find(_==2)
nums.indexWhere(_==2)

The usage of "_" in Scala

In anonymous functions, the "_" acts as a placeholder for parameters.

nums.map(x=>x+1)
is equivalent to 
nums.map(_+1)

List(1,2,3,4,5).foreach(print(_))
is equivalent to 
List(1,2,3,4,5).foreach(a=>print(a))

You can use two or more underscores to refer different parameters.

val sum = List(1,2,3,4,5).reduceLeft(_+_)
is equivalent to 
val sum = List(1,2,3,4,5).reduceLeft((a,b)=>a+b)

The reduceLeft method works by applying the function/operation you give it, and applying it to successive elements in the collection.

你可能感兴趣的:(Introduction to Scala)