Scala Flashcards
object keyword
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
When to create a companion object?
To isolate the static methods of a class or create a factory method
Unit
Equivalent of void. Subtype of anyval.
Concurrency vs Parralism
Concurreny - when several computations execute sequentially during overlapping time periods. Concurrency abstractions - threads, semaphore, synchronization. Future.
Parallelism - Processes are executed simultaneously.
Monad
- 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
Lambda Calculus
- Literal translation is “Functional Analysis”
- System of treating problems by agreed on symbols as a method of calculation
Var vs Val
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.
Closure
The return value of the function depends on the value of one or more variables declared outside the function
Future
- 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
Future vs Promise
Two different sides of a pipe. On the promise side, data is pushed in. On the Future side, data is pulled out.
Traits
- 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
Currying
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
Case classes
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.
Implicit parameter
optional parameter with default values
Tail recursion
The scala compiler optimizes recursive functions so that they don’t allocate new stack space