Scala 基础知识学习笔记
Scala 同时拥有面向对象和函数式编程的特性,是一个 high-level 编程语言。并且它能运行在 JVM、JavaScript 和 Native LLVM 上。因为其高性能,所以被广泛的运用于大数据处理的应用程序中。
目前 Scala 有两个版本:2.13.8 和 3.1.8
因为 Scala 是能够运行在 JVM 上的,所以可以无缝使用 Java 的各种 API。
Scala Ecosystem¶
Scala has a vibrant ecosystem, with libraries and frameworks for every need. The “Awesome Scala” list provides a list of hundreds of open source projects that are available to Scala developers, and the Scaladex provides a searchable index of Scala libraries. Some of the more notable libraries are listed below.
Web development¶
- The Play Framework followed the Ruby on Rails model to become a lightweight, stateless, developer-friendly, web-friendly architecture for highly-scalable applications
- Scalatra is a tiny, high-performance, async web framework, inspired by Sinatra
- Finatra is Scala services built on TwitterServer and Finagle
- Scala.js is a strongly-typed replacement for JavaScript that provides a safer way to build robust front-end web applications
- ScalaJs-React lifts Facebook’s React library into Scala.js, and endeavours to make it as type-safe and Scala-friendly as possible
- Lagom is a microservices framework that helps you decompose your legacy monolith and build, test, and deploy entire systems of Reactive microservices
HTTP(S) Libraries¶
- Akka-http
- Finch
- Http4s
- Sttp
JSON Libraries¶
- Argonaut
- Circe
- Json4s
- Play-JSON
Serialization¶
- ScalaPB
Science and data analysis¶
- Algebird
- Spire
- Squants
Big Data¶
- Apache Spark
- Apache Flink
AI, Machine Learning¶
- BigDL (Distributed Deep Learning Framework for Apache Spark) for Apache Spark
- TensorFlow Scala
Functional Programming & Functional Reactive Programming¶
FP: * Cats * Zio
Functional reactive programming (FRP): * fs2 * monix
Build Tools¶
- sbt
- Gradle
- Mill
Hello World¶
In scala, all types inherits from top-level class Any
, whose immediate children is AnyVal
and AnyRef
.
Look following code block, def
is a keyword to declare a method, the method hello
is declared to be a "main" method with @main
annotation.
Unit
1 2 3 4 |
|
Compile and Run:
1 2 3 4 5 |
|
Variables and Types¶
Variables¶
There are two types of variables:
- val: Creates a immutable variable - like
final
in Java. Good practice: always create a variable withval
, unless you have reason to use a mutable variable. - var: Creates a mutable variable. Only used for a variable will change over time.
Example:
1 2 3 4 5 |
|
If you try to assign a new value to immutable variable, there will be a compile error.
Declaring variable with types¶
Data Type | Possible Values |
---|---|
Boolean | true or false |
Byte | 8-bit signed two’s complement integer (-2^7 to 2^7-1, inclusive): -128 to 127 |
Short | 16-bit signed two’s complement integer (-2^15 to 2^15-1, inclusive): -32,768 to 32,767 |
Int | 32-bit two’s complement integer (-2^31 to 2^31-1, inclusive): -2,147,483,648 to 2,147,483,647 |
Long | 64-bit two’s complement integer (-2^63 to 2^63-1, inclusive): (-2^63 to 2^63-1, inclusive) |
Float | 32-bit IEEE 754 single-precision float: 1.40129846432481707e-45 to 3.40282346638528860e+38 |
Double | 64-bit IEEE 754 double-precision float: 4.94065645841246544e-324 to 1.79769313486231570e+308 |
Char | 16-bit unsigned Unicode character (0 to 2^16-1, inclusive): 0 to 65,535 |
String | a sequence of Char |
1 2 3 4 |
|
Built-in data types:
- Byte
- Int
- Long
- Short
- Double
- Float
Int
and Double
are the default numeric types, you should create them without explicitly declaring the data type:
1 2 |
|
You can also append L
, D
, F
to numbers to specify their type:
1 2 3 |
|
Big numbers:
- BigInt
- BigDecimal
1 2 |
|
String
and Char
:
1 2 |
|
Strings¶
You can use Strings variables in a string formatter:
1 2 3 4 |
|
Multiline strings¶
1 2 |
|
Type Casting¶
1 2 3 4 5 |
|
Nothing and Null¶
Nothing
is a subtype of all types, bottom type. No value that has the type Nothing
. Common use is to signal non-termination, eg. a thrown exception, program exit...
Null
is also a subtype of all reference types (i.e. any subtypeof AnyRef
).
Control Structures¶
if/else¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Ternary Expression:
1 |
|
for loops and expression¶
for loops¶
1 2 3 4 5 6 |
|
for
with guards:
1 2 3 4 5 |
|
for expression¶
Use yield
keyword instead of do
, you can create for
expressions which are used to calculate and yield results.
1 2 3 4 |
|
You can also write the code like this:
1 2 3 4 |
|
match expressions¶
match
keyword is like Java switch
statement:
1 2 3 4 5 6 7 8 9 10 |
|
try/catch/finally¶
1 2 3 4 5 6 7 |
|
while loops¶
1 |
|
OOP Domain Modeling¶
Traits¶
traits
is like interface
in Java, but it can also contain abstract and concrete methods and fields, and they can have parameters, just like classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
class
can have multiple extends trait
, also we can use override
to override a method from a extended trait.
Classes¶
class
in Scala is pretty similar to Java classes.
1 2 3 4 5 6 |
|
Enums¶
For example, a pizza has three main attributes:
1 2 3 4 5 6 |
|
How to use them:
1 2 3 4 5 6 7 8 9 |
|
Case classes¶
case
class is a kind of class (has all functionality of a class
) which the fields in it are immutable. Some additional features:
- Case class constructor parameters are public
val
fields by default, so the fields are immutable and accessor methods of each parameter are generated. unapply
method is generated incase
class (Use inmatch
expressions).- A
copy
method is generated in the class. equals
andhashcode
methods are generated to implement structural equality.- A default
toString
method is generated.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Methods¶
1 2 3 4 5 6 7 |
|
Named Parameter and Default Parameter Value:
1 2 3 4 5 6 7 8 |
|
Extension Methods¶
Extension Methods let you add new methods to closed classes. For instance, if you want to add two methods named hello
and aloha
to String
class, decalre them as extension methods:
1 2 3 4 5 |
|
First-Class Functions¶
Scala has most features you'd expect in a functional programming language:
- Lambda
- Higher-order functions (HOFs)
- Immutable collections in the standard library
Lambda¶
map
method for List
class:
1 2 3 |
|
Also has method reference:
1 2 3 4 5 6 |
|
Immutable collections¶
1 2 3 4 5 |
|
Create methods take function as parameter¶
1 2 3 4 5 6 |
|
Object¶
object
in Scala creates a singleton object, which means, object
defines a class that has exactly one instance.
Common Usage:
- Used to create a collection of utility methods.
1 2 3 4 5 |
|
- Companion object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
object Circle
is companion object, and class Circle
is companion class. - Implement traits to create modules.
1 2 3 4 5 6 7 8 9 10 11 12 |
|