SlideShare a Scribd company logo
Getting Started With Scala
Meetu Maltiar
Cisco
twitter: @meetumaltiar
blog: meetumaltiar.com
1
26th April 2014 Bangalore, INDIA
Agenda
Making a Scala project with SBT
Scala language features
Scala Collections
Scala Test
Wrap up
SBT
Build tool for Scala projects
Scala based frameworks like Akka uses it
SBT build definition uses Scala based DSL
Incremental compilation
Works with mixed scala and java projects
SBT: Create a project
SBT installation: download jar and create a script
instructions: http://www.scala-sbt.org/
Create a directory bojug-scala-bootcamp and descend to it
In terminal type sbt
Once sbt is started enter following commands:
set name := “bojug-scala-bootcamp”
set version := “1.0”
set scalaVersion := “2.10.2”
session save
exit
!
Open build.sbt and have a look!!
SBT: dependency & eclipse plugin
Current build.sbt has same information we inserted before
!
now add a dependency for ScalaTest by adding a following line:
libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.1.0" % “test"
!
our full build.sbt is like this now:
name := "bojug-scala-bootcamp"
version := "1.0"
scalaVersion := “2.10.2"
libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.1.0" % “test"
!
Create projects/plugins.sbt and add sbteclipse plugin by adding a single line:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % “2.1.2")
!
Execute sbt eclipse
It will generate src folder and eclipse related configs. We are ready to import this project to eclipse!!
Scala: Introduction
Scala is a JVM based language
Scala is a hybrid FP and OO making it scalable
Scala has a REPL
Scala is interoperable with java
Scala: Feels like a scripting language
It has a REPL
Types can be inferred
Less boilerplate
scala> var capitals = Map("US" -> "Washington", "France" -> "Paris")
capitals: scala.collection.immutable.Map[String,String] = Map(US -> Washington, France -> Paris)
!
scala> capitals += ("Japan" -> "Tokyo")
!
scala> capitals("Japan")
res1: String = Tokyo
Scala: It’s Object Oriented
Every value is an object
Every operation is a method call
Exceptions like statics and primitives are removed
scala> (1).hashCode
res2: Int = 1
!
scala> (1).+(2)
res3: Int = 3
Scala: Compared With Java
Scala adds Scala removes
pure object system static members
operator overloading primitive types
closures break and continue
mixin composition with traits special treatment of interfaces
existential types wildcards
abstract types raw types
pattern matching enums
Scala: cheat sheet (1) definitions
Scala method definitions
!
def fun(x: Int) = {
result
}
!
def fun = result
!
Scala variable definitions
!
var x: Int = expression
val x: String = expression
Java method definitions
!
Int fun(int x) {
return result
}
!
(no parameterless methods)
!
java variable definitions
!
Int x = expression
final String x = expression
Scala: cheat sheet (2) definitions
Scala class and object
!
class Sample(x: Int, p: Int) {
def instMeth(y: Int): Int = x + y
}
!
object Sample {
def staticMeth(x: Int, y: Int): Int = x * y
}
!
!
!
!
!
!
!
!
!
Java class
!
class Sample {
private final int x;
public final int p;
!
Sample(int x, int p) {
this.x = x;
this.p = p;
}
!
int instMeth(int y) {
return x + y;
}
!
static int staticMeth(int x, int y) {
return x *y;
}
}
Scala: cheat sheet (3) Traits
Scala Trait
!
trait T {
var field = “!”
!
def abstractMeth(x: Int)
def concreteMeth(x: String) = x + field
}
!
Scala mixin composition
!
class C extends Super with T
!
!
!
!
!
Java Interface
!
Interface T {
int abstractMeth(int x)
}
!
(no concrete methods)
!
(no fields)
!
java extension plus implementation
!
class C extends Super implements T
!
!
!
!
Scala: Higher Order Functions
Functions are first class entities in Scala
!
You can create them anonymously, pass them around as parameters or assign it to variable
!
scala> val incrementFunction = (x: Int) = x + 1
incrementFunction: Int => Int = <function1>
!
Higher order function takes other functions as parameters, or whose result is a function
!
def higherOrderFunction(f: Int => Int, x: Int): Int = {
f(x) + x
}
!
higherOrderFunction: (f: Int => Int, x: Int)
!
and when we call higherOrderFunction(incrementFunction, 2) ??
Scala: Pattern matching
All that is required to add a case keyword to each class that is to be pattern matchable
!
Pattern match also returns a value
!
Similar to switch except that Scala compares objects as expressions. Only one matcher is
executed at a time.
!
case class Employee(name: String)
val employee = Employee(“john”)
employee match {
case Employee(“john”) => “Hello John!”
case _ => “Hello there!”
}
!
res0: String = Hello John
Scala: Traits
They are fundamental unit of code reuse in Scala
!
They encapsulate method and field definitions, which can be reused by mixing them in classes
!
They cannot be instantiated so they refer to pure behaviours unlike classes
!
Unlike class inheritance a class can mix any number of traits
!
Unlike interfaces they can have concrete methods
We have already have a look at them in scala cheat sheet
Scala Collections: Introduction
case class Person(name: String, age: Int)
!
val people = List(Person(“John”, 23), Person(“Jack”, 13), Person(“Mary”, 17), Person(“May”, 43))
!
val (minors, adults) = people partition (_.age < 18)
There are three concepts in play here:
Pattern matching
infix method call
a function value
Scala Collections: It’s way
De-emphasise destructive updates
Focus on transformers that map collections to collections
Have a complete range of persistent collections
Scala Collections: Properties
Object-oriented
!
Generic: List[T], Map[K, V]
!
Optionally persistent: scala.collections.immutable
!
Higher order: methods like foreach, map, filter
!
Uniform return type principle: operations return same type as their left operand
Scala Collections: Uniform return type
scala> val ys = List(1, 2, 3)
ys: List[Int] = List(1, 2, 3)
!
scala> val xs: Seq[Int] = ys
xs: Seq[Int] = List(1, 2, 3)
!
scala> xs map (_ + 1)
res0: Seq[Int] = List(2, 3, 4)
!
scala> ys map (_ + 1)
res1: List[Int] = List(2, 3, 4)
Scala Collections: Map and Filter
scala> val xs = List(1, 2, 3)
xs: List[Int] = List(1, 2, 3)
!
scala> val ys = xs map (x => x + 1)
ys: List[Int] = List(2, 3, 4)
!
scala> val ys = xs map (_ + 1)
ys: List[Int] = List(2, 3, 4)
!
scala> val zs = ys filter(_ % 2 == 0)
zs: List[Int] = List(2, 4)
!
scala> val as = ys map (0 to _)
as: List[scala.collection.immutable.Range.Inclusive] =
List(Range(0, 1), Range(0, 1, 2), Range(0, 1, 2, 3))
Scala Collections: flatMap and groupBy
scala> val bs = as.flatten
bs: List[Int] = List(0, 1, 0, 1, 2, 0, 1, 2, 3)
!
scala> val bs = ys flatMap(0 to _)
bs: List[Int] = List(0, 1, 0, 1, 2, 0, 1, 2, 3)
!
scala> val fruits = Vector(“apples, oranges”, “ananas”)
fruits: scala.collection.immutable.Vector[java.lang.String] =
Vector(apples, oranges, ananas)
!
scala> fruit groupBy(_.head)
res2: scala.collection.immutable.Map[char,
scala.collection.immutable.Vector[java.lang.String]] =
Map(a -> Vector(apples, ananas), o -> Vector(oranges))
Scala Collections: for notation
scala> for(x <- xs) yield x + 1 // map
res0: Seq[Int] = List(2, 3, 4)
!
scala> for(x <- res0 if x % 2 == 0) yield x // filter
res1: Seq[Int] = List(2, 4)
!
scala> for(x <- xs; y <- 0 to x) yield y // flatMap
res2: Seq[Int] = List(0, 1, 0, 1, 2, 0, 1, 2, 3)
Scala Collections: Using Maps
scala> val m = Map(1 -> “ABC”, 2 -> “DEF”, 3 -> “GHI”)
m:scala.collection.immutable.Map[Int, java.lang.String] =
Map(1 -> ABC, 2 -> DEF, 3 -> GHI)
!
scala> m(2)
res1: java.lang.String = DEF
!
scala> m + (4 -> “JKL”)
res2: scala.collection.immutable.Map[Int, java.lang.String] =
Map(1 -> ABC, 2 -> DEF, 3 -> GHI, 4 -> JKL)
!
scala> m map { case (k, v) => (v, k)}
res3: scala.collection.immutable.Map[java.lang.String, Int] =
Map(ABC -> 1, DEF -> 2, GHI -> 3)
23
Scala Collections: Hierarchy
All collection are in scala.collection or one of its sub-packages
mutable, immutable and generic
!
Root collections are in scala.collection define same interface as
immutable collections and mutable collections and adds some
modification operations to make it mutable
!
The generic package contains building block for implementing
collections
24
Scala Collections: Hierarchy
25
Traversable
Iterable
Seq
IndexedSeq
LinearSeq
mutable.buffer
Range
Set
SortedSet
immutable.HashSet
mutable.HashSet
mutable.LinkedHashSet
BitSet
Map
SortedMap
immutable.HashMap
mutable.HashMap
mutable.LinkedHashMap
Scala Collections: Trait Traversable
26
Top of collection hierarchy. Its abstract method is foreach
def foreach[U](f: Elem => U)
!
Traversable also provides lot of concrete methods they fall in following
categories
!
Addition: ++, appends two traversables together
Map operations: map, flatMap, collect
Conversions: toArray, toList, toIterable, toSeq, toIndexedSeq, toStream, toSet, toMap
Copying operations: copyToBuffer and copyToArray
Size info operations: isEmpty, nonEmpty, size and hasDefiniteSize
Element retrieval operations: head, last, headOption, lastOption and find
Sub-collection retrieval operations: tail, init, slice, take, drop, takeWhile, dropWhile, filter, filterNot, withFilter
Subdivision operations: splitAt, span, partition, groupBy
Element tests: exists, forAll, count
Folds: foldLeft, foldRight, /:, :, reduceLeft, reduceRight
Specific Folds: sum, product, min, max
String operations: mkString, addString, stringPrefix
Scala Collections: Everything is a library
27
Collections feel that they are like language constructs
!
Language does not contain any collection related constructs
- no collection types
- no collection literals
- no collection operators
!
Everything is a library
!
They are extensible
Scala Test: Introduction
28
ScalaTest is an open source framework for java platform
!
With ScalaTest we can test either Scala or Java code
!
Integrates with popular tools like jUnit, TestNG, Ant, Maven and SBT
!
Designed to do different styles of testing like Behaviour Driven Design
for example
Scala Test: Concepts
29
Three concepts:
!
Suite: A collection of tests. A test is anything which has a name and
can succeed or fail
!
Runner: ScalaTest provides a runner application and can run a suite of
tests
!
Reporter: As the tests are run, events are fired to the reporter, it takes
care of presenting results back to user
Scala Test: It is customisable
30
Suite
<<trait>>
def expectedTestCount(Filter: Int)
def testNames: Set[String]
def tags: Map[String, Set[String]]
def nestedSuites: List[Suite]
def run(Option[String], Reporter, …)
def runNestedSuites(Reporter, …)
def runTests(Option[String], Reporter)
def runTest(Reporter, …)
def withFixture(NoArgTest)
Scala Test: under the hood
31
When you run Test in ScalaTest you basically invoke
run(Option[String], Reporter, …) on Suite object
It then calls runNestedSuites(Reporter, …)
And it calls runTests(Option[String], Reporter, …)
!
runNestedSuites(Reporter, …):
Invokes nestedSuites(): List[Suite] to get all nested suites
!
runTests(Option[String], Reporter, …) will call def testNames:
Set[String] to get set of tests to run. For each test it calls
runTest(Reporter, …) It wraps the test code as a function object
with a name and passes it to the withFixture(NoArgTest) which actually
runs the test
Scala Test: Available Traits
32
Suite
FunSuite
Spec
FlatSpec
WordSpec
FeatureSpec
Assertions
ShouldMatchers
MustMatchers
Scala Test: FunSuite
33
ScalaTest provided === operator. It is defined in trait Assertions.
Allows the failure report to include both left and right values
!
For writing Functional Tests use FunSuite trait. Test name goes in
parentheses and test body goes in curly braces
!
The test code in curly braces is passed as a by-name parameter to
“test” method which registers for later execution
Scala Test: FunSuite Example
34
import org.scalatest.FunSuite	
!
class EmployeeFunSuiteTest extends FunSuite {	
!
test("employees with same name are same") {	
val emp1 = Employee("john")	
val emp2 = Employee("john")	
emp1 === emp2	
}	
!
}	
!
case class Employee(name: String)
Assignment
35
Lets map the world. We have Continents and Countries
!
Make a collection hierarchy to hold the above information
!
Write method on the collection hierarchy to get countries for a
continent
!
Write method on the collection hierarchy to get continent for a country
!
Write tests using FunSuite to test methods created above

More Related Content

What's hot (20)

PPTX
Scala for curious
Tim (dev-tim) Zadorozhniy
 
PDF
Scala collections api expressivity and brevity upgrade from java
IndicThreads
 
PDF
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
ODP
Collections In Scala
Knoldus Inc.
 
PDF
Data Structures In Scala
Knoldus Inc.
 
PPT
An introduction to scala
Mohsen Zainalpour
 
PDF
The Scala Programming Language
league
 
PPTX
Purely Functional Data Structures in Scala
Vladimir Kostyukov
 
PDF
Introduction to parallel and distributed computation with spark
Angelo Leto
 
PDF
Introducing Akka
Meetu Maltiar
 
PDF
Introduction à Scala - Michel Schinz - January 2010
JUG Lausanne
 
PPTX
Taxonomy of Scala
shinolajla
 
PPTX
Scala training workshop 02
Nguyen Tuan
 
ODP
Functions In Scala
Knoldus Inc.
 
PDF
Scala parallel-collections
Knoldus Inc.
 
PDF
Metaprogramming in Scala 2.10, Eugene Burmako,
Vasil Remeniuk
 
PDF
Practical cats
Raymond Tay
 
PPTX
Scala Introduction
Constantine Nosovsky
 
PDF
Suit case class
Didier Plaindoux
 
PDF
Google06
Zhiwen Guo
 
Scala for curious
Tim (dev-tim) Zadorozhniy
 
Scala collections api expressivity and brevity upgrade from java
IndicThreads
 
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
Collections In Scala
Knoldus Inc.
 
Data Structures In Scala
Knoldus Inc.
 
An introduction to scala
Mohsen Zainalpour
 
The Scala Programming Language
league
 
Purely Functional Data Structures in Scala
Vladimir Kostyukov
 
Introduction to parallel and distributed computation with spark
Angelo Leto
 
Introducing Akka
Meetu Maltiar
 
Introduction à Scala - Michel Schinz - January 2010
JUG Lausanne
 
Taxonomy of Scala
shinolajla
 
Scala training workshop 02
Nguyen Tuan
 
Functions In Scala
Knoldus Inc.
 
Scala parallel-collections
Knoldus Inc.
 
Metaprogramming in Scala 2.10, Eugene Burmako,
Vasil Remeniuk
 
Practical cats
Raymond Tay
 
Scala Introduction
Constantine Nosovsky
 
Suit case class
Didier Plaindoux
 
Google06
Zhiwen Guo
 

Similar to Getting Started With Scala (20)

PDF
2014 holden - databricks umd scala crash course
Holden Karau
 
PPTX
Scala intro for Java devs 20150324
Erik Schmiegelow
 
ODP
Functional Programming With Scala
Knoldus Inc.
 
ODP
Functional programming with Scala
Neelkanth Sachdeva
 
PDF
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
PDF
Getting Started With Scala
Meetu Maltiar
 
PDF
Spark workshop
Wojciech Pituła
 
PDF
scalaliftoff2009.pdf
Hiroshi Ono
 
PDF
scalaliftoff2009.pdf
Hiroshi Ono
 
PDF
scalaliftoff2009.pdf
Hiroshi Ono
 
PDF
scalaliftoff2009.pdf
Hiroshi Ono
 
PPT
Scala Talk at FOSDEM 2009
Martin Odersky
 
ODP
Scala ntnu
Alf Kristian Støyle
 
PPTX
Xebicon2013 scala vsjava_final
Urs Peter
 
PDF
Meet scala
Wojciech Pituła
 
PDF
Scala Collections
Meetu Maltiar
 
PPTX
Scala basic
Nguyen Tuan
 
PDF
Scala parallel-collections
Knoldus Inc.
 
PPTX
Principles of functional progrmming in scala
ehsoon
 
PPT
Scala introduction
Yardena Meymann
 
2014 holden - databricks umd scala crash course
Holden Karau
 
Scala intro for Java devs 20150324
Erik Schmiegelow
 
Functional Programming With Scala
Knoldus Inc.
 
Functional programming with Scala
Neelkanth Sachdeva
 
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
Getting Started With Scala
Meetu Maltiar
 
Spark workshop
Wojciech Pituła
 
scalaliftoff2009.pdf
Hiroshi Ono
 
scalaliftoff2009.pdf
Hiroshi Ono
 
scalaliftoff2009.pdf
Hiroshi Ono
 
scalaliftoff2009.pdf
Hiroshi Ono
 
Scala Talk at FOSDEM 2009
Martin Odersky
 
Xebicon2013 scala vsjava_final
Urs Peter
 
Meet scala
Wojciech Pituła
 
Scala Collections
Meetu Maltiar
 
Scala basic
Nguyen Tuan
 
Scala parallel-collections
Knoldus Inc.
 
Principles of functional progrmming in scala
ehsoon
 
Scala introduction
Yardena Meymann
 
Ad

More from Meetu Maltiar (6)

PDF
Hands-On AWS: Java SDK + CLI for Cloud Developers
Meetu Maltiar
 
PDF
Fitnesse With Scala
Meetu Maltiar
 
PDF
Akka 2.0 Reloaded
Meetu Maltiar
 
PDF
Scala categorytheory
Meetu Maltiar
 
PDF
Scala test
Meetu Maltiar
 
PPTX
Easy ORMness with Objectify-Appengine
Meetu Maltiar
 
Hands-On AWS: Java SDK + CLI for Cloud Developers
Meetu Maltiar
 
Fitnesse With Scala
Meetu Maltiar
 
Akka 2.0 Reloaded
Meetu Maltiar
 
Scala categorytheory
Meetu Maltiar
 
Scala test
Meetu Maltiar
 
Easy ORMness with Objectify-Appengine
Meetu Maltiar
 
Ad

Recently uploaded (20)

PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 

Getting Started With Scala

  • 1. Getting Started With Scala Meetu Maltiar Cisco twitter: @meetumaltiar blog: meetumaltiar.com 1 26th April 2014 Bangalore, INDIA
  • 2. Agenda Making a Scala project with SBT Scala language features Scala Collections Scala Test Wrap up
  • 3. SBT Build tool for Scala projects Scala based frameworks like Akka uses it SBT build definition uses Scala based DSL Incremental compilation Works with mixed scala and java projects
  • 4. SBT: Create a project SBT installation: download jar and create a script instructions: http://www.scala-sbt.org/ Create a directory bojug-scala-bootcamp and descend to it In terminal type sbt Once sbt is started enter following commands: set name := “bojug-scala-bootcamp” set version := “1.0” set scalaVersion := “2.10.2” session save exit ! Open build.sbt and have a look!!
  • 5. SBT: dependency & eclipse plugin Current build.sbt has same information we inserted before ! now add a dependency for ScalaTest by adding a following line: libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.1.0" % “test" ! our full build.sbt is like this now: name := "bojug-scala-bootcamp" version := "1.0" scalaVersion := “2.10.2" libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.1.0" % “test" ! Create projects/plugins.sbt and add sbteclipse plugin by adding a single line: addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % “2.1.2") ! Execute sbt eclipse It will generate src folder and eclipse related configs. We are ready to import this project to eclipse!!
  • 6. Scala: Introduction Scala is a JVM based language Scala is a hybrid FP and OO making it scalable Scala has a REPL Scala is interoperable with java
  • 7. Scala: Feels like a scripting language It has a REPL Types can be inferred Less boilerplate scala> var capitals = Map("US" -> "Washington", "France" -> "Paris") capitals: scala.collection.immutable.Map[String,String] = Map(US -> Washington, France -> Paris) ! scala> capitals += ("Japan" -> "Tokyo") ! scala> capitals("Japan") res1: String = Tokyo
  • 8. Scala: It’s Object Oriented Every value is an object Every operation is a method call Exceptions like statics and primitives are removed scala> (1).hashCode res2: Int = 1 ! scala> (1).+(2) res3: Int = 3
  • 9. Scala: Compared With Java Scala adds Scala removes pure object system static members operator overloading primitive types closures break and continue mixin composition with traits special treatment of interfaces existential types wildcards abstract types raw types pattern matching enums
  • 10. Scala: cheat sheet (1) definitions Scala method definitions ! def fun(x: Int) = { result } ! def fun = result ! Scala variable definitions ! var x: Int = expression val x: String = expression Java method definitions ! Int fun(int x) { return result } ! (no parameterless methods) ! java variable definitions ! Int x = expression final String x = expression
  • 11. Scala: cheat sheet (2) definitions Scala class and object ! class Sample(x: Int, p: Int) { def instMeth(y: Int): Int = x + y } ! object Sample { def staticMeth(x: Int, y: Int): Int = x * y } ! ! ! ! ! ! ! ! ! Java class ! class Sample { private final int x; public final int p; ! Sample(int x, int p) { this.x = x; this.p = p; } ! int instMeth(int y) { return x + y; } ! static int staticMeth(int x, int y) { return x *y; } }
  • 12. Scala: cheat sheet (3) Traits Scala Trait ! trait T { var field = “!” ! def abstractMeth(x: Int) def concreteMeth(x: String) = x + field } ! Scala mixin composition ! class C extends Super with T ! ! ! ! ! Java Interface ! Interface T { int abstractMeth(int x) } ! (no concrete methods) ! (no fields) ! java extension plus implementation ! class C extends Super implements T ! ! ! !
  • 13. Scala: Higher Order Functions Functions are first class entities in Scala ! You can create them anonymously, pass them around as parameters or assign it to variable ! scala> val incrementFunction = (x: Int) = x + 1 incrementFunction: Int => Int = <function1> ! Higher order function takes other functions as parameters, or whose result is a function ! def higherOrderFunction(f: Int => Int, x: Int): Int = { f(x) + x } ! higherOrderFunction: (f: Int => Int, x: Int) ! and when we call higherOrderFunction(incrementFunction, 2) ??
  • 14. Scala: Pattern matching All that is required to add a case keyword to each class that is to be pattern matchable ! Pattern match also returns a value ! Similar to switch except that Scala compares objects as expressions. Only one matcher is executed at a time. ! case class Employee(name: String) val employee = Employee(“john”) employee match { case Employee(“john”) => “Hello John!” case _ => “Hello there!” } ! res0: String = Hello John
  • 15. Scala: Traits They are fundamental unit of code reuse in Scala ! They encapsulate method and field definitions, which can be reused by mixing them in classes ! They cannot be instantiated so they refer to pure behaviours unlike classes ! Unlike class inheritance a class can mix any number of traits ! Unlike interfaces they can have concrete methods We have already have a look at them in scala cheat sheet
  • 16. Scala Collections: Introduction case class Person(name: String, age: Int) ! val people = List(Person(“John”, 23), Person(“Jack”, 13), Person(“Mary”, 17), Person(“May”, 43)) ! val (minors, adults) = people partition (_.age < 18) There are three concepts in play here: Pattern matching infix method call a function value
  • 17. Scala Collections: It’s way De-emphasise destructive updates Focus on transformers that map collections to collections Have a complete range of persistent collections
  • 18. Scala Collections: Properties Object-oriented ! Generic: List[T], Map[K, V] ! Optionally persistent: scala.collections.immutable ! Higher order: methods like foreach, map, filter ! Uniform return type principle: operations return same type as their left operand
  • 19. Scala Collections: Uniform return type scala> val ys = List(1, 2, 3) ys: List[Int] = List(1, 2, 3) ! scala> val xs: Seq[Int] = ys xs: Seq[Int] = List(1, 2, 3) ! scala> xs map (_ + 1) res0: Seq[Int] = List(2, 3, 4) ! scala> ys map (_ + 1) res1: List[Int] = List(2, 3, 4)
  • 20. Scala Collections: Map and Filter scala> val xs = List(1, 2, 3) xs: List[Int] = List(1, 2, 3) ! scala> val ys = xs map (x => x + 1) ys: List[Int] = List(2, 3, 4) ! scala> val ys = xs map (_ + 1) ys: List[Int] = List(2, 3, 4) ! scala> val zs = ys filter(_ % 2 == 0) zs: List[Int] = List(2, 4) ! scala> val as = ys map (0 to _) as: List[scala.collection.immutable.Range.Inclusive] = List(Range(0, 1), Range(0, 1, 2), Range(0, 1, 2, 3))
  • 21. Scala Collections: flatMap and groupBy scala> val bs = as.flatten bs: List[Int] = List(0, 1, 0, 1, 2, 0, 1, 2, 3) ! scala> val bs = ys flatMap(0 to _) bs: List[Int] = List(0, 1, 0, 1, 2, 0, 1, 2, 3) ! scala> val fruits = Vector(“apples, oranges”, “ananas”) fruits: scala.collection.immutable.Vector[java.lang.String] = Vector(apples, oranges, ananas) ! scala> fruit groupBy(_.head) res2: scala.collection.immutable.Map[char, scala.collection.immutable.Vector[java.lang.String]] = Map(a -> Vector(apples, ananas), o -> Vector(oranges))
  • 22. Scala Collections: for notation scala> for(x <- xs) yield x + 1 // map res0: Seq[Int] = List(2, 3, 4) ! scala> for(x <- res0 if x % 2 == 0) yield x // filter res1: Seq[Int] = List(2, 4) ! scala> for(x <- xs; y <- 0 to x) yield y // flatMap res2: Seq[Int] = List(0, 1, 0, 1, 2, 0, 1, 2, 3)
  • 23. Scala Collections: Using Maps scala> val m = Map(1 -> “ABC”, 2 -> “DEF”, 3 -> “GHI”) m:scala.collection.immutable.Map[Int, java.lang.String] = Map(1 -> ABC, 2 -> DEF, 3 -> GHI) ! scala> m(2) res1: java.lang.String = DEF ! scala> m + (4 -> “JKL”) res2: scala.collection.immutable.Map[Int, java.lang.String] = Map(1 -> ABC, 2 -> DEF, 3 -> GHI, 4 -> JKL) ! scala> m map { case (k, v) => (v, k)} res3: scala.collection.immutable.Map[java.lang.String, Int] = Map(ABC -> 1, DEF -> 2, GHI -> 3) 23
  • 24. Scala Collections: Hierarchy All collection are in scala.collection or one of its sub-packages mutable, immutable and generic ! Root collections are in scala.collection define same interface as immutable collections and mutable collections and adds some modification operations to make it mutable ! The generic package contains building block for implementing collections 24
  • 26. Scala Collections: Trait Traversable 26 Top of collection hierarchy. Its abstract method is foreach def foreach[U](f: Elem => U) ! Traversable also provides lot of concrete methods they fall in following categories ! Addition: ++, appends two traversables together Map operations: map, flatMap, collect Conversions: toArray, toList, toIterable, toSeq, toIndexedSeq, toStream, toSet, toMap Copying operations: copyToBuffer and copyToArray Size info operations: isEmpty, nonEmpty, size and hasDefiniteSize Element retrieval operations: head, last, headOption, lastOption and find Sub-collection retrieval operations: tail, init, slice, take, drop, takeWhile, dropWhile, filter, filterNot, withFilter Subdivision operations: splitAt, span, partition, groupBy Element tests: exists, forAll, count Folds: foldLeft, foldRight, /:, :, reduceLeft, reduceRight Specific Folds: sum, product, min, max String operations: mkString, addString, stringPrefix
  • 27. Scala Collections: Everything is a library 27 Collections feel that they are like language constructs ! Language does not contain any collection related constructs - no collection types - no collection literals - no collection operators ! Everything is a library ! They are extensible
  • 28. Scala Test: Introduction 28 ScalaTest is an open source framework for java platform ! With ScalaTest we can test either Scala or Java code ! Integrates with popular tools like jUnit, TestNG, Ant, Maven and SBT ! Designed to do different styles of testing like Behaviour Driven Design for example
  • 29. Scala Test: Concepts 29 Three concepts: ! Suite: A collection of tests. A test is anything which has a name and can succeed or fail ! Runner: ScalaTest provides a runner application and can run a suite of tests ! Reporter: As the tests are run, events are fired to the reporter, it takes care of presenting results back to user
  • 30. Scala Test: It is customisable 30 Suite <<trait>> def expectedTestCount(Filter: Int) def testNames: Set[String] def tags: Map[String, Set[String]] def nestedSuites: List[Suite] def run(Option[String], Reporter, …) def runNestedSuites(Reporter, …) def runTests(Option[String], Reporter) def runTest(Reporter, …) def withFixture(NoArgTest)
  • 31. Scala Test: under the hood 31 When you run Test in ScalaTest you basically invoke run(Option[String], Reporter, …) on Suite object It then calls runNestedSuites(Reporter, …) And it calls runTests(Option[String], Reporter, …) ! runNestedSuites(Reporter, …): Invokes nestedSuites(): List[Suite] to get all nested suites ! runTests(Option[String], Reporter, …) will call def testNames: Set[String] to get set of tests to run. For each test it calls runTest(Reporter, …) It wraps the test code as a function object with a name and passes it to the withFixture(NoArgTest) which actually runs the test
  • 32. Scala Test: Available Traits 32 Suite FunSuite Spec FlatSpec WordSpec FeatureSpec Assertions ShouldMatchers MustMatchers
  • 33. Scala Test: FunSuite 33 ScalaTest provided === operator. It is defined in trait Assertions. Allows the failure report to include both left and right values ! For writing Functional Tests use FunSuite trait. Test name goes in parentheses and test body goes in curly braces ! The test code in curly braces is passed as a by-name parameter to “test” method which registers for later execution
  • 34. Scala Test: FunSuite Example 34 import org.scalatest.FunSuite ! class EmployeeFunSuiteTest extends FunSuite { ! test("employees with same name are same") { val emp1 = Employee("john") val emp2 = Employee("john") emp1 === emp2 } ! } ! case class Employee(name: String)
  • 35. Assignment 35 Lets map the world. We have Continents and Countries ! Make a collection hierarchy to hold the above information ! Write method on the collection hierarchy to get countries for a continent ! Write method on the collection hierarchy to get continent for a country ! Write tests using FunSuite to test methods created above