Home >Java >javaTutorial >An introductory tutorial to Scala for Java programmers

An introductory tutorial to Scala for Java programmers

高洛峰
高洛峰Original
2016-11-22 16:14:321998browse

Java 8 has some preliminary functional programming capabilities: closures, etc., as well as a new concurrent programming model and Stream, a data collection with high-order functions and delayed calculations. After trying Java 8, you may feel that you still have more to learn. Yes, you will find that Scala can satisfy your thirst for knowledge after trying functional programming for the first time.

Install Scala

Download from the official Scala download address: http://scala-lang.org/download/:

wget -c http://downloads.lightbend.com/scala/2.11.8/scala-2.11.8.tgztar zxf scala-2.11.8.tgz
cd scala-2.11.8./bin/scala
Welcome to Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions to have them evaluated.Type :help for more information.

scala>

RELP

Just now we have started Scala RELP, which is a command line-based interaction programming environment. This is a very common tool for students with dynamic languages ​​such as Python and Ruby. But Javaers will find it more magical when they see it for the first time. We can do some code attempts in RELP without starting a clumsy IDE, which is very convenient when we think about problems. There is good news for Javaers, JDK 9 will have built-in support for RELP functionality.

For IDEs (integrated development environments) commonly used in Scala, it is recommended to use IDEA for scala plugins and scala-ide.

The power of Scala, in addition to its better support for multi-core programming, functional features and some Scala-based third-party libraries and frameworks (such as: Akka, Playframework, Spark, Kafka...), also lies in its ability to Seam combined with Java. All libraries and frameworks developed for Java can be naturally integrated into the Scala environment. Of course, Scala can also be easily integrated with Java environments, such as Spring. If you need the support of a third-party library, you can use Maven, Gradle, Sbt and other compilation environments to introduce it.

Scala is an object-oriented programming language with functional features. It inherits the object-oriented features of Java, and at the same time absorbs and enhances many functional features from other languages ​​such as Haskell.

Variables, basic data types

Variables in Scala do not need to explicitly specify the type, but they need to be declared in advance. This avoids many namespace pollution problems. Scala has a very powerful automatic type deduction function, which can automatically deduce the type of variables based on rvalues ​​and context. You can declare and assign values ​​directly as follows.

scala> val a = 1
a: Int = 1

scala> val b = true
b: Boolean = true

scala> val c = 1.0
c: Double = 1.0

scala> val a = 30 + "岁"
a: String = 30岁

Immutable

(Note: Functional programming has a very important feature: immutability. In addition to the immutability of variables, Scala also defines a set of immutable collections scala.collection.immutable._.)

Val means this is a final variable, which is a constant. Once defined, it cannot be changed. Correspondingly, variables defined using var are common variables and can be changed. As you can see from the terminal print, Scala automatically deduces the type of the variable from the rvalue. Scala can write code like a dynamic language, but has the compile-time checks of a static language. This is a good improvement over the long, repetitive type declarations in Java.

(Note: In RELP, val variables can be reassigned, which is a feature of RELP`. This is not possible in ordinary code.)

Basic data types

Basic data types in Scala are: Byte, Short, Int, Long, Float, Double, Boolean, Char, String. Unlike Java, Scala does not distinguish between native types and boxed types, such as int and Integer. It is unified and abstracted into the Int type, so that all types are objects in Scala. The compiler will automatically decide whether to use native or boxed types at compile time.

Strings

There are 3 types of strings in Scala.

are ordinary strings respectively, and their characteristics are the same as Java strings.

Connecting three double quotes also has a special meaning in Scala. It means that the wrapped content is an original string and does not require character transcoding. This feature is very advantageous when defining regular expressions.

There is also a type of string called "string interpolation", which can directly reference variables in the context and insert the result into the string.

scala> val c2 = '杨'
c2: Char = 杨

scala> val s1 = "重庆誉存企业信用管理有限公司"
s1: String = 重庆誉存企业信用管理有限公司

scala> val s2 = s"重庆誉存企业信用管理有限公司${c2}景"
s2: String = 重庆誉存企业信用管理有限公司

scala> val s3 = s"""重庆誉存企业信用管理有限公司"工程师"\n${c2}景是江津人"""
s3: String =
重庆誉存企业信用管理有限公司"工程师"
杨景是江津人

Operators and naming

Operators in Scala are actually methods (functions) defined on objects. What you see like: 3 + 2 actually looks like this: 3.+(2). The + symbol is a method defined on the Int object. Supports the same operators (methods) as Java:

(Note: In Scala, the . sign before the method and the parentheses on both sides of the method can be omitted without causing ambiguity. In this way, we can define many Beautiful DSL)

==, !=: comparison operations

!, |, &, ^: logical operations

>>, 6067b1d153ae62510ce0427096536e98是定义Tuple2的一种便捷方式。

scala> map + ("z" -> "Z")
res23: scala.collection.immutable.Map[String,String] = Map(a -> A, b -> B, z -> Z)

scala> map.filterNot(entry => entry._1 == "a")
res24: scala.collection.immutable.Map[String,String] = Map(b -> B)

scala> val map3 = map - "a"
map3: scala.collection.immutable.Map[String,String] = Map(b -> B)

scala> map
res25: scala.collection.immutable.Map[String,String] = Map(a -> A, b -> B)

Scala的immutable collection并没有添加和删除元素的操作,其定义+(List使用::在头部添加)操作都是生成一个新的集合,而要删除一个元素一般使用 - 操作直接将Key从map中减掉即可。

(注:Scala中也scala.collection.mutable._集合,它定义了不可变集合的相应可变集合版本。一般情况下,除非一此性能优先的操作(其实Scala集合采用了共享存储的优化,生成一个新集合并不会生成所有元素的复本,它将会和老的集合共享大元素。因为Scala中变量默认都是不可变的),推荐还是采用不可变集合。因为它更直观、线程安全,你可以确定你的变量不会在其它地方被不小心的更改。)

Class

Scala里也有class关键字,不过它定义类的方式与Java有些区别。Scala中,类默认是public的,且类属性和方法默认也是public的。Scala中,每个类都有一个“主构造函数”,主构造函数类似函数参数一样写在类名后的小括号中。因为Scala没有像Java那样的“构造函数”,所以属性变量都会在类被创建后初始化。所以当你需要在构造函数里初始化某些属性或资源时,写在类中的属性变量就相当于构造初始化了。

在Scala中定义类非常简单:

class Person(name: String, val age: Int) {
  override def toString(): String = s"姓名:$name, 年龄: $age"
}

默认,Scala主构造函数定义的属性是private的,可以显示指定:val或var来使其可见性为:public。

Scala中覆写一个方法必需添加:override关键字,这对于Java来说可以是一个修正。当标记了override关键字的方法在编译时,若编译器未能在父类中找到可覆写的方法时会报错。而在Java中,你只能通过@Override注解来实现类似功能,它的问题是它只是一个可选项,且编译器只提供警告。这样你还是很容易写出错误的“覆写”方法,你以后覆写了父类函数,但其实很有可能你是实现了一个新的方法,从而引入难以察觉的BUG。

实例化一个类的方式和Java一样,也是使用new关键字。

scala> val me = new Person("杨景", 30)
me: Person = 姓名:杨景, 年龄: 30scala> println(me)
姓名:杨景, 年龄: 30scala> me.name
<console>:20: error: value name is not a member of Person
       me.name
          ^

scala> me.ageres11: Int = 30

case class(样本类)

case class是Scala中学用的一个特性,像Kotlin这样的语言也学习并引入了类似特性(在Kotlin中叫做:data class)。case class具有如下特性:

不需要使用new关键词创建,直接使用类名即可

默认变量都是public final的,不可变的。当然也可以显示指定var、private等特性,但一般不推荐这样用

自动实现了:equals、hashcode、toString等函数

自动实现了:Serializable接口,默认是可序列化的

可应用到match case(模式匹配)中

自带一个copy方法,可以方便的根据某个case class实例来生成一个新的实例

……

这里给出一个case class的使用样例:

scala> trait Person
defined trait Person

scala> case class Man(name: String, age: Int) extends Person
defined class Man

scala> case class Woman(name: String, age: Int) extends Person
defined class Woman

scala> val man = Man("杨景", 30)
man: Man = Man(杨景,30)

scala> val woman = Woman("女人", 23)
woman: Woman = Woman(女人,23)

scala> val manNextYear = man.copy(age = 31)
manNextYear: Man = Man(杨景,31)

object

Scala有一种不同于Java的特殊类型,Singleton Objects。

object Blah {  def sum
(l: List[Int]): Int = l.sum
}

在Scala中,没有Java里的static静态变量和静态作用域的概念,取而代之的是:object。它除了可以实现Java里static的功能,它同时还是一个线程安全的单例类。

伴身对象

大多数的object都不是独立的,通常它都会与一个同名的class定义在一起。这样的object称为伴身对象。

class IntPair(val x: Int, val y: Int)

object IntPair {
  import math.Ordering
  implicit def ipord: Ordering[IntPair] =
    Ordering.by(ip => (ip.x, ip.y))
}

注意

伴身对象必需和它关联的类定义定义在同一个.scala文件。

伴身对象和它相关的类之间可以相互访问受保护的成员。在Java程序中,很多时候会把static成员设置成private的,在Scala中需要这样实现此特性:

class X {
  import X._
  def blah = foo
}
object X {
  private def foo = 42
}

函数

在Scala中,函数是一等公民。函数可以像类型一样被赋值给一个变量,也可以做为一个函数的参数被传入,甚至还可以做为函数的返回值返回。

从Java 8开始,Java也具备了部分函数式编程特性。其Lamdba函数允许将一个函数做值赋给变量、做为方法参数、做为函数返回值。

在Scala中,使用def关键ygnk来定义一个函数方法:

scala> def calc(n1: Int, n2: Int): (Int, Int) = {
     |   (n1 + n2, n1 * n2)
     | }
calc: (n1: Int, n2: Int)(Int, Int)

scala> val (add, sub) = calc(5, 1)
add: Int = 6
sub: Int = 5

这里定义了一个函数:calc,它有两个参数:n1和n2,其类型为:Int。cala函数的返回值类型是一个有两个元素的元组,在Scala中可以简写为:(Int, Int)。在Scala中,代码段的最后一句将做为函数返回值,所以这里不需要显示的写return关键字。

而val (add, sub) = calc(5, 1)一句,是Scala中的抽取功能。它直接把calc函数返回的一个Tuple2值赋给了add他sub两个变量。

函数可以赋给变量:

scala> val calcVar = calc _
calcVar: (Int, Int) => (Int, Int) = <function2>

scala> calcVar(2, 3)
res4: (Int, Int) = (5,6)

scala> val sum: (Int, Int) => Int = (x, y) => x + y
sum: (Int, Int) => Int = <function2>

scala> sum(5, 7)
res5: Int = 12

在Scala中,有两种定义函数的方式:

将一个现成的函数/方法赋值给一个变量,如:val calcVar = calc _。下划线在此处的含意是将函数赋给了变量,函数本身的参数将在变量被调用时再传入。

直接定义函数并同时赋给变量,如:val sum: (Int, Int) => Int = (x, y) => x + y,在冒号之后,等号之前部分:(Int, Int) => Int是函数签名,代表sum这个函数值接收两个Int类型参数并返回一个Int类型参数。等号之后部分是函数体,在函数函数时,x、y参数类型及返回值类型在此可以省略。

一个函数示例:自动资源管理

在我们的日常代码中,资源回收是一个很常见的操作。在Java 7之前,我们必需写很多的try { ... } finally { xxx.close() }这样的样版代码来手动回收资源。Java 7开始,提供了try with close这样的自动资源回收功能。Scala并不能使用Java 7新加的try with close资源自动回收功能,但Scala中有很方便的方式实现类似功能:

def using[T <: AutoCloseable, R](res: T)(func: T => R): R = {  try {
    func(res)
  } finally {    if (res != null)
      res.close()
  }
}val allLine = using(Files.newBufferedReader(Paths.get("/etc/hosts"))) { reader =>  @tailrec
  def readAll(buffer: StringBuilder, line: String): String = {    if (line == null) buffer.toString    else {
      buffer.append(line).append(&#39;\n&#39;)
      readAll(buffer, reader.readLine())
    }
  }

  readAll(new StringBuilder(), reader.readLine())
}

println(allLine)

using是我们定义的一个自动化资源管帮助函数,它接爱两个参数化类型参数,一个是实现了AutoCloseable接口的资源类,一个是形如:T => R的函数值。func是由用户定义的对res进行操作的函数代码体,它将被传给using函数并由using代执行。而res这个资源将在using执行完成返回前调用finally代码块执行.close方法来清理打开的资源。

这个:T 61acb5695cfa7e858594848db53194d3 x + y这个的函数字面量定义函数形式。所以,既然通过变量定义的函数可以放在其它函数代码体内,通过def定义的函数也一样可以放在其它代码体内,这和Javascript很像。

@tailrec注解的含义是这个函数是尾递归函数,编译器在编译时将对其优化成相应的while循环。若一个函数不是尾递归的,加上此注解在编译时将报错。

模式匹配(match case)

模式匹配是函数式编程里面很强大的一个特性。

之前已经见识过了模式匹配的简单使用方式,可以用它替代:if else、switch这样的分支判断。除了这些简单的功能,模式匹配还有一系列强大、易用的特性。

match 中的值、变量和类型

scala> for {
     |   x <- Seq(1, false, 2.7, "one", &#39;four, new java.util.Date(), new RuntimeException("运行时异常"))
     | } {
     |   val str = x match {
     |     case d: Double => s"double: $d"
     |     case false => "boolean false"
     |     case d: java.util.Date => s"java.util.Date: $d"
     |     case 1 => "int 1"
     |     case s: String => s"string: $s"
     |     case symbol: Symbol => s"symbol: $symbol"
     |     case unexpected => s"unexpected value: $unexpected"
     |   }
     |   println(str)
     | }
int 1
boolean false
double: 2.7
string: one
symbol: &#39;four
java.util.Date: Sun Jul 24 16:51:20 CST 2016
unexpected value: java.lang.RuntimeException: 运行时异常

上面小试牛刀校验变量类型的同时完成类型转换功能。在Java中,你肯定写过或见过如下的代码:

public void receive(message: Object) {    
if (message isInstanceOf String) {     
  String strMsg = (String) message;
        ....
    } else if (message isInstanceOf java.util.Date) {
        java.util.Date dateMsg = (java.util.Date) message;
        ....
    } ....
}

对于这样的代码,真是辣眼睛啊~~~。

序列的匹配

scala> val nonEmptySeq = Seq(1, 2, 3, 4, 5)

scala> val emptySeq = Seq.empty[Int]

scala> val emptyList = Nil

scala> val nonEmptyList = List(1, 2, 3, 4, 5)

scala> val nonEmptyVector = Vector(1, 2, 3, 4, 5)

scala> val emptyVector = Vector.empty[Int]

scala> val nonEmptyMap = Map("one" -> 1, "two" -> 2, "three" -> 3)

scala> val emptyMap = Map.empty[String, Int]

scala> def seqToString[T](seq: Seq[T]): String = seq match {
     |   case head +: tail => s"$head +: " + seqToString(tail)
     |   case Nil => "Nil"
     | }

scala> for (seq <- Seq(
     |   nonEmptySeq, emptySeq, nonEmptyList, emptyList,
     |   nonEmptyVector, emptyVector, nonEmptyMap.toSeq, emptyMap.toSeq)) {
     |   println(seqToString(seq))
     | }
1 +: 2 +: 3 +: 4 +: 5 +: Nil
Nil
1 +: 2 +: 3 +: 4 +: 5 +: Nil
Nil
1 +: 2 +: 3 +: 4 +: 5 +: Nil
Nil
(one,1) +: (two,2) +: (three,3) +: Nil
Nil

模式匹配能很方便的抽取序列的元素,seqToString使用了模式匹配以递归的方式来将序列转换成字符串。case head +: tail将序列抽取成“头部”和“非头部剩下”两部分,head将保存序列第一个元素,tail保存序列剩下部分。而case Nil将匹配一个空序列。

case class的匹配

scala> trait Person

scala> case class Man(name: String, age: Int) extends Person

scala> case class Woman(name: String, age: Int) extends Person

scala> case class Boy(name: String, age: Int) extends Person

scala> val father = Man("父亲", 33)

scala> val mather = Woman("母亲", 30)

scala> val son = Man("儿子", 7)

scala> val daughter = Woman("女儿", 3)

scala> for (person <- Seq[Person](father, mather, son, daughter)) {
     |   person match {
     |     case Man("父亲", age) => println(s"父亲今年${age}岁")
     |     case man: Man if man.age < 10 => println(s"man is $man")
     |     case Woman(name, 30) => println(s"${name}今年有30岁")
     |     case Woman(name, age) => println(s"${name}今年有${age}岁")
     |   }
     | }
父亲今年33岁
母亲今年有30岁
man is Man(儿子,7)
女儿今年有3岁

在模式匹配中对case class进行解构操作,可以直接提取出感兴趣的字段并赋给变量。同时,模式匹配中还可以使用guard语句,给匹配判断添加一个if表达式做条件判断。

并发

Scala是对多核和并发编程的支付做得非常好,它的Future类型提供了执行异步操作的高级封装。

Future对象完成构建工作以后,控制权便会立刻返还给调用者,这时结果还不可以立刻可用。Future实例是一个句柄,它指向最终可用的结果值。不论操作成功与否,在future操作执行完成前,代码都可以继续执行而不被阻塞。Scala提供了多种方法用于处理future。

scala> :paste
// Entering paste mode (ctrl-D to finish)

import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global

val futures = (0 until 10).map { i =>
  Future {
    val s = i.toString
    print(s)
    s
  }
}

val future = Future.reduce(futures)((x, y) => x + y)

val result = Await.result(future, Duration.Inf)

// Exiting paste mode, now interpreting.

0132564789

scala> val result = Await.result(future, Duration.Inf)
result: String = 0123456789

上面代码创建了10个Future对象,Future.apply方法有两个参数列表。第一个参数列表包含一个需要并发执行的命名方法体(by-name body);而第二个参数列表包含了隐式的ExecutionContext对象,可以简单的把它看作一个线程池对象,它决定了这个任务将在哪个异步(线程)执行器中执行。futures对象的类型为IndexedSeq[Future[String]]。本示例中使用Future.reduce把一个futures的IndexedSeq[Future[String]]类型压缩成单独的Future[String]类型对象。Await.result用来阻塞代码并获取结果,输入的Duration.Inf用于设置超时时间,这里是无限制。

这里可以看到,在Future代码内部的println语句打印输出是无序的,但最终获取的result结果却是有序的。这是因为虽然每个Future都是在线程中无序执行,但Future.reduce方法将按传入的序列顺序合并结果。

除了使用Await.result阻塞代码获取结果,我们还可以使用事件回调的方式异步获取结果。Future对象提供了几个方法通过回调将执行的结果返还给调用者,常用的有:

onComplete: PartialFunction[Try[T], Unit]:当任务执行完成后调用,无论成功还是失败

onSuccess: PartialFunction[T, Unit]:当任务成功执行完成后调用

onFailure: PartialFunction[Throwable, Unit]:当任务执行失败(异常)时调用

import scala.concurrent.Future
import scala.util.{Failure, Success}
import scala.concurrent.ExecutionContext.Implicits.global

val futures = (1 to 2) map {
  case 1 => Future.successful("1是奇数")
  case 2 => Future.failed(new RuntimeException("2不是奇数"))
}

futures.foreach(_.onComplete {
  case Success(i) => println(i)
  case Failure(t) => println(t)
})

Thread.sleep(2000)

futures.onComplete方法是一个偏函数,它的参数是:Try[String]。Try有两个子类,成功是返回Success[String],失败时返回Failure[Throwable],可以通过模式匹配的方式获取这个结果。

总结

本篇文章简单的介绍了Scala的语言特性,本文并不只限于Java程序员,任何有编程经验的程序员都可以看。现在你应该对Scala有了一个基础的认识,并可以写一些简单的代码了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn