Scala Flashcards

1
Q

object keyword

A

Combines a few concepts from Java.

It’s like a singleton class in Java in the sense that it’s a class that has exactly one instance. Unlike Java, there’s no need to make the methods static. (Scala has no static keyword.)

It’s like an anonymous class in the sense that you declare and instantiate the class at the same time.

Objects are created lazily (like a val variable).

An object and a class with the same name are called companions and can access each other’s private methods.

One of the most common use cases for objects is to create utility methods or factory methods to return an instance of the class.

https://docs.scala-lang.org/tour/singleton-objects.html

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

When to create a companion object?

A

To isolate the static methods of a class or create a factory method

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

Unit

A

Equivalent of void. Subtype of anyval.

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

Concurrency vs Parralism

A

Concurreny - when several computations execute sequentially during overlapping time periods. Concurrency abstractions - threads, semaphore, synchronization. Future.
Parallelism - Processes are executed simultaneously.

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

Monad

A
  • Wraps another object in scala
  • Structures that represent sequential computations
  • The output of the computation is an input to other calculations
  • Provides identity through unit
  • Bind via flatMap
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Lambda Calculus

A
  • Literal translation is “Functional Analysis”

- System of treating problems by agreed on symbols as a method of calculation

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

Var vs Val

A

Var - ordinary java variable. Mutable declaration.
Val - once variable is declared with val the reference cannot be changed to point to another reference. Immutable declaration. Similar to final in Java.

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

Closure

A

The return value of the function depends on the value of one or more variables declared outside the function

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

Future

A
  • Represents future value of a task once it has completed. Does not represent the asynchronous computation that is spawned when a future is created.
  • Allows async programming through chaining of operations
  • Supports callback functions like onComplete, OnFailure, and onSuccess
  • Java’s future class does not provide promises or callbacks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Future vs Promise

A

Two different sides of a pipe. On the promise side, data is pushed in. On the Future side, data is pulled out.

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

Traits

A
  • Enable multiple inheritance. A trait can only extend one class but a class can have multiple traits.
  • Traits cannot be instantiated.
  • Often used for dependency injection.
  • Useful for declaring generic types with abstract methods
  • Use the extends keyword to extend a trait. Then implement any abstract methods using the override keyword.

https://docs.scala-lang.org/tour/traits.html

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

Currying

A

Technique of transforming a function with multiple arguments into a chain of functions each with a single argument.
Makes it easier to defer execution and reuse functions

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

Case classes

A

Good for modeling immutable data and pattern matching (substitute for switch and long if/else blocks in Java)

  • No need to create a constructor or declare parameters explicitly. Both are done by default.
  • Serializable by default
  • For equality, instances are compared by structure (are the values of the fields identical?), not by reference equality.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Implicit parameter

A

optional parameter with default values

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

Tail recursion

A

The scala compiler optimizes recursive functions so that they don’t allocate new stack space

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

Streams

A

Like a list but with lazy evaluation

17
Q

Implicit functions

A
  • Convert one type to another
  • Like casting but called automatically by the compiler
  • Bad practice, especially when implicits are visible outside the class
  • Used to add behavior to a type via a practice called pimping
18
Q

Sealed traits and classes

A

Subtypes must be declared in the same file. When all subtypes are known, you don’t need a case _ => catch all case.

19
Q

Monoid

A
  • Monoids combine n things into 1 thing and support the n=0 case.
  • A monoid is any type that has an operation taking two arguments of that type and returning a value of that type.
  • The operation must be associative.
  • The monoid must support 0. In other words, an operation where combining one thing and zero return the first thing unchanged.
20
Q

Apply

A

Factory method that allows you to skip new

21
Q

Partial Function

A
  • A regular function must return a value for the entire range of valid inputs
  • A partial function does not have to handle all possible inputs that could be passed to it.
  • This is especially useful for traits with many concrete implementations (case classes). You don’t have to address every case class in the partial function.

The collect() function in Scala collections is ax example of a partial function.

It accepts a partial function of whatever is in the collection, and returns a list of whatever is returned. The result of which is you can compress a .filter().map() into something more dignified and elegant.

val maybeNumbers = Seq(Some(1), Some(34), None, Some(-87), None)

val meh: Seq[Int] = maybeNumbers.filter(.isDefined).map(.get)
val elegant: Seq[Int] = maybeNumbers.collect { case Some(i) => i }