Slide 1

Slide 1 text

Introducing Scala Meetu Maltiar Principal Consultant Knoldus

Slide 2

Slide 2 text

Agenda Starting a Scala project Scala as a language Scala Collections Scala Test

Slide 3

Slide 3 text

SBT Build tool for Scala based projects Scala based frameworks like Akka uses it SBT build definition uses scala based DSL Incremental compilation Works with mixed Scala and Java based projects

Slide 4

Slide 4 text

SBT: Lets create a project SBT Installation download jar and create a script Instructions: xsbt wiki Descend in directory where you wanna create the project In terminal type sbt Once the sbt is started enter following commands set name := “ScalaKnolx” set version := “1.0” set scalaVersion := “2.9.1” session save exit Open build.sbt and have a look!!

Slide 5

Slide 5 text

SBT: Eclipse IDE Add Typesafe repo and sbteclipse plugin In build.sbt resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/" libraryDependencies += "org.scalatest" %% "scalatest" % "1.6.1" libraryDependencies += "junit" % "junit" % "4.9" Create project/plugins.sbt and add sbteclipse plugin addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.0.0") Execute sbt eclipse It will generate eclipse related configs and now we are ready to import this project in eclipse!!

Slide 6

Slide 6 text

Scala Introduction Scala is a JVM based language Scala combines FP and OO which makes it a scalable language Scala has a REPL Scala is interoperable with Java

Slide 7

Slide 7 text

Scala is a scripting language It has a REPL. Types can be inferred Less Boilerplate Scala> var capital = Map(“US” → “Washington”, “France” → “Paris”) Capital: Map[String, String] = Map(US-> Washington, France->Paris) Scala> capital += (“japan” → “Tokyo”) Scala> capital(“France”) Res2: String = Paris

Slide 8

Slide 8 text

Scala is OO Every value is an object Every operation is method call Exceptions to this in java like statics and primitives are removed Scala> (1).hashCode Res1: Int = 1 Scala> (1).+(2) Res2: Int = 3

Slide 9

Slide 9 text

Scala compared to Java Scala adds Scala removes + pure object system - static members + operator overloading - primitive types + closures - break, continue + mixin composition with traits - special treatment of interfaces + existential types - wildcards + abstract types - raw types + pattern matching - enums

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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 method definitions 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; } }

Slide 12

Slide 12 text

Scala cheat sheet (3): Traits Scala Trait trait T { var field = “!” def abstractMth(x: Int): Int def concMth(x: String) = x + field } Scala mixin composition class C extends Super with T Java Interface Interface T { Int abstractMth(String x) } (no concrete methods) (no fields) Java extension plus implementation class C extends Super implements T

Slide 13

Slide 13 text

Scala HOF Scala is also FP along-with OO This means that Function is also an Object They can be passed along as objects private def higherOrderFunction(f: Int => Int, x: Int): Int = { f(x) + 1 }

Slide 14

Slide 14 text

Scala Pattern Match All that is required is to add case keyword to each class that is to be pattern matchable Similar to switch except that Scala compares objects as expressions getInteger(4) match { case 4 => println("four") case _ => println("not four") } def getInteger(x: Int): Int = { x }

Slide 15

Slide 15 text

Scala Traits They are fundamental unit of code reuse in Scala They encapsulates method and field definitions, which can be reused by mixing them in classes Unlike class inheritance a class can mix any number of traits Unlike Interfaces they can have concrete methods

Slide 16

Slide 16 text

Scala Collections Class Person(val name: String, age: Int) val people: Array[Person] val(minors, adults) = people partition (_.age < 18) Three concepts: - pattern mach - infix method call - a function value

Slide 17

Slide 17 text

Scala way of Collections De-emphasize destructive updates Focus on transformers that map collections to collections Have complete range of persistent collections

Slide 18

Slide 18 text

Collection 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

Slide 19

Slide 19 text

Uniform Return Type Principle 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)

Slide 20

Slide 20 text

Using 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) xs: 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))

Slide 21

Slide 21 text

Using 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 fruit = Vector(“apples”, “oranges”, “ananas”) fruit: 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))

Slide 22

Slide 22 text

Using 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)

Slide 23

Slide 23 text

String also a collection scala> val aString = “hello world” aString: java.lang.String = hello world scala> aString map (_.toUpper) res1: String = HELLO WORLD Even String is a collection that means that we can apply higher order functions on it

Slide 24

Slide 24 text

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)} res2: scala.collection.immutable.Map[java.lang.String, Int] = Map(ABC → 1, DEF → 2, GHI → 3)

Slide 25

Slide 25 text

Scala Collection Hierarchy All collection classes 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 add some modification operations to make it mutable The generic package contains building block for implementing collections

Slide 26

Slide 26 text

Scala.Collection Hierarchy

Slide 27

Slide 27 text

Scala.Collection.Immutable

Slide 28

Slide 28 text

Overview of Collections

Slide 29

Slide 29 text

Commonality In Collections All classes are quite common. For instance every collection can be created by same uniform syntax Set(1, 2, 3) Seq(1, 2, 3) Traversable(1, 2, 3) Map(“x” → 24, “y” → 25) Applies with specific collection implementations List(1, 2, 3) HashMap(“x” → 24, “y” → 25) All these collections get displayed with toString in same way

Slide 30

Slide 30 text

Trait Traversable 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

Slide 31

Slide 31 text

Everything is a library Collections feel that they are language constructs Language does not contain any collection related constructs - no collection types - no collection literals - no collection operators Everything is in library They are extensible

Slide 32

Slide 32 text

Scala Test Scala Test 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 Behavior Driven Design for example

Slide 33

Slide 33 text

Scala Test Concepts 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 reporter, it takes care of presenting results back to user

Slide 34

Slide 34 text

Scala Test Is Customizable Suite <> 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)

Slide 35

Slide 35 text

Scala Test: under the hood When you run a Test in Scala Test 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 a list of nested suites runTests(Option[String], Reporter, …) will call def testNames: Set[String] to get set of test names 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

Slide 36

Slide 36 text

Pick a core Trait

Slide 37

Slide 37 text

Mixin other Traits

Slide 38

Slide 38 text

Scala Test: Available Traits Suite FunSuite Spec FlatSpec WordSpec FeatureSpec Assertions ShouldMatchers MustMatchers

Slide 39

Slide 39 text

Suite Traits approach to writing tests. Create classes extending Suite and define test methods Test methods have names testXXXX. All methods must be public Scala Test provides === operator. It is defined in Trait Assertions. Allows the failure report to include both right and left values

Slide 40

Slide 40 text

FunSuite For writing Functional Tests use FunSuite Fun => functional “test” is a method defined in 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

Slide 41

Slide 41 text

Assignment 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 of a continent Write method on the collection hierarchy to get continent for a country Write tests using FunSuite to test the methods created above