This is my personal notes for Functional Programming Principles in Scala at Coursera, which is considered one of the most popular introduction course to Scala.
Basics
Data Structure
Arrays
Arrays preserve order, can contain duplicates, and are mutable.
Lists
Lists preserve order, can contain duplicates, and are immutable.
Sets
Sets do not preserve order and have no duplicates
Tuple
A tuple groups together simple logical collections of items without using a class.
Unlike case classes, they don’t have named accessors, instead they have accessors that are named by their position and is 1-based rather than 0-based.
|
|
Tuple has some special sauce for simply making Tuples of 2 values: ->
Maps
It can hold basic datatypes.
Class + Constructor
|
|
Inheritance + Overloading Methods
|
|
Abstract Classes
Abstract class: a class that defines some methods but does not implement them. Instead, subclasses that extend the abstract class define these methods. You can’t create an instance of an abstract class.
Traits
Traits are collections of fields and behaviors that you can extend or mixin to your classes.
Option
Option is a container that may or may not hold something.
Option itself is generic and has two subclasses: Some[T] or None
Let’s look at an example of how Option is used:
Map.get uses Option for its return type. Option tells you that the method might not return what you’re asking for
Apply Methods
Objects
Objects are used to hold single instances of a class. Often used for factories.
Pattern Matching
|
|
Case Classes
|
|
Exceptions
|
|
Collections
Week 1 - Fundaments
substitution model - reduce an expression to a value (lambda calculus)
evaluate the the function from left to right, replace the parameter from right to left.
Lists
Lists preserve order, can contain duplicates, and are immutable.
- Call-by-value (more efficient)
- evaluate function argument only once
- Call-by-name (more easy to terminate)
- function argument is not evaluated if the corresponding parameter is unused in the evaluation of the function body
abstract expression introduces a name for an expression by which it can be then referred to.
- linear recursion
- tail recursion
to avoid too-deep recursion chain
Week 2
Anonymous function
An anonymous function (x_1:T_1, …, x_n:T_n) => E can always be expressed using def as follows
|
|
Function types
The type A => B is the type of a function that takes an argument of type A and returns a result of B.
e.g. Int => Int is the type of functions that map integers to integers
Curry
By repeating the process n times
Week 3
Abstract Classes
|
|
Super class to class C (all objects and classes) is base class.
How classes are organized
Class and objects are organized in packages.
Import packages:
- Name Import
import week3.Rational
;import week3.{Rational, Hello}
- Wildcard Import
import week3._
Some entities are automatically imported in any Scala program.
- All members of package scala
- All members of package java.lang
- All members of the singleton object scala.Predef.
e.g.Int, Boolean, Object, require, assert
Week5
Definition of Merge
|
|
better use of pattern matching
|
|
Rules for Implicit Parameters
Say, a function takes an implicit parameter of type T.
The compiler will search an implicit definition that
- is marked
implicit
- has a type compatible with
T
- is visible at the point of the function call, or is defined with a companion object associated with T
Natural Induction
n -> n + 1
Structure Induction
Some more sequence operations
xs exists p
:true
if there is an element x of xs such that p(x) holds, false otherwisexs forall p
:true
if p(x) holds for all elements x of xs, false otherwisexs zip ys
: a sequence of pairs drawn from corresponding elements of sequences xs and ysxs.unzip
: Splits a sequence of pairs xs into two sequences consisting of the first, respectively second halves of all pairsxs.flapMap f
: applies collection-valued function f to all elements of xs and concatenates the resultsxs.sum
the sum of all elements of this numeric collectionxs.product
: the product of all elements of this numeric collectionxs.max
: the maximum of all elements of this collection (an Ordering must exist)xs.min
: the minimum of all elements of this collectionReference
https://twitter.github.io/scala_school/collections.html
http://blog.tmorris.net/posts/scalaoption-cheat-sheet/