SlideShare une entreprise Scribd logo
1  sur  58
Télécharger pour lire hors ligne
Scala for Java Developers
Ramnivas Laddad
@ramnivas
@ramnivas
•  Author of books and articles
–  AspectJ in Action (1st and 2nd edition)
•  Spring framework, Cloud Foundry
•  Main interests
–  Cloud computing
–  Aspect-oriented programming
–  Scala and functional programming
•  Speaker at many professional conferences
–  JavaOne, JavaPolis, SpringOne, Software Development, No Fluff Just Stuff,
EclipseCon, O’Reilly OSCON etc.
•  Active involvement in AspectJ, Spring, and Cloud Foundry since
their early form
What is Scala
3
“a general purpose programming language designed to
express common programming patterns in a concise,
elegant, and type-safe way. It smoothly integrates features
of object-oriented and functional languages, enabling Java
and other programmers to be more productive.”
http://www.scala-lang.org
Object-oriented
•  Everything is an object
–  No “primitives”
•  Classes
–  Same as Java, but concise
•  Traits
–  Interfaces done right
•  Singletons
–  Language-level concept
4
Statically typed
•  Rich type system (by Java’s standard)
–  Higher-kinded types
–  Implicit conversions
–  Type evidence
•  Expressive type system
•  Inferred types
5
Functional Programming
•  Functions as values
–  May be
•  Saved
•  Passed to other functions (higher-order functions)
 No need to write ugly anonymous classes
–  Advanced pattern matching
–  Expressions return a value
•  if/else, try/catch, match, …
•  Promotes immutability
–  But doesn’t force it
6
Java Interoperability
•  Compiles to Java byte code
–  Jars, wars, …
–  No special operational change
•  Scala calling Java, Java calling Scala code is fine
•  Whole Java eco-system at your service
•  You can write apps using Scala and Spring, today
7
Hello World: Scripting Style
$ scala hello-script.scala	
Hello World
	
println("Hello World")	
No compilation
Hello World: Porting of Java Code
$ scalac hello-java.scala	
$ scala example.Main	
Hello World
// hello-java.scala	
package example	
	
object Main {	
def main(args: Array[String]) {	
	println("Hello World") 	
}	
}
‘static’
Inferred
semicolons
Hello World: Using the App trait
$ scalac hello-app.scala	
$ scala example.Main	
Hello World
// hello-app.scala	
package example	
	
object Main extends App {	
println("Hello World") 	
}
Simple Class
class Person	
val p = new Person	
Type
Inferred
Default
access: public
No curly
braces needed
(but allowed)
Simple Class
class Person	
val p: Person = new Person	
Explicit type
specification
Class with constructor
class Person(firstName: String, 	
lastName: String)	
val p = new Person("Ramnivas", "Laddad")	
println(p.firstName) // Error
Primary
constructor
Fields –
accessible in
class body
Class with “getters”
class Person(val firstName: String, 	
val lastName: String)	
val p = new Person("Ramnivas", "Laddad")	
println(p.firstName)
Value
(Java ‘final’)
Class with “getters” and “setters”
class Person(var firstName: String, 	
var lastName: String)	
val p = new Person("Ramnivas", "Laddad")	
println(p.firstName)	
p.firstName = "Ramnivas2”
Variable (Java
non-final)
Extending a class
class Student(firstName: String, 	
lastName: String, 	
val grade: Int)	
extends Person(firstName, lastName)	
val s = new Student("Ramnivas", "Laddad", 1)	
println(s.firstName)	
println(s.grade)
Defining methods
class Person(val firstName: String, 	
val lastName: String) {	
def name = firstName + " " + lastName	
	
override def toString = name 	
}	
val p = new Person("Ramnivas", "Laddad")	
println(p.name) // Ramnivas Laddad	
println(p) // Ramnivas Laddad
Not optional
Uniform access principle
class Person(val firstName: String, 	
val lastName: String) {	
val name = firstName + " " + lastName	
	
override def toString = name 	
}	
val p = new Person("Ramnivas", "Laddad")	
println(p.name) // Ramnivas Laddad	
println(p) // Ramnivas Laddad
Names in Scala
•  Class, method, field names can contain non alpha-
numeric characters
–  ::
–  :::
–  ~>
–  f@#:
•  Valuable if used judiciously
–  DSLs
19
More about methods and fields
•  Declaring abstract methods and fields
–  Just don’t provide definition
def learn(subject: String)	
val knowledge
•  Classes with abstract method must be declared abstract
–  Just as in Java
•  Methods can be defined inside methods
•  Methods may be marked @tailrec to check for tail-
recursiveness
20
Finer access control levels
•  Default access level: public
•  Protected: protected
–  Same as Java
•  Private:
–  private 	
–  private[this] Access only from this instance 	
–  private[package-name] Access from package and its
subpackages
	
21
Traits: Interfaces done right
22
trait PartyGoer {	
val age: Int	
val yearsUntilLegalDrinking = 	
if (age >= 21) 0 else 21-age	
}	
class Student(firstName: String, lastName: String, 	
val age: Int, val grade: Int) 	
extends Person(firstName, lastName) 	
with PartyGoer
val s = new Student("a", "b", 17, 12)	
s.yearsUntilLegalDrinking // 4
Collections
val people = List("John", "Jacob", 	
"Mike")	
val firstPerson = people(0)	
println(firstPerson) // John
Collections
val people = Array("John", "Jacob", 	
"Mike")	
val firstPerson = people(0)	
println(firstPerson) // John
Working with collections: for comprehension
25
for (person <- people) {	
println(person)	
}
Working with collections: for comprehension
26
for (person <- people if person startsWith "J") {	
println("""Lucky one to start name in "J" """ + person)	
}	
	
// You are lucky one to start name in "J" John	
// You are lucky one to start name in "J" Jacob
Working with collections: for comprehension
27
for (person <- people if person startsWith "J") {	
println(s"""Lucky one to start name in "J" $person""")	
}	
	
// You are lucky one to start name in "J" John	
// You are lucky one to start name in "J" Jacob
Working with collections: filter
val student1 = new Student("first1", "last1", 1)	
val student2 = new Student("first2", "last2", 1)	
val student3 = new Student("first3", "last3", 2)	
val student4 = new Student("first4", "last4", 6)	
val students = List(student1, student2, 	
student3, student4)	
val firstGraders = students.filter(	
s => s.grade == 1)	
println(firstGraders) 	
// List(first1 last1, first2 last2)
Working with collections: filter
val student1 = new Student("first1", "last1", 1)	
val student2 = new Student("first2", "last2", 1)	
val student3 = new Student("first3", "last3", 2)	
val student4 = new Student("first4", "last4", 6)	
val students = List(student1, student2, 	
student3, student4)	
val inFirstGrade: Student => Boolean 	
= s => s.grade == 1	
	
val firstGraders = students.filter(inFirstGrade)	
// List(first1 last1, first2 last2)
Working with collections: using _
val student1 = new Student("first1", "last1", 1)	
val student2 = new Student("first2", "last2", 1)	
val student3 = new Student("first3", "last3", 2)	
val student4 = new Student("first4", "last4", 6)	
val students = List(student1, student2, 	
student3, student4)	
val firstGraders 	
= students.filter(_.grade == 1)	
println(firstGraders) 	
// List(first1 last1, first2 last2)
Working with collections: passing method as function
val student1 = new Student("first1", "last1", 1)	
val student2 = new Student("first2", "last2", 1)	
val student3 = new Student("first3", "last3", 2)	
val student4 = new Student("first4", "last4", 6)	
val students = List(student1, student2, 	
student3, student4)	
def inFirstGrade(s: Student) : Boolean 	
= s.grade == 1 	
val firstGraders = students.filter(inFirstGrade)	
// List(first1 last1, first2 last2)
Working with collections: partition
val student1 = new Student("first1", "last1", 1)	
val student2 = new Student("first2", "last2", 1)	
val student3 = new Student("first3", "last3", 2)	
val student4 = new Student("first4", "last4", 6)	
val students = List(student1, student2, 	
student3, student4)	
val (elementarySchoolers, middleSchoolers) 	
= students.partition(_.grade < 6)	
println(elementarySchoolers)	
println(middleSchoolers)	
//List(first1 last1, first2 last2, first3 last3)	
//List(first4 last4)	
Tuple
Working with collections: transforming
val student1 = new Student("first1", "last1", 1)	
val student2 = new Student("first2", "last2", 1)	
val student3 = new Student("first3", "last3", 2)	
val student4 = new Student("first4", "last4", 6)	
val students = List(student1, student2, 	
student3, student4)	
val rollCall = students.map(_.firstName)	
println(rollCall)	
// List(first1, first2, first3, first4)
Map
// student1, student2, student3, student4	
val studentSchools = Map(student1 -> "Miller", 	
student2 -> "Lawson",	
student3 -> "Lawson", 	
student4 -> "Miller")	
println(studentSchools(student1)) // Miller
More collections
•  Set
•  IndexedSeq
•  Vector (good one!)
•  Range
–  1 to 100
–  1 until 100
–  2 until 100 by 2
•  …
•  Mutable versions
•  Parallel versions
35
Putting it together: Quicksort
def quicksort[T](input: Traversable[T])	
(ordering: Ordering[T]) : Traversable[T] = 	
if (input.isEmpty) {	
input	
} else {	
val (low, high) 	
= input.tail.partition(ordering.lt(_, input.head))	
quicksort(low)(ordering) ++ List(input.head) 	
++ quicksort(high)(ordering)	
}	
println(quicksort(List(1, 3, 4, 5, 1))(Ordering.Int))
Putting it together: Quicksort
def quicksort[T](input: Traversable[T])	
(implicit ordering: Ordering[T]) 	
: Traversable[T] =	
if (input.isEmpty) {	
input	
} else {	
val (low, high) 	
= input.tail.partition(ordering.lt(_, input.head))	
quicksort(low) ++ List(input.head) ++ quicksort(high)	
}	
println(quicksort(List(1, 3, 4, 5, 1)))
Pattern matching: Basics
val a:Any = "foo"	
	
a match {	
case str: String => println("A string: " + str)	
case i: Int => println("An int: " + i)	
case _ => println("Something else")	
}
Pattern matching: with collections
val l = List("a", "b", "c")	
	
l match {	
case Nil => println("Empty!")	
case head :: Nil => 	
println("Only one item " + head)	
case head :: tail => 	
println("Item " + head + 	
" followed by " + tail)	
}
Quicksort with pattern matching
def quicksort[T](input: Traversable[T])	
(implicit ordering: Ordering[T]) : Traversable[T] =
input match {	
case head :: tail => 	
val (low, high) = tail.partition(ordering.lt(_, head))	
quicksort(low) ++ List(head) ++ quicksort(high)	
case _ => input	
}	
println(quicksort(List(1, 3, 4, 5, 1)))
Destutter using Pattern matching
41
def destutter[A](lst: List[A]): List[A] = lst match {	
case h1 :: h2 :: tail if (h1 == h2) 	
=> destutter(h2 :: tail)	
case h1 :: h2 :: tail 	
=> h1 :: destutter(h2 :: tail)	
case _ 	
=> lst	
}	
// destutter(List(1,1,1,1,1,1)) => List(1)	
// destutter(List(1,1,4,3,3,2)) => List(1,4,3,2)	
// destutter(List())=> List()
Case classes
•  Useful in pattern matching
–  “case”
•  Offer many useful common methods
–  equals()
–  hashCode()
–  toString
–  copy()
42
Case classes
43
case class Human(name: String)	
case class SuperHero(name: String, power: String)	
val characters = List(Human("Programmer"), 	
SuperHero("Customer", "money"), 	
SuperHero("QA", "testing"))
Case classes and pattern matching
44
val actions = for (character <- characters) 	
yield character match {	
case Human(name) => 	
name + " needs to be saved"	
case SuperHero(name, power) => 	
name + " will save using " + power	
}	
	
actions.foreach(println)
Pattern matching and extracting just enough
45
val actions = for (character <- characters) 	
yield character match {	
case Human(name) => 	
name + " needs to be saved"	
case SuperHero(_, power) => 	
"Could be saved using " + power	
}	
	
actions.foreach(println)	
	
// Programmer needs to be saved	
// Could be saved using money	
// Could be saved using testing
Regular expressions
46
val text = "Ramnivas Laddad" 	
	
val Name = """(w+)s+(w+)""".r	
	
val person = text match {	
case Name(first, last) => 	
Some(new Person(first, last))	
case _ => 	
None	
}	
	
println(person) // Some(Ramnivas Laddad)
Options
47
val texts = 	
List("Ramnivas Laddad", "foo", "Martin Odersky")	
	
val peopleOptions = texts.map {	
_ match {	
case Name(first, last) => 	
Some(new Person(first, last))	
case _ => None	
}	
}	
	
println(peopleOptions)	
// List(Some(Ramnivas Laddad), 	
None, 	
Some(Martin Odersky))
Options: flattening
48
val texts = 	
List("Ramnivas Laddad", "foo", "Martin Odersky")	
	
val peopleOptions = texts.map {	
_ match {	
case Name(first, last) => 	
Some(new Person(first, last))	
case _ => None	
}	
}	
	
println(peopleOptions.flatten)	
// List(Ramnivas Laddad, Martin Odersky)
Options: flatMap
49
val texts = 	
List("Ramnivas Laddad", "foo", "Martin Odersky")	
	
val people = texts.flatMap {	
_ match {	
case Name(first, last) => 	
Some(new Person(first, last))	
case _ => None	
}	
}	
	
println(people)	
// List(Ramnivas Laddad, Martin Odersky)
Higher order functions
def process() : Unit = {	
retry(5) {	
...	
}	
}
def retry[T](maxRetry: Int)(thunk: => T) = {	
def loop(thunk: => T, attempt: Int): T = {	
try {	
thunk	
} catch {	
case ex if (attempt < maxRetry) =>	
loop(thunk, attempt + 1)	
}	
}	
loop(thunk, 0)	
}
Thunk
Caching using Scala
def getQuoteGraph(stock: Stock, 	
days: Int) : Array[Byte] = {	
cached("chart", stock.ticker + ":" + days) {	
!
... Expensive calculation !
!
}	
}
Caching higher-order function
abstract class Caching(val cacheManager: CacheManager) {	
def cached[T](region: String, key: Any)	
(thunk: => T): T = {	
val cache = ...	
	
if (cache.containsKey(key)) {	
cache.get(key).asInstanceOf[T]	
} else {	
val thunkVal: T = thunk	
cache.put(key, thunkVal)	
thunkVal	
}	
}	
}
Transaction management
def findOrder(orderId: Long) : Order = {	
transactional(readOnly=true) {	
//...	
}	
}	
	
def updateOrder(order: Order) {	
transactional() {	
//...	
}	
}
53
Transaction management function
def transactional[T](propgation: Propagation = Propagation.REQUIRED,	
isolation: Isolation = Isolation.DEFAULT,	
readOnly: Boolean = false,	
timeout: Int =TransactionDefinition.TIMEOUT_DEFAULT,	
rollbackFor: List[Throwable] = List(),	
noRollbackFor: List[Throwable] = List())	
(thunk: => T) : T
Transaction management implementation
abstract class TransactionManagement(val txManager: PlatformTransactionManager) {	
	
def transactional[T](...)(thunk: => T) : T = {	
val txAttribute = new TransactionAttributeWithRollbackRules(...)	
	
val status = txManager.getTransaction(txAttribute)	
	
try {	
val ret = thunk	
txManager.commit(status)	
ret	
} catch {	
case ex => {	
if (txAttribute.rollbackOn(ex)) {	
txManager.rollback(status)	
} else {	
txManager.commit(status)	
}	
throw ex	
}	
}	
}	
}
There is more… a lot more
•  Methods/functions
–  Default parameters
–  Named parameters
–  Curried parameters
–  Partial, partially-applied
functions
•  Type system
–  Higher-kinded types
–  Bounded types
–  Implicit type conversion
–  Type parameter
evidence
–  Type aliasing
•  Lazy values
•  Partial imports
•  Actors
•  Extractors
•  Scala ecosystem
•  Combinator/parser
•  Continuations
•  Compiler plugin
•  …
56
Learning Scala
•  Read a Scala book
–  Get the whole picture
•  May feel complex at first
–  Java-style Scala may serve best during initial exploration
–  Over time you will appreciate its simplicity
–  Will affect your non-Scala programming deeply
57
Be ready to be humbled
Be ready to have fun!
Scala for Java Developers
Ramnivas Laddad
@ramnivas

Contenu connexe

Tendances

Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Derek Chen-Becker
 
Programming picaresque
Programming picaresqueProgramming picaresque
Programming picaresqueBret McGuire
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldBTI360
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaDerek Chen-Becker
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksSeniorDevOnly
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvmIsaias Barroso
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersMiles Sabin
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaOstap Andrusiv
 
Native XML processing in C++ (BoostCon'11)
Native XML processing in C++ (BoostCon'11)Native XML processing in C++ (BoostCon'11)
Native XML processing in C++ (BoostCon'11)Sumant Tambe
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scalascalaconfjp
 
Scala-对Java的修正和超越
Scala-对Java的修正和超越Scala-对Java的修正和超越
Scala-对Java的修正和超越Caoyuan Deng
 
Scala Quick Introduction
Scala Quick IntroductionScala Quick Introduction
Scala Quick IntroductionDamian Jureczko
 

Tendances (19)

Workshop Scala
Workshop ScalaWorkshop Scala
Workshop Scala
 
scala
scalascala
scala
 
Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010
 
Scala. Inception.
Scala. Inception.Scala. Inception.
Scala. Inception.
 
Programming picaresque
Programming picaresqueProgramming picaresque
Programming picaresque
 
Scala
ScalaScala
Scala
 
Scala on Android
Scala on AndroidScala on Android
Scala on Android
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to Scala
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java Developers
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with Scala
 
Native XML processing in C++ (BoostCon'11)
Native XML processing in C++ (BoostCon'11)Native XML processing in C++ (BoostCon'11)
Native XML processing in C++ (BoostCon'11)
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
 
Scala-对Java的修正和超越
Scala-对Java的修正和超越Scala-对Java的修正和超越
Scala-对Java的修正和超越
 
Scala Quick Introduction
Scala Quick IntroductionScala Quick Introduction
Scala Quick Introduction
 

En vedette

Functional Scala II (in practice)
Functional Scala II (in practice)Functional Scala II (in practice)
Functional Scala II (in practice)Mario Gleichmann
 
Scala 2.10.0 (english version)
Scala 2.10.0 (english version)Scala 2.10.0 (english version)
Scala 2.10.0 (english version)Daniel Sobral
 
Scala Workshop
Scala WorkshopScala Workshop
Scala WorkshopClueda AG
 
Scala For Java Programmers
Scala For Java ProgrammersScala For Java Programmers
Scala For Java ProgrammersEnno Runne
 
Sqoop on Spark for Data Ingestion-(Veena Basavaraj and Vinoth Chandar, Uber)
Sqoop on Spark for Data Ingestion-(Veena Basavaraj and Vinoth Chandar, Uber)Sqoop on Spark for Data Ingestion-(Veena Basavaraj and Vinoth Chandar, Uber)
Sqoop on Spark for Data Ingestion-(Veena Basavaraj and Vinoth Chandar, Uber)Spark Summit
 
Scala dreaded underscore
Scala dreaded underscoreScala dreaded underscore
Scala dreaded underscoreRUDDER
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
The Evolution of Scala
The Evolution of ScalaThe Evolution of Scala
The Evolution of ScalaMartin Odersky
 
Scala Days San Francisco
Scala Days San FranciscoScala Days San Francisco
Scala Days San FranciscoMartin Odersky
 
A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to ScalaTim Underwood
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java DevelopersMichael Galpin
 
Lambda Architecture with Spark, Spark Streaming, Kafka, Cassandra, Akka and S...
Lambda Architecture with Spark, Spark Streaming, Kafka, Cassandra, Akka and S...Lambda Architecture with Spark, Spark Streaming, Kafka, Cassandra, Akka and S...
Lambda Architecture with Spark, Spark Streaming, Kafka, Cassandra, Akka and S...Helena Edelson
 
Lambda Architecture with Spark Streaming, Kafka, Cassandra, Akka, Scala
Lambda Architecture with Spark Streaming, Kafka, Cassandra, Akka, ScalaLambda Architecture with Spark Streaming, Kafka, Cassandra, Akka, Scala
Lambda Architecture with Spark Streaming, Kafka, Cassandra, Akka, ScalaHelena Edelson
 
Scala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on HerokuScala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on HerokuHavoc Pennington
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 

En vedette (18)

Functional Scala II (in practice)
Functional Scala II (in practice)Functional Scala II (in practice)
Functional Scala II (in practice)
 
Functional Scala I
Functional Scala IFunctional Scala I
Functional Scala I
 
Scala 2.10.0 (english version)
Scala 2.10.0 (english version)Scala 2.10.0 (english version)
Scala 2.10.0 (english version)
 
Scala Workshop
Scala WorkshopScala Workshop
Scala Workshop
 
Scala For Java Programmers
Scala For Java ProgrammersScala For Java Programmers
Scala For Java Programmers
 
Sqoop on Spark for Data Ingestion-(Veena Basavaraj and Vinoth Chandar, Uber)
Sqoop on Spark for Data Ingestion-(Veena Basavaraj and Vinoth Chandar, Uber)Sqoop on Spark for Data Ingestion-(Veena Basavaraj and Vinoth Chandar, Uber)
Sqoop on Spark for Data Ingestion-(Veena Basavaraj and Vinoth Chandar, Uber)
 
Scala dreaded underscore
Scala dreaded underscoreScala dreaded underscore
Scala dreaded underscore
 
Scala vs Ruby
Scala vs RubyScala vs Ruby
Scala vs Ruby
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
The Evolution of Scala
The Evolution of ScalaThe Evolution of Scala
The Evolution of Scala
 
Scala Days San Francisco
Scala Days San FranciscoScala Days San Francisco
Scala Days San Francisco
 
A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to Scala
 
Scala Days NYC 2016
Scala Days NYC 2016Scala Days NYC 2016
Scala Days NYC 2016
 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java Developers
 
Lambda Architecture with Spark, Spark Streaming, Kafka, Cassandra, Akka and S...
Lambda Architecture with Spark, Spark Streaming, Kafka, Cassandra, Akka and S...Lambda Architecture with Spark, Spark Streaming, Kafka, Cassandra, Akka and S...
Lambda Architecture with Spark, Spark Streaming, Kafka, Cassandra, Akka and S...
 
Lambda Architecture with Spark Streaming, Kafka, Cassandra, Akka, Scala
Lambda Architecture with Spark Streaming, Kafka, Cassandra, Akka, ScalaLambda Architecture with Spark Streaming, Kafka, Cassandra, Akka, Scala
Lambda Architecture with Spark Streaming, Kafka, Cassandra, Akka, Scala
 
Scala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on HerokuScala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on Heroku
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 

Similaire à Scala for Java Developers (Silicon Valley Code Camp 13)

Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java DevelopersRamnivasLaddad
 
1.1 motivation
1.1 motivation1.1 motivation
1.1 motivationwpgreenway
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersMatthew Farwell
 
Intro to scala
Intro to scalaIntro to scala
Intro to scalaJoe Zulli
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basicswpgreenway
 
CGIS_IP_T6_AccessCtrl_ClassScope_Packages_API.pdf
CGIS_IP_T6_AccessCtrl_ClassScope_Packages_API.pdfCGIS_IP_T6_AccessCtrl_ClassScope_Packages_API.pdf
CGIS_IP_T6_AccessCtrl_ClassScope_Packages_API.pdfRosaGomezCano
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersMiles Sabin
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
2.1 recap from-day_one
2.1 recap from-day_one2.1 recap from-day_one
2.1 recap from-day_onefuturespective
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java ProgrammersEric Pederson
 
FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3Toni Kolev
 
Scala: Devnology - Learn A Language Scala
Scala: Devnology - Learn A Language ScalaScala: Devnology - Learn A Language Scala
Scala: Devnology - Learn A Language ScalaJan Willem Tulp
 

Similaire à Scala for Java Developers (Silicon Valley Code Camp 13) (20)

Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java Developers
 
1.1 motivation
1.1 motivation1.1 motivation
1.1 motivation
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developersSoftshake 2013: 10 reasons why java developers are jealous of Scala developers
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
1.2 scala basics
1.2 scala basics1.2 scala basics
1.2 scala basics
 
CGIS_IP_T6_AccessCtrl_ClassScope_Packages_API.pdf
CGIS_IP_T6_AccessCtrl_ClassScope_Packages_API.pdfCGIS_IP_T6_AccessCtrl_ClassScope_Packages_API.pdf
CGIS_IP_T6_AccessCtrl_ClassScope_Packages_API.pdf
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
2.1 recap from-day_one
2.1 recap from-day_one2.1 recap from-day_one
2.1 recap from-day_one
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
1.1 motivation
1.1 motivation1.1 motivation
1.1 motivation
 
1.1 motivation
1.1 motivation1.1 motivation
1.1 motivation
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 
FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3
 
Scala: Devnology - Learn A Language Scala
Scala: Devnology - Learn A Language ScalaScala: Devnology - Learn A Language Scala
Scala: Devnology - Learn A Language Scala
 

Dernier

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
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
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
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
 
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
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
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
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
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
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 

Dernier (20)

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
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
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
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
 
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
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
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
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 

Scala for Java Developers (Silicon Valley Code Camp 13)

  • 1. Scala for Java Developers Ramnivas Laddad @ramnivas
  • 2. @ramnivas •  Author of books and articles –  AspectJ in Action (1st and 2nd edition) •  Spring framework, Cloud Foundry •  Main interests –  Cloud computing –  Aspect-oriented programming –  Scala and functional programming •  Speaker at many professional conferences –  JavaOne, JavaPolis, SpringOne, Software Development, No Fluff Just Stuff, EclipseCon, O’Reilly OSCON etc. •  Active involvement in AspectJ, Spring, and Cloud Foundry since their early form
  • 3. What is Scala 3 “a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive.” http://www.scala-lang.org
  • 4. Object-oriented •  Everything is an object –  No “primitives” •  Classes –  Same as Java, but concise •  Traits –  Interfaces done right •  Singletons –  Language-level concept 4
  • 5. Statically typed •  Rich type system (by Java’s standard) –  Higher-kinded types –  Implicit conversions –  Type evidence •  Expressive type system •  Inferred types 5
  • 6. Functional Programming •  Functions as values –  May be •  Saved •  Passed to other functions (higher-order functions)  No need to write ugly anonymous classes –  Advanced pattern matching –  Expressions return a value •  if/else, try/catch, match, … •  Promotes immutability –  But doesn’t force it 6
  • 7. Java Interoperability •  Compiles to Java byte code –  Jars, wars, … –  No special operational change •  Scala calling Java, Java calling Scala code is fine •  Whole Java eco-system at your service •  You can write apps using Scala and Spring, today 7
  • 8. Hello World: Scripting Style $ scala hello-script.scala Hello World println("Hello World") No compilation
  • 9. Hello World: Porting of Java Code $ scalac hello-java.scala $ scala example.Main Hello World // hello-java.scala package example object Main { def main(args: Array[String]) { println("Hello World") } } ‘static’ Inferred semicolons
  • 10. Hello World: Using the App trait $ scalac hello-app.scala $ scala example.Main Hello World // hello-app.scala package example object Main extends App { println("Hello World") }
  • 11. Simple Class class Person val p = new Person Type Inferred Default access: public No curly braces needed (but allowed)
  • 12. Simple Class class Person val p: Person = new Person Explicit type specification
  • 13. Class with constructor class Person(firstName: String, lastName: String) val p = new Person("Ramnivas", "Laddad") println(p.firstName) // Error Primary constructor Fields – accessible in class body
  • 14. Class with “getters” class Person(val firstName: String, val lastName: String) val p = new Person("Ramnivas", "Laddad") println(p.firstName) Value (Java ‘final’)
  • 15. Class with “getters” and “setters” class Person(var firstName: String, var lastName: String) val p = new Person("Ramnivas", "Laddad") println(p.firstName) p.firstName = "Ramnivas2” Variable (Java non-final)
  • 16. Extending a class class Student(firstName: String, lastName: String, val grade: Int) extends Person(firstName, lastName) val s = new Student("Ramnivas", "Laddad", 1) println(s.firstName) println(s.grade)
  • 17. Defining methods class Person(val firstName: String, val lastName: String) { def name = firstName + " " + lastName override def toString = name } val p = new Person("Ramnivas", "Laddad") println(p.name) // Ramnivas Laddad println(p) // Ramnivas Laddad Not optional
  • 18. Uniform access principle class Person(val firstName: String, val lastName: String) { val name = firstName + " " + lastName override def toString = name } val p = new Person("Ramnivas", "Laddad") println(p.name) // Ramnivas Laddad println(p) // Ramnivas Laddad
  • 19. Names in Scala •  Class, method, field names can contain non alpha- numeric characters –  :: –  ::: –  ~> –  f@#: •  Valuable if used judiciously –  DSLs 19
  • 20. More about methods and fields •  Declaring abstract methods and fields –  Just don’t provide definition def learn(subject: String) val knowledge •  Classes with abstract method must be declared abstract –  Just as in Java •  Methods can be defined inside methods •  Methods may be marked @tailrec to check for tail- recursiveness 20
  • 21. Finer access control levels •  Default access level: public •  Protected: protected –  Same as Java •  Private: –  private –  private[this] Access only from this instance –  private[package-name] Access from package and its subpackages 21
  • 22. Traits: Interfaces done right 22 trait PartyGoer { val age: Int val yearsUntilLegalDrinking = if (age >= 21) 0 else 21-age } class Student(firstName: String, lastName: String, val age: Int, val grade: Int) extends Person(firstName, lastName) with PartyGoer val s = new Student("a", "b", 17, 12) s.yearsUntilLegalDrinking // 4
  • 23. Collections val people = List("John", "Jacob", "Mike") val firstPerson = people(0) println(firstPerson) // John
  • 24. Collections val people = Array("John", "Jacob", "Mike") val firstPerson = people(0) println(firstPerson) // John
  • 25. Working with collections: for comprehension 25 for (person <- people) { println(person) }
  • 26. Working with collections: for comprehension 26 for (person <- people if person startsWith "J") { println("""Lucky one to start name in "J" """ + person) } // You are lucky one to start name in "J" John // You are lucky one to start name in "J" Jacob
  • 27. Working with collections: for comprehension 27 for (person <- people if person startsWith "J") { println(s"""Lucky one to start name in "J" $person""") } // You are lucky one to start name in "J" John // You are lucky one to start name in "J" Jacob
  • 28. Working with collections: filter val student1 = new Student("first1", "last1", 1) val student2 = new Student("first2", "last2", 1) val student3 = new Student("first3", "last3", 2) val student4 = new Student("first4", "last4", 6) val students = List(student1, student2, student3, student4) val firstGraders = students.filter( s => s.grade == 1) println(firstGraders) // List(first1 last1, first2 last2)
  • 29. Working with collections: filter val student1 = new Student("first1", "last1", 1) val student2 = new Student("first2", "last2", 1) val student3 = new Student("first3", "last3", 2) val student4 = new Student("first4", "last4", 6) val students = List(student1, student2, student3, student4) val inFirstGrade: Student => Boolean = s => s.grade == 1 val firstGraders = students.filter(inFirstGrade) // List(first1 last1, first2 last2)
  • 30. Working with collections: using _ val student1 = new Student("first1", "last1", 1) val student2 = new Student("first2", "last2", 1) val student3 = new Student("first3", "last3", 2) val student4 = new Student("first4", "last4", 6) val students = List(student1, student2, student3, student4) val firstGraders = students.filter(_.grade == 1) println(firstGraders) // List(first1 last1, first2 last2)
  • 31. Working with collections: passing method as function val student1 = new Student("first1", "last1", 1) val student2 = new Student("first2", "last2", 1) val student3 = new Student("first3", "last3", 2) val student4 = new Student("first4", "last4", 6) val students = List(student1, student2, student3, student4) def inFirstGrade(s: Student) : Boolean = s.grade == 1 val firstGraders = students.filter(inFirstGrade) // List(first1 last1, first2 last2)
  • 32. Working with collections: partition val student1 = new Student("first1", "last1", 1) val student2 = new Student("first2", "last2", 1) val student3 = new Student("first3", "last3", 2) val student4 = new Student("first4", "last4", 6) val students = List(student1, student2, student3, student4) val (elementarySchoolers, middleSchoolers) = students.partition(_.grade < 6) println(elementarySchoolers) println(middleSchoolers) //List(first1 last1, first2 last2, first3 last3) //List(first4 last4) Tuple
  • 33. Working with collections: transforming val student1 = new Student("first1", "last1", 1) val student2 = new Student("first2", "last2", 1) val student3 = new Student("first3", "last3", 2) val student4 = new Student("first4", "last4", 6) val students = List(student1, student2, student3, student4) val rollCall = students.map(_.firstName) println(rollCall) // List(first1, first2, first3, first4)
  • 34. Map // student1, student2, student3, student4 val studentSchools = Map(student1 -> "Miller", student2 -> "Lawson", student3 -> "Lawson", student4 -> "Miller") println(studentSchools(student1)) // Miller
  • 35. More collections •  Set •  IndexedSeq •  Vector (good one!) •  Range –  1 to 100 –  1 until 100 –  2 until 100 by 2 •  … •  Mutable versions •  Parallel versions 35
  • 36. Putting it together: Quicksort def quicksort[T](input: Traversable[T]) (ordering: Ordering[T]) : Traversable[T] = if (input.isEmpty) { input } else { val (low, high) = input.tail.partition(ordering.lt(_, input.head)) quicksort(low)(ordering) ++ List(input.head) ++ quicksort(high)(ordering) } println(quicksort(List(1, 3, 4, 5, 1))(Ordering.Int))
  • 37. Putting it together: Quicksort def quicksort[T](input: Traversable[T]) (implicit ordering: Ordering[T]) : Traversable[T] = if (input.isEmpty) { input } else { val (low, high) = input.tail.partition(ordering.lt(_, input.head)) quicksort(low) ++ List(input.head) ++ quicksort(high) } println(quicksort(List(1, 3, 4, 5, 1)))
  • 38. Pattern matching: Basics val a:Any = "foo" a match { case str: String => println("A string: " + str) case i: Int => println("An int: " + i) case _ => println("Something else") }
  • 39. Pattern matching: with collections val l = List("a", "b", "c") l match { case Nil => println("Empty!") case head :: Nil => println("Only one item " + head) case head :: tail => println("Item " + head + " followed by " + tail) }
  • 40. Quicksort with pattern matching def quicksort[T](input: Traversable[T]) (implicit ordering: Ordering[T]) : Traversable[T] = input match { case head :: tail => val (low, high) = tail.partition(ordering.lt(_, head)) quicksort(low) ++ List(head) ++ quicksort(high) case _ => input } println(quicksort(List(1, 3, 4, 5, 1)))
  • 41. Destutter using Pattern matching 41 def destutter[A](lst: List[A]): List[A] = lst match { case h1 :: h2 :: tail if (h1 == h2) => destutter(h2 :: tail) case h1 :: h2 :: tail => h1 :: destutter(h2 :: tail) case _ => lst } // destutter(List(1,1,1,1,1,1)) => List(1) // destutter(List(1,1,4,3,3,2)) => List(1,4,3,2) // destutter(List())=> List()
  • 42. Case classes •  Useful in pattern matching –  “case” •  Offer many useful common methods –  equals() –  hashCode() –  toString –  copy() 42
  • 43. Case classes 43 case class Human(name: String) case class SuperHero(name: String, power: String) val characters = List(Human("Programmer"), SuperHero("Customer", "money"), SuperHero("QA", "testing"))
  • 44. Case classes and pattern matching 44 val actions = for (character <- characters) yield character match { case Human(name) => name + " needs to be saved" case SuperHero(name, power) => name + " will save using " + power } actions.foreach(println)
  • 45. Pattern matching and extracting just enough 45 val actions = for (character <- characters) yield character match { case Human(name) => name + " needs to be saved" case SuperHero(_, power) => "Could be saved using " + power } actions.foreach(println) // Programmer needs to be saved // Could be saved using money // Could be saved using testing
  • 46. Regular expressions 46 val text = "Ramnivas Laddad" val Name = """(w+)s+(w+)""".r val person = text match { case Name(first, last) => Some(new Person(first, last)) case _ => None } println(person) // Some(Ramnivas Laddad)
  • 47. Options 47 val texts = List("Ramnivas Laddad", "foo", "Martin Odersky") val peopleOptions = texts.map { _ match { case Name(first, last) => Some(new Person(first, last)) case _ => None } } println(peopleOptions) // List(Some(Ramnivas Laddad), None, Some(Martin Odersky))
  • 48. Options: flattening 48 val texts = List("Ramnivas Laddad", "foo", "Martin Odersky") val peopleOptions = texts.map { _ match { case Name(first, last) => Some(new Person(first, last)) case _ => None } } println(peopleOptions.flatten) // List(Ramnivas Laddad, Martin Odersky)
  • 49. Options: flatMap 49 val texts = List("Ramnivas Laddad", "foo", "Martin Odersky") val people = texts.flatMap { _ match { case Name(first, last) => Some(new Person(first, last)) case _ => None } } println(people) // List(Ramnivas Laddad, Martin Odersky)
  • 50. Higher order functions def process() : Unit = { retry(5) { ... } } def retry[T](maxRetry: Int)(thunk: => T) = { def loop(thunk: => T, attempt: Int): T = { try { thunk } catch { case ex if (attempt < maxRetry) => loop(thunk, attempt + 1) } } loop(thunk, 0) } Thunk
  • 51. Caching using Scala def getQuoteGraph(stock: Stock, days: Int) : Array[Byte] = { cached("chart", stock.ticker + ":" + days) { ! ... Expensive calculation ! ! } }
  • 52. Caching higher-order function abstract class Caching(val cacheManager: CacheManager) { def cached[T](region: String, key: Any) (thunk: => T): T = { val cache = ... if (cache.containsKey(key)) { cache.get(key).asInstanceOf[T] } else { val thunkVal: T = thunk cache.put(key, thunkVal) thunkVal } } }
  • 53. Transaction management def findOrder(orderId: Long) : Order = { transactional(readOnly=true) { //... } } def updateOrder(order: Order) { transactional() { //... } } 53
  • 54. Transaction management function def transactional[T](propgation: Propagation = Propagation.REQUIRED, isolation: Isolation = Isolation.DEFAULT, readOnly: Boolean = false, timeout: Int =TransactionDefinition.TIMEOUT_DEFAULT, rollbackFor: List[Throwable] = List(), noRollbackFor: List[Throwable] = List()) (thunk: => T) : T
  • 55. Transaction management implementation abstract class TransactionManagement(val txManager: PlatformTransactionManager) { def transactional[T](...)(thunk: => T) : T = { val txAttribute = new TransactionAttributeWithRollbackRules(...) val status = txManager.getTransaction(txAttribute) try { val ret = thunk txManager.commit(status) ret } catch { case ex => { if (txAttribute.rollbackOn(ex)) { txManager.rollback(status) } else { txManager.commit(status) } throw ex } } } }
  • 56. There is more… a lot more •  Methods/functions –  Default parameters –  Named parameters –  Curried parameters –  Partial, partially-applied functions •  Type system –  Higher-kinded types –  Bounded types –  Implicit type conversion –  Type parameter evidence –  Type aliasing •  Lazy values •  Partial imports •  Actors •  Extractors •  Scala ecosystem •  Combinator/parser •  Continuations •  Compiler plugin •  … 56
  • 57. Learning Scala •  Read a Scala book –  Get the whole picture •  May feel complex at first –  Java-style Scala may serve best during initial exploration –  Over time you will appreciate its simplicity –  Will affect your non-Scala programming deeply 57 Be ready to be humbled Be ready to have fun!
  • 58. Scala for Java Developers Ramnivas Laddad @ramnivas