Scala Flashcards

1
Q

var x = 5

A

variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

val x = 5

A

constant

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

x = 5

A

constant, bad form!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

var x: Double = 5

A

explicit type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

def f(x: Int) = { x*x }

A

define function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

def f(x: Int) { x*x }

A

error: without “=” it’s a Unix-returning procedure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

def f(x: Any) = println(x)

A

Any is the root of the Scala class hierarchy. This function can take anything for parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

def f(x) = println(x)

A

Syntax error: must have a type for every arg.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

type R = Double

A

type alias

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
call by?
def f(x: R)
A

call-by-value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
call by?
def f(x: => R)
A

call-by-name (lazy parameters)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

(x:R) => x*x

A

anonymous function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

(1 to 5).map(*2)
or
(1 to 5).reduceLeft(
+_)

A

anonymous function - underscore is positionally matched argument

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

(1 to 5).map( x => x*x )

A

anonymous function: to use an argument twice, you have to name the variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

(1 to 5).map(2)
or
(1 to 5).map(
2)

A

anonymous function bound infix method. Use 2*_ instead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

(1 to 5).map {val x=_*2; println(x); x}

A

anonymous function - block style returns last expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

(1 to 5) filter {%2 == 2} map {*2}

A

anonymous function - pipeline style.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
def compose(g:R=>R, h:R=>R) = 
 (x:R) => g(h(x))
val f = compose({_*2),{_-1})
A

anonymous function - to pass in multiple blocks, need outer parenthesis

19
Q

val zscore =
(mean:R, sd:R) =>
(x:R) =>
(x-mean)/sd

20
Q

def zscore(mean:R, sd:R) =
(x:R) =>
(x-mean)/sd

21
Q
def zscore(mean:R, sd:R)(x:R)
   = (x-mean)/sd
A

currying (syntax sugar)

22
Q

val normer = zscore(7, 0.4)_

A

need trailing underscore to get the partial, only for the sugar version

23
Q

def mapmakeT(seq: List[T]) = seq.map(g)

A

generic type

24
Q

5.+(3); 5 + 3

1 to 5) map (_*2

A

infix sugar

25
def sum(args: Int*) = args.reduceLeft(_+_)
varargs
26
import scala.collection._
wildcard import
27
import scala.collection.Vector | import scala.collection.{Vector,Sequence}
selective import
28
import scala.collection.{Vector => Vec28}
renaming import
29
(1,2,3)
tuple literal (Tuple3)
30
var (x,y,z) = (1,2,3)
destructuring bind: tuple unpacking via pattern matching
31
var xs = List(1,2,3)
list (immutable)
32
``` var xs = List(1,2,3) xs(2) ```
parenthesis indexing
33
1 :: List(2,3)
cons
34
Byte
8 bit signed value
35
Short
16 bit signed value
36
Int
32 bit signed value
37
Long
64 bit signed value
38
Float
32 bit floating point
39
Double
64 bit floating point
40
Unit
Corresponds to no value
41
Null
null or empty reference
42
Nothing
The subtype of every other type, includes no values
43
Any
Supertype of any type. Any object is of type Any.
44
AnyRef
The supertype of any reference type