Lambda Collections, Classes, Objects, Apply Flashcards

(23 cards)

1
Q

T or F. def foobar = expression cannot be re-assigned.

A

True

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

What is the advantage of lambdas?

A

Unlike regular functions then act like variables:
- we can reassign them

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

Define HoF.

A

A higher-order function is a function that accepts as argument, or returns another function.

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

Why not use for each loop?

A

It returns unit so in order to do anything its lambda argument must be impure (have side effects). This is not good practice in FP.

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

What are the disadvantages for the traditional loop approach?

A
  • Creates corners for bugs to hide e.g. out-by-one
  • Violates “tell don’t ask”
  • Cannot be easily parallelised
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define map.

A

Applies a given lambda to each element of a collection to return a transformed collection of the same type. Elements in transformed collection don’t have to be same type.

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

Define filter.

A

Selects elements of original collection for which given predicate is True.

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

T or F. All HoFs create a new collection. They do not alter the original.

A

True

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

Define reduce.

A

Combines the elements of collection (of type T) into a single value of type T.

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

Define fold.

A

Works like reduce. But has initial seed value and can reduce to different type.

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

Define flatten.

A

Flattens nested collections into a single collection. All elements must be collections.

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

What does the term first-class values mean?

A

Means it (e.g. function, object) can be treated like any other value. Passed as argument, assigned to variables, stored in collections and returned from functions.

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

class Person:
private var age = 0

def compare(other: Person) =
age > other.age

Is the access to other.age legal? Why?

A

Yes. Privacy is class-based.

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

Define method signature.

A

A method’s name and the number and type of its arguments.

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

What is method overloading? Who decides which method to use?

A

Having multiple methods with the same name within a class but with different arguments.

Compiler.

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

What is an object? What is its type?

A

A single instance that can be accessed through the objects name. They are usually globally accessible.

Singleton.

17
Q

What is a Singleton?

A

Structure with a single instance shared among entire program.

18
Q

What is companion object and companion class?

A

Companion object has the same name as companion class. Each instance of companion class shares a single instance of companion object. Works like static in Java. Also, they can access each others private fields.

19
Q

T or F. A function is just an object with an apply method.

20
Q

What is a factory method?

A

Constructor proxy in the companion object made by defining apply method within the object.

21
Q

What is the difference between the effect of new Array(100) and Array(100)?

A

The latter invokes the apply method.

22
Q

When we create a normal class, what is generated also?

A

A companion object with a default apply method.

23
Q

val numbers = Array(1,2,3)
println(numbers(1))

What is happening here in terms of function calls?

A

1st line calls apply method in Array companion object.

2nd line calls apply method in Array class.