My talk at Bangalore Java Users Group. It was meant developers who want to get them started on Scala. This talk objectives was to get started on creating a project in Scala, write some code using collections and test it using ScalaTest.
Injustice - Developers Among Us (SciFiDevCon 2024)
Getting Started With Scala - Meetu Maltiar
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
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
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