Scala
Session 2
Course Contents
1. The Basics
2. Getting up to speed
3. Collections
4. Classes and Objects
5. Inheritance and Traits
6. Functional Programming
Today’s Session (Getting Up to Speed)
● Conditional Expressions
● Type Hierarchy
● Loops
● Pattern Matching
Environment Setup
- JDK
- sbt (build tool for Scala)
- Idea-Intellij
Sample Project
- Lets create a simple project via IntelliJ
Expressions vs Statements
- Expression returns a value e.g. (1+3)
- Statement carries out an action e.g. (if-else)
- In Scala, almost every construct have a
value.
Conditional Expressions
- if (x > 0) 1 else -1
- This is also equivalent to
- x > 0 ? 1 : -1 (Java, C++)
- Every expression has a type
- Type of expression is supertype of both branches
- Unit type
- if (x >0) 1 else ()
- Think of () as placeholder for “No useful value”
- Think of Unit as analog to void. However void is like empty wallet,
whereas unit is like wallet with a bill labelled “No dollars”
- If condition multiple lines
Type Hierarchy
Loops
- while
- for
Pattern Matching
- Simple Matching
- Matching on Type
- Matching on Sequence
- Matching on Tuples

Lecture 2

  • 1.
  • 2.
    Course Contents 1. TheBasics 2. Getting up to speed 3. Collections 4. Classes and Objects 5. Inheritance and Traits 6. Functional Programming
  • 3.
    Today’s Session (GettingUp to Speed) ● Conditional Expressions ● Type Hierarchy ● Loops ● Pattern Matching
  • 4.
    Environment Setup - JDK -sbt (build tool for Scala) - Idea-Intellij
  • 5.
    Sample Project - Letscreate a simple project via IntelliJ
  • 6.
    Expressions vs Statements -Expression returns a value e.g. (1+3) - Statement carries out an action e.g. (if-else) - In Scala, almost every construct have a value.
  • 7.
    Conditional Expressions - if(x > 0) 1 else -1 - This is also equivalent to - x > 0 ? 1 : -1 (Java, C++) - Every expression has a type - Type of expression is supertype of both branches - Unit type - if (x >0) 1 else () - Think of () as placeholder for “No useful value” - Think of Unit as analog to void. However void is like empty wallet, whereas unit is like wallet with a bill labelled “No dollars” - If condition multiple lines
  • 8.
  • 9.
  • 10.
    Pattern Matching - SimpleMatching - Matching on Type - Matching on Sequence - Matching on Tuples