lecture 2

10
Scala Session 2

Upload: muhammad-fayyaz

Post on 26-Jan-2017

70 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Lecture 2

ScalaSession 2

Page 2: Lecture 2

Course Contents1. The Basics2. Getting up to speed3. Collections 4. Classes and Objects5. Inheritance and Traits6. Functional Programming

Page 3: Lecture 2

Today’s Session (Getting Up to Speed)● Conditional Expressions● Type Hierarchy● Loops● Pattern Matching

Page 4: Lecture 2

Environment Setup

- JDK- sbt (build tool for Scala)- Idea-Intellij

Page 5: Lecture 2

Sample Project

- Lets create a simple project via IntelliJ

Page 6: Lecture 2

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.

Page 7: Lecture 2

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

Page 8: Lecture 2

Type Hierarchy

Page 9: Lecture 2

Loops

- while- for

Page 10: Lecture 2

Pattern Matching

- Simple Matching- Matching on Type- Matching on Sequence- Matching on Tuples