SlideShare una empresa de Scribd logo
1 de 62
If I were to pick a language to use on the JVM today other than Java, it would be Scala. –  James Gosling, creator of Java http://www.adam-bien.com/roller/abien/entry/java_net_javaone_which_programming Scala, it must be stated, is the current heir apparent to the Java throne. No other language on the JVM seems as capable of being a "replacement for Java" as Scala, and the momentum behind Scala is now unquestionable. –  Charlies Nutter, JRuby lead http://blog.headius.com/2009/04/future-part-one.html My tip though for the long term replacement of javac is Scala. I'm very impressed with it! I can honestly say if someone had shown me the Programming in Scala book […] back in 2003 I'd probably have never created Groovy. –  James Strachan, creator of Groovy http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html
Agenda ,[object Object]
Intro to Scala ,[object Object]
Pattern matching
Functions
Classes and traits ,[object Object]
Practical Scala
About us ,[object Object]
Java developers with 6 and 9 years experience
Scala enthusiasts since 2008
Developed SubmitIT for JavaZone
Alf worked on Scala application for Kommuneforlaget
Held 4 Scala training courses for 60+ participants
 
public   class  Person {  private   int   age ; private  String  name ; public  Person( int  age, String name) { this . age  = age; this . name  = name; } public   int  getAge() {  return   this . age ;  } public   void  setAge( int  age) {  this . age  = age;  } public  String getName() {  return   this . name ;  } public   void  setName(String name) {  this . name  = name; } } class   Person ( var   age :  Int ,   var   name :  String )
List<Person> persons =  ...  List<Person> adults =  new  LinkedList<Person>(); List<Person> kids =  new  LinkedList<Person>(); for  (Person person : persons) { if  (person.getAge() < 18) { kids.add(person); }  else  { adults.add(person); } } val   persons :  List [ Person ] =  ... val  ( kids ,  adults ) =  persons . partition (_. age   <  18)
using ( new   BufferedReader ( new   FileReader ( &quot;f.txt&quot; ))) { reader  =>  println ( reader . readLine() ) } BufferedReader reader =  null ; try  { reader =  new  BufferedReader( new  FileReader( &quot;f.txt&quot; )); System. out .println(reader.readLine()); }  finally  { if  (reader !=  null ) { try  { reader.close(); }  catch  (IOException e) { // Exception on close, ignore } } }
Scala ,[object Object]
Statically typed
Java compatible ,[object Object]
Existing libraries/frameworks ,[object Object]
;
public   class  Person {  private   int   age ; private  String  name ; public  Person( int  age, String name) { this . age  = age; this . name  = name; } public   int  getAge() {  return   this . age ;  } public   void  setAge( int  age) {  this . age  = age;  } public  String getName() {  return   this . name ;  } public   void  setName(String name) {  this . name  = name; } }
public   class  Person {  private   int   age private  String  name public  Person( int  age, String name) { this . age  = age this . name  = name } public   int  getAge() {  return   this . age   } public   void  setAge( int  age) {  this . age  = age  } public  String getName() {  return   this . name   } public   void  setName(String name) {  this . name  = name } }
Variables var   i :  Int  = 42
Variables var   i  = 42 i  = 3 i  =  &quot;Hello world!&quot; // compile error
Values val   i  = 42 i  = 3 // compile error
Methods def   sum ( a :  Int ,  b :  Int ):  Int  = { return   a   +   b }
Methods def   sum ( a :  Int ,  b :  Int ):  Int  = { a   +   b }
Methods def   sum ( a :  Int ,  b :  Int ) = { a   +   b }
Methods def   sum ( a :  Int ,  b :  Int ) =  a   +   b
Methods def   sayHello ( name :  String ) { println ( &quot;Hello, &quot;   +   name ) }
Methods &quot;apple&quot; . charAt (0) &quot;apple&quot;   charAt  0 1.0.+(2.0) 1.0 + 2.0
Collections val   list   =  List ( &quot;apple&quot; ,  &quot;orange&quot; ,  &quot;banana&quot; ) val   map   =  Map (1  ->   &quot;one&quot; , 2  ->   &quot;two&quot; ) val   array  =  Array (1, 2, 3, 4, 5) list (1) // orange map (2) // two array (3) // 4
myObject   match  { case  1 =>  println ( &quot;First&quot; ) case  2 =>  println ( &quot;Second&quot; ) case  _ =>  println ( &quot;Unknown&quot; ) } Pattern matching
myObject   match  { case   i :  Int  =>  println ( &quot;Found number &quot;  +  i ) case   s :  String  =>  println ( &quot;Found text &quot;  +  s ) case  _ =>  println ( &quot;Unknown&quot; ) } Pattern matching
val   email  =  &quot;&quot;&quot; (.+) @ (.+) &quot;&quot;&quot; . r &quot; scala @ java.no &quot;   match  { case   email ( name ,  domain ) =>  println ( &quot;User &quot;   +   name   +   &quot; at &quot;   +   domain ) case   x  =>  println ( x   +   &quot; is not an email&quot; ) } Pattern matching
val   numbers  =  List (1, 2, 3) val   secondNumber  =  numbers   match  { case   List (_,  i , _*) =>  Some ( i ) case  _ =>  None } =>  secondNumber :  Option [ Int ] =  Some (2) Pattern matching
Functions Predicate <Integer> even =  new   Predicate <Integer>() { @ Override public   boolean  apply(Integer i) { return  i % 2 == 0; } }; List<Integer> numbers = …  // 1, 2, 3, 4 Iterable<Integer> evenNums = Iterables .filter(numbers, even); => [2, 4] Google collections:
Functions val   even  = ( i :  Int ) =>  i   %  2  ==  0 val   numbers  =  List (1, 2, 3, 4) val   evenNums  =  numbers . filter ( even ) => List(2, 4) Scala collections:
Functions val   numbers  =  List (1, 2, 3, 4) val   evenNums  =  numbers . filter (( i :  Int ) =>  i   %  2  ==  0) => List(2, 4) Scala collections:
Functions val   numbers  =  List (1, 2, 3, 4) val   evenNums  =  numbers . filter ( i  =>  i   %  2  ==  0) => List(2, 4) Scala collections:
Functions val   numbers  =  List (1, 2, 3, 4) val   evenNums  =  numbers . filter (_ % 2 == 0) => List(2, 4) Scala collections:
Functions Predicate <Integer> even =  new   Predicate <Integer>() { @ Override public   boolean  apply(Integer i) { return  i % 2 == 0; } }; val   numbers  =  List (1, 2, 3, 4) val   evenNums  =  numbers . filter (_ % 2 == 0) => List(2, 4) Scala collections:
Functions #boolean(int) even = #(int i)(i % 2 == 0) JDK7 lambda expressions: val   even  = ( i :  Int ) =>  i   %  2  ==  0 Scala functions:
Functions Iterables.filter(numbers, #(int i)(i % 2 == 0)) JDK7 lambda expressions: numbers . filter (_ % 2 == 0) Scala functions:
Control structures return values val   numbers  =  for  ( i  <- 1  to  10)  yield   i val   res  =  if  ( cond )  x   else   y val   res2  =  try  {  x  }  catch  { …  y  }  finally  { … }
Classes and constructors class   Person ( val   age :  Int )
Classes and constructors class   Person ( val   age :  Int ) { def   this () =  this (42) var   name :  String  = _ override   def   toString  =  &quot;My name is &quot;   +   name }
Traits (= Interface + Mixin) ,[object Object]
Implement methods
Initialized fields
Abstract methods and fields
Does not support constructors
Call to super -> strict semantics
scala.Ordered trait trait   Ordered [ A ] { def   compare ( that :  A ):  Int def   <   ( that :  A ):  Boolean  = ( this   compare   that )  <   0 def   >   ( that :  A ):  Boolean  = ( this   compare   that )  >   0 def   <=  ( that :  A ):  Boolean  = ( this   compare   that )  <=  0 def   >=  ( that :  A ):  Boolean  = ( this   compare   that )  >=  0 }
The Ordered trait class   Person ( val   age :  Int )  extends   Ordered [ Person ] { def   compare ( other :  Person ) =  this . age   -   other . age } val   person1  =  new   Person (21) val   person2  =  new   Person (31) person1  <  person2  // true person1  <=  person2 // true person1  >=  person2 // false
“Dynamic mixins” class   Person ( val   name :  String,  val   age :  Int ) { override   def   toString  =  &quot;My name is &quot;   +   name } trait   Loud  { override   def   toString  =  super . toString . toUpperCase } val   person  =  new   Person ( &quot;Fredrik&quot; , 18)  with   Loud println ( person ) => MY NAME IS FREDRIK
Just scratching the surface... ,[object Object]
Singleton objects
Closures
For-comprehensions

Más contenido relacionado

La actualidad más candente

The Groovy Puzzlers – The Complete 01 and 02 Seasons
The Groovy Puzzlers – The Complete 01 and 02 SeasonsThe Groovy Puzzlers – The Complete 01 and 02 Seasons
The Groovy Puzzlers – The Complete 01 and 02 SeasonsBaruch Sadogursky
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 
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
 
Java 8 Puzzlers [as presented at OSCON 2016]
Java 8 Puzzlers [as presented at  OSCON 2016]Java 8 Puzzlers [as presented at  OSCON 2016]
Java 8 Puzzlers [as presented at OSCON 2016]Baruch Sadogursky
 
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev FedorProgramming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev FedorFedor Lavrentyev
 
The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196Mahmoud Samir Fayed
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonUC San Diego
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory archana singh
 
Programmation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScriptProgrammation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScriptLoïc Knuchel
 
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf MilanFrom Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf MilanFabio Collini
 
Kotlin Advanced - Apalon Kotlin Sprint Part 3
Kotlin Advanced - Apalon Kotlin Sprint Part 3Kotlin Advanced - Apalon Kotlin Sprint Part 3
Kotlin Advanced - Apalon Kotlin Sprint Part 3Kirill Rozov
 
Kotlin collections
Kotlin collectionsKotlin collections
Kotlin collectionsMyeongin Woo
 
JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashBret Little
 
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFrom java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFabio Collini
 
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night TurinAsync code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night TurinFabio Collini
 

La actualidad más candente (20)

The Groovy Puzzlers – The Complete 01 and 02 Seasons
The Groovy Puzzlers – The Complete 01 and 02 SeasonsThe Groovy Puzzlers – The Complete 01 and 02 Seasons
The Groovy Puzzlers – The Complete 01 and 02 Seasons
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
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
 
Java 8 Puzzlers [as presented at OSCON 2016]
Java 8 Puzzlers [as presented at  OSCON 2016]Java 8 Puzzlers [as presented at  OSCON 2016]
Java 8 Puzzlers [as presented at OSCON 2016]
 
Why Learn Python?
Why Learn Python?Why Learn Python?
Why Learn Python?
 
Alternate JVM Languages
Alternate JVM LanguagesAlternate JVM Languages
Alternate JVM Languages
 
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev FedorProgramming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
 
The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
Google Guava
Google GuavaGoogle Guava
Google Guava
 
Programmation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScriptProgrammation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScript
 
Sneaking inside Kotlin features
Sneaking inside Kotlin featuresSneaking inside Kotlin features
Sneaking inside Kotlin features
 
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf MilanFrom Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
From Java to Kotlin beyond alt+shift+cmd+k - Kotlin Community Conf Milan
 
Kotlin Advanced - Apalon Kotlin Sprint Part 3
Kotlin Advanced - Apalon Kotlin Sprint Part 3Kotlin Advanced - Apalon Kotlin Sprint Part 3
Kotlin Advanced - Apalon Kotlin Sprint Part 3
 
Kotlin collections
Kotlin collectionsKotlin collections
Kotlin collections
 
JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and Lodash
 
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFrom java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
 
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night TurinAsync code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
 
Lodash js
Lodash jsLodash js
Lodash js
 

Destacado

Clojure - JVM språket som er "multi-core ready"
Clojure - JVM språket som er "multi-core ready"Clojure - JVM språket som er "multi-core ready"
Clojure - JVM språket som er "multi-core ready"Alf Kristian Støyle
 
Bibliothcairesdufuturetfuturdesbibliothques 140617024643-phpapp02 (1)
Bibliothcairesdufuturetfuturdesbibliothques 140617024643-phpapp02 (1)Bibliothcairesdufuturetfuturdesbibliothques 140617024643-phpapp02 (1)
Bibliothcairesdufuturetfuturdesbibliothques 140617024643-phpapp02 (1)William Jouve
 
Distributed Computing - Cloud Computing and Other Buzzwords: Implications for...
Distributed Computing - Cloud Computing and Other Buzzwords: Implications for...Distributed Computing - Cloud Computing and Other Buzzwords: Implications for...
Distributed Computing - Cloud Computing and Other Buzzwords: Implications for...Mark Conrad
 
The account problem in Java and Clojure
The account problem in Java and ClojureThe account problem in Java and Clojure
The account problem in Java and ClojureAlf Kristian Støyle
 
Scala 3camp 2011
Scala   3camp 2011Scala   3camp 2011
Scala 3camp 2011Scalac
 
Scala == Effective Java
Scala == Effective JavaScala == Effective Java
Scala == Effective JavaScalac
 

Destacado (11)

Likwidacja linii tramwaju szybkiego w Zielonej Górze
Likwidacja linii tramwaju szybkiego w Zielonej GórzeLikwidacja linii tramwaju szybkiego w Zielonej Górze
Likwidacja linii tramwaju szybkiego w Zielonej Górze
 
Clojure - JVM språket som er "multi-core ready"
Clojure - JVM språket som er "multi-core ready"Clojure - JVM språket som er "multi-core ready"
Clojure - JVM språket som er "multi-core ready"
 
Logi
LogiLogi
Logi
 
Learning Lisp
Learning LispLearning Lisp
Learning Lisp
 
Bibliothcairesdufuturetfuturdesbibliothques 140617024643-phpapp02 (1)
Bibliothcairesdufuturetfuturdesbibliothques 140617024643-phpapp02 (1)Bibliothcairesdufuturetfuturdesbibliothques 140617024643-phpapp02 (1)
Bibliothcairesdufuturetfuturdesbibliothques 140617024643-phpapp02 (1)
 
Clojure workshop
Clojure workshopClojure workshop
Clojure workshop
 
Distributed Computing - Cloud Computing and Other Buzzwords: Implications for...
Distributed Computing - Cloud Computing and Other Buzzwords: Implications for...Distributed Computing - Cloud Computing and Other Buzzwords: Implications for...
Distributed Computing - Cloud Computing and Other Buzzwords: Implications for...
 
Paralell collections in Scala
Paralell collections in ScalaParalell collections in Scala
Paralell collections in Scala
 
The account problem in Java and Clojure
The account problem in Java and ClojureThe account problem in Java and Clojure
The account problem in Java and Clojure
 
Scala 3camp 2011
Scala   3camp 2011Scala   3camp 2011
Scala 3camp 2011
 
Scala == Effective Java
Scala == Effective JavaScala == Effective Java
Scala == Effective Java
 

Similar a Scala introduction

JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersTikal Knowledge
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scalaparag978978
 
2.1 Recap From Day One
2.1 Recap From Day One2.1 Recap From Day One
2.1 Recap From Day Oneretronym
 
1.2 Scala Basics
1.2 Scala Basics1.2 Scala Basics
1.2 Scala Basicsretronym
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Languageintelliyole
 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009David Pollak
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In ScalaSkills Matter
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. ExperienceMike Fogus
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokusHamletDRC
 

Similar a Scala introduction (20)

Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
JBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java ProgrammersJBUG 11 - Scala For Java Programmers
JBUG 11 - Scala For Java Programmers
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scala
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
2.1 Recap From Day One
2.1 Recap From Day One2.1 Recap From Day One
2.1 Recap From Day One
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
1.2 Scala Basics
1.2 Scala Basics1.2 Scala Basics
1.2 Scala Basics
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. Experience
 
Scala en
Scala enScala en
Scala en
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
 

Último

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
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
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 

Último (20)

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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
 
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
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
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...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Scala introduction

  • 1. If I were to pick a language to use on the JVM today other than Java, it would be Scala. – James Gosling, creator of Java http://www.adam-bien.com/roller/abien/entry/java_net_javaone_which_programming Scala, it must be stated, is the current heir apparent to the Java throne. No other language on the JVM seems as capable of being a &quot;replacement for Java&quot; as Scala, and the momentum behind Scala is now unquestionable. – Charlies Nutter, JRuby lead http://blog.headius.com/2009/04/future-part-one.html My tip though for the long term replacement of javac is Scala. I'm very impressed with it! I can honestly say if someone had shown me the Programming in Scala book […] back in 2003 I'd probably have never created Groovy. – James Strachan, creator of Groovy http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html
  • 2.
  • 3.
  • 6.
  • 8.
  • 9. Java developers with 6 and 9 years experience
  • 12. Alf worked on Scala application for Kommuneforlaget
  • 13. Held 4 Scala training courses for 60+ participants
  • 14.  
  • 15. public class Person { private int age ; private String name ; public Person( int age, String name) { this . age = age; this . name = name; } public int getAge() { return this . age ; } public void setAge( int age) { this . age = age; } public String getName() { return this . name ; } public void setName(String name) { this . name = name; } } class Person ( var age : Int , var name : String )
  • 16. List<Person> persons = ... List<Person> adults = new LinkedList<Person>(); List<Person> kids = new LinkedList<Person>(); for (Person person : persons) { if (person.getAge() < 18) { kids.add(person); } else { adults.add(person); } } val persons : List [ Person ] = ... val ( kids , adults ) = persons . partition (_. age < 18)
  • 17. using ( new BufferedReader ( new FileReader ( &quot;f.txt&quot; ))) { reader => println ( reader . readLine() ) } BufferedReader reader = null ; try { reader = new BufferedReader( new FileReader( &quot;f.txt&quot; )); System. out .println(reader.readLine()); } finally { if (reader != null ) { try { reader.close(); } catch (IOException e) { // Exception on close, ignore } } }
  • 18.
  • 20.
  • 21.
  • 22. ;
  • 23. public class Person { private int age ; private String name ; public Person( int age, String name) { this . age = age; this . name = name; } public int getAge() { return this . age ; } public void setAge( int age) { this . age = age; } public String getName() { return this . name ; } public void setName(String name) { this . name = name; } }
  • 24. public class Person { private int age private String name public Person( int age, String name) { this . age = age this . name = name } public int getAge() { return this . age } public void setAge( int age) { this . age = age } public String getName() { return this . name } public void setName(String name) { this . name = name } }
  • 25. Variables var i : Int = 42
  • 26. Variables var i = 42 i = 3 i = &quot;Hello world!&quot; // compile error
  • 27. Values val i = 42 i = 3 // compile error
  • 28. Methods def sum ( a : Int , b : Int ): Int = { return a + b }
  • 29. Methods def sum ( a : Int , b : Int ): Int = { a + b }
  • 30. Methods def sum ( a : Int , b : Int ) = { a + b }
  • 31. Methods def sum ( a : Int , b : Int ) = a + b
  • 32. Methods def sayHello ( name : String ) { println ( &quot;Hello, &quot; + name ) }
  • 33. Methods &quot;apple&quot; . charAt (0) &quot;apple&quot; charAt 0 1.0.+(2.0) 1.0 + 2.0
  • 34. Collections val list = List ( &quot;apple&quot; , &quot;orange&quot; , &quot;banana&quot; ) val map = Map (1 -> &quot;one&quot; , 2 -> &quot;two&quot; ) val array = Array (1, 2, 3, 4, 5) list (1) // orange map (2) // two array (3) // 4
  • 35. myObject match { case 1 => println ( &quot;First&quot; ) case 2 => println ( &quot;Second&quot; ) case _ => println ( &quot;Unknown&quot; ) } Pattern matching
  • 36. myObject match { case i : Int => println ( &quot;Found number &quot; + i ) case s : String => println ( &quot;Found text &quot; + s ) case _ => println ( &quot;Unknown&quot; ) } Pattern matching
  • 37. val email = &quot;&quot;&quot; (.+) @ (.+) &quot;&quot;&quot; . r &quot; scala @ java.no &quot; match { case email ( name , domain ) => println ( &quot;User &quot; + name + &quot; at &quot; + domain ) case x => println ( x + &quot; is not an email&quot; ) } Pattern matching
  • 38. val numbers = List (1, 2, 3) val secondNumber = numbers match { case List (_, i , _*) => Some ( i ) case _ => None } => secondNumber : Option [ Int ] = Some (2) Pattern matching
  • 39. Functions Predicate <Integer> even = new Predicate <Integer>() { @ Override public boolean apply(Integer i) { return i % 2 == 0; } }; List<Integer> numbers = … // 1, 2, 3, 4 Iterable<Integer> evenNums = Iterables .filter(numbers, even); => [2, 4] Google collections:
  • 40. Functions val even = ( i : Int ) => i % 2 == 0 val numbers = List (1, 2, 3, 4) val evenNums = numbers . filter ( even ) => List(2, 4) Scala collections:
  • 41. Functions val numbers = List (1, 2, 3, 4) val evenNums = numbers . filter (( i : Int ) => i % 2 == 0) => List(2, 4) Scala collections:
  • 42. Functions val numbers = List (1, 2, 3, 4) val evenNums = numbers . filter ( i => i % 2 == 0) => List(2, 4) Scala collections:
  • 43. Functions val numbers = List (1, 2, 3, 4) val evenNums = numbers . filter (_ % 2 == 0) => List(2, 4) Scala collections:
  • 44. Functions Predicate <Integer> even = new Predicate <Integer>() { @ Override public boolean apply(Integer i) { return i % 2 == 0; } }; val numbers = List (1, 2, 3, 4) val evenNums = numbers . filter (_ % 2 == 0) => List(2, 4) Scala collections:
  • 45. Functions #boolean(int) even = #(int i)(i % 2 == 0) JDK7 lambda expressions: val even = ( i : Int ) => i % 2 == 0 Scala functions:
  • 46. Functions Iterables.filter(numbers, #(int i)(i % 2 == 0)) JDK7 lambda expressions: numbers . filter (_ % 2 == 0) Scala functions:
  • 47. Control structures return values val numbers = for ( i <- 1 to 10) yield i val res = if ( cond ) x else y val res2 = try { x } catch { … y } finally { … }
  • 48. Classes and constructors class Person ( val age : Int )
  • 49. Classes and constructors class Person ( val age : Int ) { def this () = this (42) var name : String = _ override def toString = &quot;My name is &quot; + name }
  • 50.
  • 54. Does not support constructors
  • 55. Call to super -> strict semantics
  • 56. scala.Ordered trait trait Ordered [ A ] { def compare ( that : A ): Int def < ( that : A ): Boolean = ( this compare that ) < 0 def > ( that : A ): Boolean = ( this compare that ) > 0 def <= ( that : A ): Boolean = ( this compare that ) <= 0 def >= ( that : A ): Boolean = ( this compare that ) >= 0 }
  • 57. The Ordered trait class Person ( val age : Int ) extends Ordered [ Person ] { def compare ( other : Person ) = this . age - other . age } val person1 = new Person (21) val person2 = new Person (31) person1 < person2 // true person1 <= person2 // true person1 >= person2 // false
  • 58. “Dynamic mixins” class Person ( val name : String, val age : Int ) { override def toString = &quot;My name is &quot; + name } trait Loud { override def toString = super . toString . toUpperCase } val person = new Person ( &quot;Fredrik&quot; , 18) with Loud println ( person ) => MY NAME IS FREDRIK
  • 59.
  • 67. Q & A
  • 68. val xml = <break time=&quot;15&quot; />
  • 69.
  • 70. Tools
  • 74.
  • 77.
  • 78. IntelliJ IDEA - very good, but slow compilation
  • 79. Eclipse - bad, but very fast when working
  • 80.
  • 81. Think twice before using a library
  • 82.
  • 83. Maven
  • 84. SBT - Simple Build Tool
  • 86.
  • 88. Commercial, but free for Scala
  • 89.
  • 91.
  • 94. The feel of Scala http://parleys.com/#id=10&st=5&sl=1
  • 95.
  • 98.
  • 99. Easy to write Java-ish Scala
  • 100. The language scales with your understanding
  • 101. Gradually migrate to more functional style
  • 102.
  • 103.
  • 106.
  • 107. f(x) = 2x + 3
  • 108.
  • 109. No side-effects from method calls
  • 110. All methods must return something
  • 111.
  • 112.
  • 114.
  • 115. Easier to avoid errors
  • 117. We have a lot to learn
  • 118. Q & A
  • 119.