Ch3 covers functional data strucure: what structures, how to define and how to use them. Contents include functional data types, pattern matching and pure function generalization.
1 Functional Data Structure
A pure function must not change data in place or perform other side effects. Functional data structure are by definition immutable. An empty list is immutable, so is the result of two lists combined.
|
|
The List data type is polymorphic in the type of its elements. The two implementations are called data constructor of List: one empty Nil, one nonempty Cons (short for construct).
The apply function in the companion object is a variadic function that accepts zero or more argument of type A. Insdie the function, the argument has a type of Seq[A] . Seq has methods such as head, tail etc. The syntax _* type is used to pass a Seq to a variadic method.
2 Pattern Maching
A patern may match
- literals
- variables (an identifier starting with a lowercase letter or undercore)
- data constructors
Pattern matching may be nested arbitrarily. A pattern matches the target if there exists an
3 Data Sharing
Since functional data structure is immutable, data can be shared for different operations.
Using parameter list helps the type inference for higher-order functions.
4 Recursion Over Lists and Generalization
The similarity between contractors and foldRight:
|
|