Case Classes, Options, Lambdas Again Flashcards

(19 cards)

1
Q

4 Advantages of immutable over mutable. Give one disadvantage as well and when it would be mitigated.

A

Advantages:
1. Easier to read, understand and update
2. Can share freely with no risk of state change
3. Prevents race conditions (Thread safe)
4. Safe hash table keys

Disadvantage: A large object structure may need to be fully copied to a new slightly different structure in immutability.

Cases classes provided a built in copy functionality to mitigate this. This is called structural sharing.

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

What are the special features of case classes?

A
  1. All class parameters are always val. Also public by default.
  2. Autogenerated methods
  3. Case classes do not have identity
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain case classes do not have identity.

A

Two instances of the same case class with the same values are considered equal. This is not the case with normal classes.

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

What is copy used for? Syntax for copy using Dog.

A

Effectively is how we mutate a case class.

case class Dog(name: String, breed: String)
val p = Dog(“Fido”, “Jack Russell”)

val q = p.copy(name = “Rudolph”)

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

T or F. We usually implement functions outside the case class.

A

True

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

c match
case _ if c.SomePredicate => …
What does this code mean?

A

Matches on anything for which the predicate is true

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

Syntax for case statement matching on Phone type when we don’t care about parameters or need to use the instance later.

A

case _: Phone

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

If we leave out a case, what will happen? What if we make the underlying trait sealed?

A

Compile fine, run time error if we match something and can’t find a case for it.

Compiler warning that not all cases are handled.

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

T or F. Case classes should never inherit from a concrete normal class and they should always be concrete.

A

True

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

What are the pros and cons of OO approach?

A

P: 1. More compact
2. More cohesive encapsulated classes.
3. Adding new class easy.

C: 1. Adding new functions hard each class must be changed

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

What are the pros and cons of the case class approach?

A

P: 1. Adding new functions easy
2. One new function can handle any class

C: 1. Adding class hard each function must be changed
2. Matching exposes internal representation of matched object making code maintenance harder.

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

Explain Option class.

A

Option[A] is an abstract class that represents either Some[A] (a useful value of type A) or None (no value).

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

T or F. None is a Singleton Object

A

True, one None object for all uses.

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

What does s.toIntOption do? What happens if we map this across a list but instead of map use flatMap?

A

toIntOption will convert a string (s) to a Some[Int] if it can or a None if conversion fails.

Flatten extracts the element from Some and ignores the None(s).

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

T or F. Map followed by flatten is the same as flatMap.

A

True.

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

Define Null Object Pattern.

A

Null Object of type T has same interface as a real object of type T, but does nothing. Although usually it is an object instead of a class.

17
Q

Code to implement Null Object Pattern.

A

object SomeNullObject extends SomeObject:
def someFunction = ()

Ensure all SomeObjects are defaulted to be SomeNullObjects.

18
Q

What is the limitation of Options and NullPet methods within lists?

A

Must define new length (and lots of other) function(s) as will include them in calculation.

19
Q

Define closure. (And the terms in the definition)

A

A closure is a first-class function with free variables that are bound in the lexical environment.

first-class: Can be treated as variable, parameter etc.
free variable: Not defined within the function
lexical environment: Code enclosing the function where it is created.