Collections are of great significance in programming. Our decision on how to store a collection of elements determines the efficiency of our programs. Scala includes an elegant and powerful collection library, which we will dive into right now!

Mutable & Immutable

  • Mutable collections: can be changed after created
  • Immutable collections: cannot be changed after created

Trait Traversable

def foreach[U](f: Elem => U)
  • The invocation of f is done for its side effect only; any function result of f is discarded by foreach.

Trait Iterable

def foreach[U](f: Elem…

Information hiding

  1. Private constructors
class Queue[T] private (
private val leading: List[T],
private val trailing: List[T]
)

Plus auxiliary constructor:

def this() = this(Nil, Nil)
def this(elems: T*) = this(elems.toList, Nil)

Or plus factory method:

object Queue {
def apply[T](xs: T*) = new Queue[T](xs.toList, Nil)
}

2. Alternative: private class

  • Only export a trait that reveals the public interface of the class.
trait Queue[T] {
def head: T
def tail: Queue[T]
def enqueue(x: T): Queue[T]
}
object Queue {
def apply[T](xs: T*):Queue[T] = new QueueImpl[T](xs.toList, Nil)
private class QueueImpl[T] (…

Traits in Scala are like Java interfaces with concrete methods.

Traits can also declare fields and maintain state.

A class can mix in multiple traits.

Usage 1: Widen thin interfaces to rich ones

E.g., Ordered trait

Define a single compare method; the Ordered trait then defines <. >, ≤, ≥ for you in terms of this compare method.

Usage 2: Define stackable modifications

abstract class IntQueue {
def get(): Int
def put(x: Int): Unit
}
// A concrete class BasicIntQueue
class BasicIntQueue extends IntQueue {
// Implementation of…

The majority of this article comes from Head First Design Patterns and Oxford Imperative Programming 3 lectures.

Identity the aspects of the application that vary and separate them from what stays the same.

  • The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
  • The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

Picture it as publishers (formally, subject)+ subscribers (formally, observer)!


Three equality comparisons: ==, equals, eq

  • == is exactly the same as eq.
// Definition of == in class Any:
final def == (that: Any): Boolean =
if (null eq this) {null eq that} else {this equals that}
  • eq is for reference equality, equals is for reference equality by default, but can be customized to more natural notions of equality.

Contracts between equals and hashCode

  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • equals should be an equivalence relation: reflexive, transitive, symmetric, consistent.

Common Pitfalls of writing equals method

class Point(val x: Int, val y: Int) { …

Abstract class

abstract class Element {
def contents: Array[String]
}
  • A method that has no implementation = The method is an abstract member of the class.
  • A class with abstract members must itself be declared abstract.
  • The abstract modifier signifies that the class may have abstract members that do not have an implementation.
  • You cannot instantiate an abstract class.
  • Terminology: declare the abstract method, define no concrete methods.

Parameterless methods

abstract class Element {
def contents: Array[String]
def height: Int = contents.length 1
def height(): Int = contents.length 2
val height = contents.length 3
}
  • 1 is Parameterless Method: Recommended — if the method…

Hi All! Welcome to my Medium! 👏

As my first story, I would like to answer a very important question: Why am I here.

I’m trying to make myself feel in love with learning. Learning starts from memorizing. However, memorizing comes hand in hand with forgetting. I hate forgetting, as I will have to revise all things I’ve learned today somewhere in the future. So, how am I going to enjoy learning?

I’ve been asking this to myself for a long time. One way I’ve found, that makes the revision a little bit easier, is to keep track of everything…

Ruining (Ray) Li

From Oxford. Studying Computer Science and on my way to a badass geek.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store