SlideShare a Scribd company logo
1 of 35
Download to read offline
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

Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from javaIndicThreads
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 
Collections In Scala
Collections In ScalaCollections In Scala
Collections In ScalaKnoldus Inc.
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In ScalaKnoldus Inc.
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Languageleague
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaVladimir Kostyukov
 
Introduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkIntroduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkAngelo Leto
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010JUG Lausanne
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
Scala training workshop 02
Scala training workshop 02Scala training workshop 02
Scala training workshop 02Nguyen Tuan
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala Knoldus Inc.
 
Scala parallel-collections
Scala parallel-collectionsScala parallel-collections
Scala parallel-collectionsKnoldus Inc.
 
Metaprogramming in Scala 2.10, Eugene Burmako,
Metaprogramming  in Scala 2.10, Eugene Burmako, Metaprogramming  in Scala 2.10, Eugene Burmako,
Metaprogramming in Scala 2.10, Eugene Burmako, Vasil Remeniuk
 

What's hot (20)

Scala for curious
Scala for curiousScala for curious
Scala for curious
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from java
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
Collections In Scala
Collections In ScalaCollections In Scala
Collections In Scala
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
 
Introduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkIntroduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with spark
 
Introducing Akka
Introducing AkkaIntroducing Akka
Introducing Akka
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
Scala training workshop 02
Scala training workshop 02Scala training workshop 02
Scala training workshop 02
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
Scala parallel-collections
Scala parallel-collectionsScala parallel-collections
Scala parallel-collections
 
Metaprogramming in Scala 2.10, Eugene Burmako,
Metaprogramming  in Scala 2.10, Eugene Burmako, Metaprogramming  in Scala 2.10, Eugene Burmako,
Metaprogramming in Scala 2.10, Eugene Burmako,
 
Practical cats
Practical catsPractical cats
Practical cats
 
Scala Introduction
Scala IntroductionScala Introduction
Scala Introduction
 
Suit case class
Suit case classSuit case class
Suit case class
 
Google06
Google06Google06
Google06
 

Similar to Getting Started With Scala - Meetu Maltiar

2014 holden - databricks umd scala crash course
2014   holden - databricks umd scala crash course2014   holden - databricks umd scala crash course
2014 holden - databricks umd scala crash courseHolden Karau
 
Scala intro for Java devs 20150324
Scala intro for Java devs 20150324Scala intro for Java devs 20150324
Scala intro for Java devs 20150324Erik Schmiegelow
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with ScalaNeelkanth Sachdeva
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With ScalaMeetu Maltiar
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalUrs Peter
 
Scala parallel-collections
Scala parallel-collectionsScala parallel-collections
Scala parallel-collectionsKnoldus Inc.
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scalaehsoon
 

Similar to Getting Started With Scala - Meetu Maltiar (20)

2014 holden - databricks umd scala crash course
2014   holden - databricks umd scala crash course2014   holden - databricks umd scala crash course
2014 holden - databricks umd scala crash course
 
Scala intro for Java devs 20150324
Scala intro for Java devs 20150324Scala intro for Java devs 20150324
Scala intro for Java devs 20150324
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
 
Meet scala
Meet scalaMeet scala
Meet scala
 
Scala Collections
Scala CollectionsScala Collections
Scala Collections
 
Scala basic
Scala basicScala basic
Scala basic
 
Scala parallel-collections
Scala parallel-collectionsScala parallel-collections
Scala parallel-collections
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 

Recently uploaded

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 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
  • 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