SlideShare a Scribd company logo
1 of 56
Scala for Java Developers Ā© 2011 SpringOne 2GX. All rights reserved. Do not distribute without permission.
What is Scala ā€œ 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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Statically typed ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Functional Programming ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Java Interoperability ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Hello World: Scripting Style $ scala hello-script.scala Hello World No compilation
Hello World: Porting of Java Code $ scalac hello-java.scala $ scala example.Main Hello World ā€˜ staticā€™ Inferred semicolons
Hello World: Using the App trait $ scalac hello-app.scala $ scala example.Main 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 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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
More about methods and fields ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Finer access control levels ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Traits: Interfaces done right trait  PartyGoer { val  age: Int val  yearsUntilLegalDrinking =  if  (age >= 18) 0  else  18-age }
Collections val  people = List( "John" ,  "Jacob" ,  "Mike" )
Collections val  people = Array( "John" ,  "Jacob" ,  "Mike" )
Working with collections: for comprehension for  (person <- people) { println(person) }
Working with collections: for comprehension for  (person <- people  if  person startsWith  &quot;J&quot; ) { println( &quot;&quot;&quot;Lucky one to start name in &quot;J&quot; &quot;&quot;&quot;  + person) } // You are lucky one to start name in &quot;J&quot; John // You are lucky one to start name in &quot;J&quot;  Jacob
Working with collections: filter val  student1 =  new  Student( &quot;first1&quot; ,  &quot;last1&quot; , 1) val  student2 =  new  Student( &quot;first2&quot; ,  &quot;last2&quot; , 1) val  student3 =  new  Student( &quot;first3&quot; ,  &quot;last3&quot; , 2) val  student4 =  new  Student( &quot;first4&quot; ,  &quot;last4&quot; , 6) val  students = List(student1, student2,  student3, student4)
Working with collections: filter val  student1 =  new  Student( &quot;first1&quot; ,  &quot;last1&quot; , 1) val  student2 =  new  Student( &quot;first2&quot; ,  &quot;last2&quot; , 1) val  student3 =  new  Student( &quot;first3&quot; ,  &quot;last3&quot; , 2) val  student4 =  new  Student( &quot;first4&quot; ,  &quot;last4&quot; , 6) val  students = List(student1, student2,  student3, student4)
Working with collections: using _ val  student1 =  new  Student( &quot;first1&quot; ,  &quot;last1&quot; , 1) val  student2 =  new  Student( &quot;first2&quot; ,  &quot;last2&quot; , 1) val  student3 =  new  Student( &quot;first3&quot; ,  &quot;last3&quot; , 2) val  student4 =  new  Student( &quot;first4&quot; ,  &quot;last4&quot; , 6) val  students = List(student1, student2,  student3, student4)
Working with collections: passing method as function val  student1 =  new  Student( &quot;first1&quot; ,  &quot;last1&quot; , 1) val  student2 =  new  Student( &quot;first2&quot; ,  &quot;last2&quot; , 1) val  student3 =  new  Student( &quot;first3&quot; ,  &quot;last3&quot; , 2) val  student4 =  new  Student( &quot;first4&quot; ,  &quot;last4&quot; , 6) val  students = List(student1, student2,  student3, student4)
Working with collections: partition val  student1 =  new  Student( &quot;first1&quot; ,  &quot;last1&quot; , 1) val  student2 =  new  Student( &quot;first2&quot; ,  &quot;last2&quot; , 1) val  student3 =  new  Student( &quot;first3&quot; ,  &quot;last3&quot; , 2) val  student4 =  new  Student( &quot;first4&quot; ,  &quot;last4&quot; , 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( &quot;first1&quot; ,  &quot;last1&quot; , 1) val  student2 =  new  Student( &quot;first2&quot; ,  &quot;last2&quot; , 1) val  student3 =  new  Student( &quot;first3&quot; ,  &quot;last3&quot; , 2) val  student4 =  new  Student( &quot;first4&quot; ,  &quot;last4&quot; , 6) val  students = List(student1, student2,  student3, student4)
Map println(studentSchools(student1))  // Miller
More collections ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 println(quicksort(List(1, 3, 4, 5, 1)))
Pattern matching: Basics val  a:Any =  &quot;foo&quot; a  match  { case  str: String => println( &quot;A string: &quot;  + str) case  i: Int => println( &quot;An int: &quot;  + i) case  _ => println( &quot;Something else&quot; ) }
Pattern matching: with collections val  l = List( &quot;a&quot; ,  &quot;b&quot; ,  &quot;c&quot; ) l  match  { case  Nil => println( &quot;Empty!&quot; ) case  head :: Nil =>  println( &quot;Only one item &quot;  + head) case  head :: tail =>  println( &quot;Item &quot;  + head +  &quot; followed by &quot;  + 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 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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Case classes case   class  Human(name: String) case   class  SuperHero(name: String, power: String) val  characters = List(Human( &quot;Programmer&quot; ),  SuperHero( &quot;Customer&quot; ,  &quot;money&quot; ),  SuperHero( &quot;QA&quot; ,  &quot;testing&quot; ))
Case classes and pattern matching val  actions =  for  (character <- characters)  yield  character  match  { case  Human(name) =>  name +  &quot; needs to be saved&quot; case  SuperHero(name, power) =>  name +  &quot; will save using &quot;  + power } actions.foreach(println)
Pattern matching and extracting just enough val  actions =  for  (character <- characters)  yield  character  match  { case  Human(name) =>  name +  &quot; needs to be saved&quot; case  SuperHero(_, power) =>  &quot;Could be saved using &quot;  + power } actions.foreach(println) // Programmer needs to be saved // Could be saved using money // Could be saved using testing
Regular expressions val  text =  &quot;Ramnivas Laddad&quot;   val  Name =  &quot;&quot;&quot;(+)+(+)&quot;&quot;&quot; .r val  person = text  match  { case  Name(first, last) =>  Some( new  Person(first, last)) case  _ =>  None } println(person)  // Some(Ramnivas Laddad)
Options val  texts =  List( &quot;Ramnivas Laddad&quot; ,  &quot;foo&quot; ,  &quot;Scott Andrews&quot; ) val  peopleOptions = texts.map { _  match  { case  Name(first, last) =>  Some( new  Person(first, last)) case  _ => None } } println(peopleOptions) // List(Some(Ramnivas Laddad),  None,  Some(Scott Andrews))
Options: flattening val  texts =  List( &quot;Ramnivas Laddad&quot; ,  &quot;foo&quot; ,  &quot;Scott Andrews&quot; ) val  peopleOptions = texts.map { _  match  { case  Name(first, last) =>  Some( new  Person(first, last)) case  _ => None } } println(peopleOptions.flatten) // List(Ramnivas Laddad, Scott Andrews)
Options: flatMap val  texts =  List( &quot;Ramnivas Laddad&quot; ,  &quot;foo&quot; ,  &quot;Scott Andrews&quot; ) val  people = texts.flatMap { _  match  { case  Name(first, last) =>  Some( new  Person(first, last)) case  _ => None } } println(people) // List(Ramnivas Laddad, Scott Andrews)
Higher order functions def  process() : Unit = { retry(5)  { ... } } def  retry[T](maxRetry: Int)(thunk: => T) = { def  retry(thunk: => T, attempt: Int): T = { try  { thunk }  catch  { case  ex  if  (attempt < maxRetry) => retry(thunk, attempt + 1) } } retry(thunk, 0) } Thunk
Caching using Scala  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Caching higher-order function ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Transaction management ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Transaction management function ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Transaction management implementation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
There is moreā€¦ a lot more ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Learning Scala ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Be ready to be humbled
Scala for Java Developers Ā© 2011 SpringOne 2GX. All rights reserved. Do not distribute without permission.

More Related Content

What's hot

Scripting3
Scripting3Scripting3
Scripting3Nao Dara
Ā 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regexwayn
Ā 
From android/java to swift (1)
From android/java to swift (1)From android/java to swift (1)
From android/java to swift (1)allanh0526
Ā 
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
Ā 
Introduction to Swift 2
Introduction to Swift 2Introduction to Swift 2
Introduction to Swift 2Joris Timmerman
Ā 
Regular Expression
Regular ExpressionRegular Expression
Regular ExpressionBharat17485
Ā 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular ExpressionMasudul Haque
Ā 
42.type: Literal-based Singleton types
42.type: Literal-based Singleton types42.type: Literal-based Singleton types
42.type: Literal-based Singleton typesGeorge Leontiev
Ā 
Marcā€™s (bio)perl course
Marcā€™s (bio)perl courseMarcā€™s (bio)perl course
Marcā€™s (bio)perl courseMarc Logghe
Ā 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and StatementsWebStackAcademy
Ā 
Php basics
Php basicsPhp basics
Php basicshamfu
Ā 
Functional programming with F#
Functional programming with F#Functional programming with F#
Functional programming with F#Remik Koczapski
Ā 
Types and perl language
Types and perl languageTypes and perl language
Types and perl languageMasahiro Honma
Ā 
Scala Demystifying the Funky Stuff
Scala Demystifying the Funky StuffScala Demystifying the Funky Stuff
Scala Demystifying the Funky StuffDan Hinojosa
Ā 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To ScalaPeter Maas
Ā 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and PatternsHenry Osborne
Ā 
06. haskell type builder
06. haskell type builder06. haskell type builder
06. haskell type builderSebastian Rettig
Ā 
String variable in php
String variable in phpString variable in php
String variable in phpchantholnet
Ā 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenitĆ  (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenitĆ  (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenitĆ  (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenitĆ  (BMR G...Andrea Telatin
Ā 

What's hot (20)

Scripting3
Scripting3Scripting3
Scripting3
Ā 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
Ā 
Intermediate JavaScript
Intermediate JavaScriptIntermediate JavaScript
Intermediate JavaScript
Ā 
From android/java to swift (1)
From android/java to swift (1)From android/java to swift (1)
From android/java to swift (1)
Ā 
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
Ā 
Introduction to Swift 2
Introduction to Swift 2Introduction to Swift 2
Introduction to Swift 2
Ā 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
Ā 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
Ā 
42.type: Literal-based Singleton types
42.type: Literal-based Singleton types42.type: Literal-based Singleton types
42.type: Literal-based Singleton types
Ā 
Marcā€™s (bio)perl course
Marcā€™s (bio)perl courseMarcā€™s (bio)perl course
Marcā€™s (bio)perl course
Ā 
JavaScript - Chapter 4 - Types and Statements
 JavaScript - Chapter 4 - Types and Statements JavaScript - Chapter 4 - Types and Statements
JavaScript - Chapter 4 - Types and Statements
Ā 
Php basics
Php basicsPhp basics
Php basics
Ā 
Functional programming with F#
Functional programming with F#Functional programming with F#
Functional programming with F#
Ā 
Types and perl language
Types and perl languageTypes and perl language
Types and perl language
Ā 
Scala Demystifying the Funky Stuff
Scala Demystifying the Funky StuffScala Demystifying the Funky Stuff
Scala Demystifying the Funky Stuff
Ā 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To Scala
Ā 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and Patterns
Ā 
06. haskell type builder
06. haskell type builder06. haskell type builder
06. haskell type builder
Ā 
String variable in php
String variable in phpString variable in php
String variable in php
Ā 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenitĆ  (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenitĆ  (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenitĆ  (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenitĆ  (BMR G...
Ā 

Similar to Scala for Java Developers

Practically Functional
Practically FunctionalPractically Functional
Practically Functionaldjspiewak
Ā 
Scala for Java Developers (Silicon Valley Code Camp 13)
Scala for Java Developers (Silicon Valley Code Camp 13)Scala for Java Developers (Silicon Valley Code Camp 13)
Scala for Java Developers (Silicon Valley Code Camp 13)Ramnivas Laddad
Ā 
Why Scala?
Why Scala?Why Scala?
Why Scala?Mike Fogus
Ā 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4Emil Vladev
Ā 
NaĆÆvetĆ© vs. Experience
NaĆÆvetĆ© vs. ExperienceNaĆÆvetĆ© vs. Experience
NaĆÆvetĆ© vs. ExperienceMike Fogus
Ā 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In ScalaSkills Matter
Ā 
Intro toswift1
Intro toswift1Intro toswift1
Intro toswift1Jordan Morgan
Ā 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009David Pollak
Ā 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java ProgrammersMike Bowler
Ā 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java DevelopersMichael Galpin
Ā 
Python - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave ParkPython - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave Parkpointstechgeeks
Ā 
Perl Presentation
Perl PresentationPerl Presentation
Perl PresentationSopan Shewale
Ā 
Processing OWL2 Ontologies using Thea: An application of Logic Programming
Processing OWL2 Ontologies using Thea: An application of Logic ProgrammingProcessing OWL2 Ontologies using Thea: An application of Logic Programming
Processing OWL2 Ontologies using Thea: An application of Logic ProgrammingVangelis Vassiliadis
Ā 
Thea: Processing OWL Ontologies - An application of logic programming
Thea: Processing OWL Ontologies - An application of logic programmingThea: Processing OWL Ontologies - An application of logic programming
Thea: Processing OWL Ontologies - An application of logic programmingguest57f623bf
Ā 
1.2 Scala Basics
1.2 Scala Basics1.2 Scala Basics
1.2 Scala Basicsretronym
Ā 
Scala introduction
Scala introductionScala introduction
Scala introductionYardena Meymann
Ā 

Similar to Scala for Java Developers (20)

Scala introduction
Scala introductionScala introduction
Scala introduction
Ā 
Practically Functional
Practically FunctionalPractically Functional
Practically Functional
Ā 
Scala for Java Developers (Silicon Valley Code Camp 13)
Scala for Java Developers (Silicon Valley Code Camp 13)Scala for Java Developers (Silicon Valley Code Camp 13)
Scala for Java Developers (Silicon Valley Code Camp 13)
Ā 
Why Scala?
Why Scala?Why Scala?
Why Scala?
Ā 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
Ā 
NaĆÆvetĆ© vs. Experience
NaĆÆvetĆ© vs. ExperienceNaĆÆvetĆ© vs. Experience
NaĆÆvetĆ© vs. Experience
Ā 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
Ā 
Intro toswift1
Intro toswift1Intro toswift1
Intro toswift1
Ā 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009
Ā 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java Programmers
Ā 
Introduction to Scala for Java Developers
Introduction to Scala for Java DevelopersIntroduction to Scala for Java Developers
Introduction to Scala for Java Developers
Ā 
Python - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave ParkPython - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave Park
Ā 
Scala
ScalaScala
Scala
Ā 
Scala presentationjune112011
Scala presentationjune112011Scala presentationjune112011
Scala presentationjune112011
Ā 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Ā 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
Ā 
Processing OWL2 Ontologies using Thea: An application of Logic Programming
Processing OWL2 Ontologies using Thea: An application of Logic ProgrammingProcessing OWL2 Ontologies using Thea: An application of Logic Programming
Processing OWL2 Ontologies using Thea: An application of Logic Programming
Ā 
Thea: Processing OWL Ontologies - An application of logic programming
Thea: Processing OWL Ontologies - An application of logic programmingThea: Processing OWL Ontologies - An application of logic programming
Thea: Processing OWL Ontologies - An application of logic programming
Ā 
1.2 Scala Basics
1.2 Scala Basics1.2 Scala Basics
1.2 Scala Basics
Ā 
Scala introduction
Scala introductionScala introduction
Scala introduction
Ā 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
Ā 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
Ā 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
Ā 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
Ā 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
Ā 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
Ā 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
Ā 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
Ā 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
Ā 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜RTylerCroy
Ā 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
Ā 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
Ā 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...gurkirankumar98700
Ā 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
Ā 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
Ā 
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | DelhiFULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhisoniya singh
Ā 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
Ā 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
Ā 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
Ā 

Recently uploaded (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
Ā 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
Ā 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
Ā 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
Ā 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Ā 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
Ā 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
Ā 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
Ā 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
Ā 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜
Ā 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Ā 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
Ā 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...
Ā 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
Ā 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
Ā 
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | DelhiFULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
Ā 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
Ā 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
Ā 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Ā 

Scala for Java Developers

  • 1. Scala for Java Developers Ā© 2011 SpringOne 2GX. All rights reserved. Do not distribute without permission.
  • 2. What is Scala ā€œ 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
  • 3.
  • 4.
  • 5.
  • 6.
  • 7. Hello World: Scripting Style $ scala hello-script.scala Hello World No compilation
  • 8. Hello World: Porting of Java Code $ scalac hello-java.scala $ scala example.Main Hello World ā€˜ staticā€™ Inferred semicolons
  • 9. Hello World: Using the App trait $ scalac hello-app.scala $ scala example.Main Hello World
  • 10. Simple Class class Person val p = new Person Type Inferred Default access: public No curly braces needed (but allowed)
  • 11. Simple Class class Person val p: Person = new Person Explicit type specification
  • 12. Class with constructor class Person(firstName: String, lastName: String) val p = new Person( &quot;Ramnivas&quot; , &quot;Laddad&quot; ) println(p.firstName) // Error Primary constructor Fields ā€“ accessible in class body
  • 13. Class with ā€œgettersā€ class Person( val firstName: String, val lastName: String) val p = new Person( &quot;Ramnivas&quot; , &quot;Laddad&quot; ) println(p.firstName) Value (Java ā€˜finalā€™)
  • 14. Class with ā€œgettersā€ and ā€œsettersā€ class Person( var firstName: String, var lastName: String) val p = new Person( &quot;Ramnivas&quot; , &quot;Laddad&quot; ) println(p.firstName) p.firstName = &quot;Ramnivas2ā€ Variable (Java non-final)
  • 15. Extending a class val s = new Student( &quot;Ramnivas&quot; , &quot;Laddad&quot; , 1) println(s.firstName) println(s.grade)
  • 16. Defining methods class Person( val firstName: String, val lastName: String) { def name = firstName + &quot; &quot; + lastName override def toString = name } val p = new Person( &quot;Ramnivas&quot; , &quot;Laddad&quot; ) println(p.name) // Ramnivas Laddad println(p) // Ramnivas Laddad Not optional
  • 17. Uniform access principle class Person( val firstName: String, val lastName: String) { val name = firstName + &quot; &quot; + lastName override def toString = name } val p = new Person( &quot;Ramnivas&quot; , &quot;Laddad&quot; ) println(p.name) // Ramnivas Laddad println(p) // Ramnivas Laddad
  • 18.
  • 19.
  • 20.
  • 21. Traits: Interfaces done right trait PartyGoer { val age: Int val yearsUntilLegalDrinking = if (age >= 18) 0 else 18-age }
  • 22. Collections val people = List( &quot;John&quot; , &quot;Jacob&quot; , &quot;Mike&quot; )
  • 23. Collections val people = Array( &quot;John&quot; , &quot;Jacob&quot; , &quot;Mike&quot; )
  • 24. Working with collections: for comprehension for (person <- people) { println(person) }
  • 25. Working with collections: for comprehension for (person <- people if person startsWith &quot;J&quot; ) { println( &quot;&quot;&quot;Lucky one to start name in &quot;J&quot; &quot;&quot;&quot; + person) } // You are lucky one to start name in &quot;J&quot; John // You are lucky one to start name in &quot;J&quot; Jacob
  • 26. Working with collections: filter val student1 = new Student( &quot;first1&quot; , &quot;last1&quot; , 1) val student2 = new Student( &quot;first2&quot; , &quot;last2&quot; , 1) val student3 = new Student( &quot;first3&quot; , &quot;last3&quot; , 2) val student4 = new Student( &quot;first4&quot; , &quot;last4&quot; , 6) val students = List(student1, student2, student3, student4)
  • 27. Working with collections: filter val student1 = new Student( &quot;first1&quot; , &quot;last1&quot; , 1) val student2 = new Student( &quot;first2&quot; , &quot;last2&quot; , 1) val student3 = new Student( &quot;first3&quot; , &quot;last3&quot; , 2) val student4 = new Student( &quot;first4&quot; , &quot;last4&quot; , 6) val students = List(student1, student2, student3, student4)
  • 28. Working with collections: using _ val student1 = new Student( &quot;first1&quot; , &quot;last1&quot; , 1) val student2 = new Student( &quot;first2&quot; , &quot;last2&quot; , 1) val student3 = new Student( &quot;first3&quot; , &quot;last3&quot; , 2) val student4 = new Student( &quot;first4&quot; , &quot;last4&quot; , 6) val students = List(student1, student2, student3, student4)
  • 29. Working with collections: passing method as function val student1 = new Student( &quot;first1&quot; , &quot;last1&quot; , 1) val student2 = new Student( &quot;first2&quot; , &quot;last2&quot; , 1) val student3 = new Student( &quot;first3&quot; , &quot;last3&quot; , 2) val student4 = new Student( &quot;first4&quot; , &quot;last4&quot; , 6) val students = List(student1, student2, student3, student4)
  • 30. Working with collections: partition val student1 = new Student( &quot;first1&quot; , &quot;last1&quot; , 1) val student2 = new Student( &quot;first2&quot; , &quot;last2&quot; , 1) val student3 = new Student( &quot;first3&quot; , &quot;last3&quot; , 2) val student4 = new Student( &quot;first4&quot; , &quot;last4&quot; , 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
  • 31. Working with collections: transforming val student1 = new Student( &quot;first1&quot; , &quot;last1&quot; , 1) val student2 = new Student( &quot;first2&quot; , &quot;last2&quot; , 1) val student3 = new Student( &quot;first3&quot; , &quot;last3&quot; , 2) val student4 = new Student( &quot;first4&quot; , &quot;last4&quot; , 6) val students = List(student1, student2, student3, student4)
  • 33.
  • 34. 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))
  • 35. Putting it together: Quicksort println(quicksort(List(1, 3, 4, 5, 1)))
  • 36. Pattern matching: Basics val a:Any = &quot;foo&quot; a match { case str: String => println( &quot;A string: &quot; + str) case i: Int => println( &quot;An int: &quot; + i) case _ => println( &quot;Something else&quot; ) }
  • 37. Pattern matching: with collections val l = List( &quot;a&quot; , &quot;b&quot; , &quot;c&quot; ) l match { case Nil => println( &quot;Empty!&quot; ) case head :: Nil => println( &quot;Only one item &quot; + head) case head :: tail => println( &quot;Item &quot; + head + &quot; followed by &quot; + tail) }
  • 38. 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)))
  • 39. Destutter using Pattern matching 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()
  • 40.
  • 41. Case classes case class Human(name: String) case class SuperHero(name: String, power: String) val characters = List(Human( &quot;Programmer&quot; ), SuperHero( &quot;Customer&quot; , &quot;money&quot; ), SuperHero( &quot;QA&quot; , &quot;testing&quot; ))
  • 42. Case classes and pattern matching val actions = for (character <- characters) yield character match { case Human(name) => name + &quot; needs to be saved&quot; case SuperHero(name, power) => name + &quot; will save using &quot; + power } actions.foreach(println)
  • 43. Pattern matching and extracting just enough val actions = for (character <- characters) yield character match { case Human(name) => name + &quot; needs to be saved&quot; case SuperHero(_, power) => &quot;Could be saved using &quot; + power } actions.foreach(println) // Programmer needs to be saved // Could be saved using money // Could be saved using testing
  • 44. Regular expressions val text = &quot;Ramnivas Laddad&quot; val Name = &quot;&quot;&quot;(+)+(+)&quot;&quot;&quot; .r val person = text match { case Name(first, last) => Some( new Person(first, last)) case _ => None } println(person) // Some(Ramnivas Laddad)
  • 45. Options val texts = List( &quot;Ramnivas Laddad&quot; , &quot;foo&quot; , &quot;Scott Andrews&quot; ) val peopleOptions = texts.map { _ match { case Name(first, last) => Some( new Person(first, last)) case _ => None } } println(peopleOptions) // List(Some(Ramnivas Laddad), None, Some(Scott Andrews))
  • 46. Options: flattening val texts = List( &quot;Ramnivas Laddad&quot; , &quot;foo&quot; , &quot;Scott Andrews&quot; ) val peopleOptions = texts.map { _ match { case Name(first, last) => Some( new Person(first, last)) case _ => None } } println(peopleOptions.flatten) // List(Ramnivas Laddad, Scott Andrews)
  • 47. Options: flatMap val texts = List( &quot;Ramnivas Laddad&quot; , &quot;foo&quot; , &quot;Scott Andrews&quot; ) val people = texts.flatMap { _ match { case Name(first, last) => Some( new Person(first, last)) case _ => None } } println(people) // List(Ramnivas Laddad, Scott Andrews)
  • 48. Higher order functions def process() : Unit = { retry(5) { ... } } def retry[T](maxRetry: Int)(thunk: => T) = { def retry(thunk: => T, attempt: Int): T = { try { thunk } catch { case ex if (attempt < maxRetry) => retry(thunk, attempt + 1) } } retry(thunk, 0) } Thunk
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56. Scala for Java Developers Ā© 2011 SpringOne 2GX. All rights reserved. Do not distribute without permission.

Editor's Notes

  1. No access spec (default: public), no parentheses, type inference
  2. How constructor params can be used in body