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

A

currying

20
Q

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

A

currying

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
Q

def sum(args: Int*) = args.reduceLeft(+)

A

varargs

26
Q

import scala.collection._

A

wildcard import

27
Q

import scala.collection.Vector

import scala.collection.{Vector,Sequence}

A

selective import

28
Q

import scala.collection.{Vector => Vec28}

A

renaming import

29
Q

(1,2,3)

A

tuple literal (Tuple3)

30
Q

var (x,y,z) = (1,2,3)

A

destructuring bind: tuple unpacking via pattern matching

31
Q

var xs = List(1,2,3)

A

list (immutable)

32
Q
var xs = List(1,2,3)
xs(2)
A

parenthesis indexing

33
Q

1 :: List(2,3)

A

cons

34
Q

Byte

A

8 bit signed value

35
Q

Short

A

16 bit signed value

36
Q

Int

A

32 bit signed value

37
Q

Long

A

64 bit signed value

38
Q

Float

A

32 bit floating point

39
Q

Double

A

64 bit floating point

40
Q

Unit

A

Corresponds to no value

41
Q

Null

A

null or empty reference

42
Q

Nothing

A

The subtype of every other type, includes no values

43
Q

Any

A

Supertype of any type. Any object is of type Any.

44
Q

AnyRef

A

The supertype of any reference type