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...
Agenda <ul><li>Background and motivation
Intro to Scala </li><ul><li>Basic syntax
Pattern matching
Functions
Classes and traits </li></ul><li><break />
Practical Scala </li></ul>
About us <ul><li>Alf Kristian Støyle and Fredrik Vraalsen
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 </li></ul>
 
public   class  Person {  private   int   age ; private  String  name ; public  Person( int  age, String name) { this . ag...
List<Person> persons =  ...  List<Person> adults =  new  LinkedList<Person>(); List<Person> kids =  new  LinkedList<Person...
using ( new   BufferedReader ( new   FileReader ( &quot;f.txt&quot; ))) { reader  =>  println ( reader . readLine() ) } Bu...
Scala <ul><li>Object oriented and functional
Statically typed
Java compatible </li><ul><li>Compiles to Java bytecode
Existing libraries/frameworks </li></ul><li>Better Java </li></ul>
;
public   class  Person {  private   int   age ; private  String  name ; public  Person( int  age, String name) { this . ag...
public   class  Person {  private   int   age private  String  name public  Person( int  age, String name) { this . age  =...
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  ...
myObject   match  { case  1 =>  println ( &quot;First&quot; ) case  2 =>  println ( &quot;Second&quot; ) case  _ =>  print...
myObject   match  { case   i :  Int  =>  println ( &quot;Found number &quot;  +  i ) case   s :  String  =>  println ( &qu...
val   email  =  &quot;&quot;&quot; (.+) @ (.+) &quot;&quot;&quot; . r &quot; scala @ java.no &quot;   match  { case   emai...
val   numbers  =  List (1, 2, 3) val   secondNumber  =  numbers   match  { case   List (_,  i , _*) =>  Some ( i ) case  _...
Functions Predicate <Integer> even =  new   Predicate <Integer>() { @ Override public   boolean  apply(Integer i) { return...
Functions val   even  = ( i :  Int ) =>  i   %  2  ==  0 val   numbers  =  List (1, 2, 3, 4) val   evenNums  =  numbers . ...
Functions val   numbers  =  List (1, 2, 3, 4) val   evenNums  =  numbers . filter (( i :  Int ) =>  i   %  2  ==  0) => Li...
Functions val   numbers  =  List (1, 2, 3, 4) val   evenNums  =  numbers . filter ( i  =>  i   %  2  ==  0) => List(2, 4) ...
Functions val   numbers  =  List (1, 2, 3, 4) val   evenNums  =  numbers . filter (_ % 2 == 0) => List(2, 4) Scala collect...
Functions Predicate <Integer> even =  new   Predicate <Integer>() { @ Override public   boolean  apply(Integer i) { return...
Functions #boolean(int) even = #(int i)(i % 2 == 0) JDK7 lambda expressions: val   even  = ( i :  Int ) =>  i   %  2  ==  ...
Functions Iterables.filter(numbers, #(int i)(i % 2 == 0)) JDK7 lambda expressions: numbers . filter (_ % 2 == 0) Scala fun...
Control structures return values val   numbers  =  for  ( i  <- 1  to  10)  yield   i val   res  =  if  ( cond )  x   else...
Classes and constructors class   Person ( val   age :  Int )
Classes and constructors class   Person ( val   age :  Int ) { def   this () =  this (42) var   name :  String  = _ overri...
Traits (= Interface + Mixin) <ul><li>“Multiple inheritance done right”
Implement methods
Initialized fields
Abstract methods and fields
Does not support constructors
Call to super -> strict semantics </li></ul>
scala.Ordered trait trait   Ordered [ A ] { def   compare ( that :  A ):  Int def   <   ( that :  A ):  Boolean  = ( this ...
The Ordered trait class   Person ( val   age :  Int )  extends   Ordered [ Person ] { def   compare ( other :  Person ) = ...
“Dynamic mixins” class   Person ( val   name :  String,  val   age :  Int ) { override   def   toString  =  &quot;My name ...
Just scratching the surface... <ul><li>Tuples
Singleton objects
Closures
For-comprehensions
Upcoming SlideShare
Loading in...5
×

Scala introduction

5,426

Published on

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,426
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
181
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

Scala introduction

  1. 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. 2. Agenda <ul><li>Background and motivation
  3. 3. Intro to Scala </li><ul><li>Basic syntax
  4. 4. Pattern matching
  5. 5. Functions
  6. 6. Classes and traits </li></ul><li><break />
  7. 7. Practical Scala </li></ul>
  8. 8. About us <ul><li>Alf Kristian Støyle and Fredrik Vraalsen
  9. 9. Java developers with 6 and 9 years experience
  10. 10. Scala enthusiasts since 2008
  11. 11. Developed SubmitIT for JavaZone
  12. 12. Alf worked on Scala application for Kommuneforlaget
  13. 13. Held 4 Scala training courses for 60+ participants </li></ul>
  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 )
  15. 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)
  16. 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 } } }
  17. 18. Scala <ul><li>Object oriented and functional
  18. 19. Statically typed
  19. 20. Java compatible </li><ul><li>Compiles to Java bytecode
  20. 21. Existing libraries/frameworks </li></ul><li>Better Java </li></ul>
  21. 22. ;
  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; } }
  23. 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 } }
  24. 25. Variables var i : Int = 42
  25. 26. Variables var i = 42 i = 3 i = &quot;Hello world!&quot; // compile error
  26. 27. Values val i = 42 i = 3 // compile error
  27. 28. Methods def sum ( a : Int , b : Int ): Int = { return a + b }
  28. 29. Methods def sum ( a : Int , b : Int ): Int = { a + b }
  29. 30. Methods def sum ( a : Int , b : Int ) = { a + b }
  30. 31. Methods def sum ( a : Int , b : Int ) = a + b
  31. 32. Methods def sayHello ( name : String ) { println ( &quot;Hello, &quot; + name ) }
  32. 33. Methods &quot;apple&quot; . charAt (0) &quot;apple&quot; charAt 0 1.0.+(2.0) 1.0 + 2.0
  33. 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
  34. 35. myObject match { case 1 => println ( &quot;First&quot; ) case 2 => println ( &quot;Second&quot; ) case _ => println ( &quot;Unknown&quot; ) } Pattern matching
  35. 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
  36. 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
  37. 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
  38. 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:
  39. 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:
  40. 41. Functions val numbers = List (1, 2, 3, 4) val evenNums = numbers . filter (( i : Int ) => i % 2 == 0) => List(2, 4) Scala collections:
  41. 42. Functions val numbers = List (1, 2, 3, 4) val evenNums = numbers . filter ( i => i % 2 == 0) => List(2, 4) Scala collections:
  42. 43. Functions val numbers = List (1, 2, 3, 4) val evenNums = numbers . filter (_ % 2 == 0) => List(2, 4) Scala collections:
  43. 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:
  44. 45. Functions #boolean(int) even = #(int i)(i % 2 == 0) JDK7 lambda expressions: val even = ( i : Int ) => i % 2 == 0 Scala functions:
  45. 46. Functions Iterables.filter(numbers, #(int i)(i % 2 == 0)) JDK7 lambda expressions: numbers . filter (_ % 2 == 0) Scala functions:
  46. 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 { … }
  47. 48. Classes and constructors class Person ( val age : Int )
  48. 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 }
  49. 50. Traits (= Interface + Mixin) <ul><li>“Multiple inheritance done right”
  50. 51. Implement methods
  51. 52. Initialized fields
  52. 53. Abstract methods and fields
  53. 54. Does not support constructors
  54. 55. Call to super -> strict semantics </li></ul>
  55. 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 }
  56. 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
  57. 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
  58. 59. Just scratching the surface... <ul><li>Tuples
  59. 60. Singleton objects
  60. 61. Closures
  61. 62. For-comprehensions
  62. 63. Implicit conversions
  63. 64. Actors
  64. 65. Native XML data types
  65. 66. Parsers </li></ul>
  66. 67. Q & A
  67. 68. val xml = <break time=&quot;15&quot; />
  68. 69. Practical Scala <ul><li>The downside
  69. 70. Tools
  70. 71. ScalaTest
  71. 72. Java interoperability
  72. 73. Learning curve
  73. 74. Your choice </li><ul><li>Functional programming
  74. 75. Implicit conversions
  75. 76. Higher order functions </li></ul></ul>
  76. 77. IDE support <ul><li>Netbeans - very good, but inferior in other areas
  77. 78. IntelliJ IDEA - very good, but slow compilation
  78. 79. Eclipse - bad, but very fast when working </li></ul>
  79. 80. Backward compatibility <ul><li>Binary compatibility broken several times
  80. 81. Think twice before using a library </li></ul>
  81. 82. Tools demo <ul><li>REPL - Read eval print loop
  82. 83. Maven
  83. 84. SBT - Simple Build Tool
  84. 85. IntelliJ IDEA </li></ul>
  85. 86. JRebel <ul><li>&quot;Complete Java projects 10-23 % faster&quot;
  86. 87. Hotswap on steroids
  87. 88. Commercial, but free for Scala </li></ul>
  88. 89. ScalaTest demo <ul><li>DSL for testing
  89. 90. Powerful assertions
  90. 91. Different test styles </li><ul><li>JUnit
  91. 92. Functional tests
  92. 93. BDD test </li></ul></ul>
  93. 94. The feel of Scala http://parleys.com/#id=10&st=5&sl=1
  94. 95. Java interoperability demo <ul><li>Scala using Java
  95. 96. Java using Scala
  96. 97. Mixed projects? </li></ul>
  97. 98. Learning curve <ul><li>Syntax small smack in the face
  98. 99. Easy to write Java-ish Scala
  99. 100. The language scales with your understanding
  100. 101. Gradually migrate to more functional style </li></ul>
  101. 102. What would you like to hear? <ul><li>Functional programming </li><ul><li>What is it all about? </li></ul><li>Implicit conversion </li><ul><li>Why does &quot;gnirtS.gnal.avaj&quot;.reverse work? </li></ul><li>Higher order functions </li><ul><li>How does the using/resource handling example work? </li></ul></ul>
  102. 103. “Functional programming” <ul><li>First class functions
  103. 104. Pattern matching
  104. 105. Higher order functions </li></ul>
  105. 106. Functional programming <ul><li>Mathematical functions have no side effects
  106. 107. f(x) = 2x + 3 </li></ul>
  107. 108. In practice <ul><li>Only immutable objects (and object graphs) </li><ul><li>All field must be immutable
  108. 109. No side-effects from method calls
  109. 110. All methods must return something </li></ul></ul>
  110. 111. Example scala.List <ul><li>head :: tail </li></ul>val list = List (1, 2) val myList = 1 :: 2 :: Nil // 0 :: 1 :: 2 :: Nil val myotherList = 0 :: myList
  111. 112. scala.collection.immutable <ul><li>...or scala.collection.mutable
  112. 113. Pick your poison! </li></ul>
  113. 114. Strive to be pure <ul><li>Concurrency
  114. 115. Easier to avoid errors
  115. 116. Easier to test </li></ul>
  116. 117. We have a lot to learn
  117. 118. Q & A
  118. 119. Resources <ul><li>scala@java.no (http://lister.java.no/mailman/listinfo/scala)
  119. 120. http://scala.java.no
  120. 121. http://www.slideshare.net/stoyle/scala-intro
  121. 122. http://github.com/stoyle/javaBin-demo
  122. 123. fredrik@vraalsen.no, alf.kristian@gmail.com </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×