Please enable JavaScript.
Coggle requires JavaScript to display documents.
Scala for the Impatient-LiveLessons (Medium (Lesson 4: OOP1 Classes and…
Scala for the Impatient-LiveLessons
Basic
Lesson 2: Control Structures and Functions p10
2.4 named, default and variable number args p14
def sum(args: Int* ) = {}
sum(1 to 10 :_* )
needed in recursive call
2.3 Define functions p13
if you omit the =, the function doesn't return a value
2.2 for each and for comprehension p12
for(i<-1 to n)
for(i <- 1 to 3; j<-1to 3)
for(i <- 1 to 3; j<-1to 3 if i!=j)
2.1 Conditional expression and block p11
:star:if expression has a value
if (x>0) "positive" else -1 //Type is Any
Value of block is the value of the last expression
If the last express is an assignment the block value is ()
:pencil2: Lab Exe: Vowels p15
for (ch <- s if "aeiuo".contains(ch)) yield ch
Lesson 1: Scala Basics p01
1.1 Know the story behind Scala p02
1.2 Use Scala Worksheet p03
1.3 Work with values, variables and fundamental data types p04
Everything is an object
1.to(10)
1.4 Invoke functions and methods p05
BigInt.probablePrime() //method
apply method
"Hello"(4)
Method without parameters don't use ()
1.5 Work with Scaladoc p06
Lab Exe 1: The Scala Worksheet p07-09
Medium
Lesson 4: OOP1 Classes and Objects p25
:red_flag:4.2 Appreciate the benefit of immutability p27
safe, important in concurrent programming
4.3 Understand object construction and class parameters p28
4.1 class, instance variables and methods p26
class Point(val x: Double, val y: Double){}
val p = new Point(3,4) // immutable
default is public
4.4 Understand the uniform access principle p29
4.5 Use the operator notation for binary methods p30
p.*(2) equals p*2
: means right-associative
1::(2::(3::Nil))
:red_flag:4.6 Understand objects and the role of companion objects p31
object Point{
def apply(x, y) = new Point(x, y)
}
val p = Point(3,4)
Lab Exe p32-34
:star:Lesson 5: OOP2 Packages, Inheritance and Traits p35
5.2 Work with import statements p37
import java.util.{HashMap=>
,
} //hide class
5.3 Declare subclasses and traits p38
isInstancdOf[myClass]
asInstanceOf[myClass]
5.1 Package and package nesting p36
:red_flag::5.4 Understand mixins and layered traits p39
Lab Exe p40-42
Lesson 3: Arrays, Maps and Tuples p16
3.2 Transform arrays p18
for() yeild
3.3 Use common array algorithms p19
sum
max
sorted
reverse
mkString()
3.1 Arrays and array buffers p17
val nums = new Array [Int] (10)
var strArray = Array("Hello", "World")
val b = new ArrayBuffer[Int]
b +=1
var a = b.toArray
var b = a.toBuffer
3.4 Work with maps and tuples p20
var s = Map("Bob"->10)
var bobScore = s("Bob")
var a = s.getOrElse("Fred", 0)
for((k, v)<- s)
var tup = (1, "Fred", 3.14) //different type
tup._2
tup._1 // start from 1
val ( _, second, third) = t // ignore first one
:pencil2:Lab Exe p21- 24
count()
groupBy()
partition()
zip()
Adbanced
:star:Lesson 7: Case Classes and Pattern Matching p52
7.2 match statement and exception catching p54
:red_flag:7.3 Extractors
extract contents form tuples
7.1 Match statement p53
:red_flag:7.4 case Classes p56
7.5 Option type p57
7.6 Case classes vs Polymorphism
:star:Lesson 6: Functional Programming p43
:red_flag:6.2 Higher order functions p45
Functions have function parameters
def valueAtOneQuarter(f:(Double)=>Double)=f(0.25)
def mulBy(factor: Double)=(x:Double)=>factor*x
val quintuple = mulBy(5)
quintuple(20) //100
If the parameter variable occurs just once, can replace with _
:red_flag:6.3 Map, filter, reduce methods p46
(1 to 9).filter(_ %2==0)
(1 to 9).reductLeft(
*
) // (...((1*2)*3)*...9)
(1 to 9).map(0.1*_)
6.1 Concept of functional programming p44
Array(3.14, 1.42).map((x:Double)=>3*x)
:red_flag:Closures and curring p47
Curring = turing a function that takes 2 args
into a function takes 1 arg
That function returns a function that consumes the second arg
def mul(x:Int, y:Int)=x*y
def mulOne(x:int) = (y:Int)=>x*y
mulOne(3)(4)
6.5 Develop control abstractions p48
def runInThread(block:()=>Unit){
new Thread{
override def run(){block()}
}.start()
}
Lab Ext p49-p51