Your SlideShare is downloading. ×
Rewriting Java in Scala ...and Making Your Code Lovely
Relevant About Me New to Scala – Summer 2009 http://www.colinhowe.co.uk http://twitter.com/colinhowe
Format <ul><li>Try to keep this as interactive as possible. </li></ul><ul><li>Introduce a concept </li></ul><ul><li>Use th...
Case Classes (1) <ul><li>Provide pattern matching </li></ul><ul><li>Default toString, equals, hashCode </li></ul>
Case Classes Example - Java @Override public boolean equals(Object obj) { if (!(obj instanceof Point)) { return false; } P...
Case Classes Example - Scala case  class Point(x : Int, y : Int)
Filter Take a list of X and find only certain X E.g. find all even numbers in a list
Filter Example - Java Predicate evenNumbers = new Predicate() { @Override public boolean evaluate(Object o) { Integer n = ...
Filter Example - Java List<Integer> evens = new LinkedList<Integer>(); for (Integer i :  list ) { if ( i % 2 == 0 ) evens....
Filter Example - Scala val evens =  list .filter( _ % 2 == 0 )
Pattern Matching Think switch on steroids result match { case &quot;A&quot; | &quot;B&quot; => println(&quot;A or B!&quot;...
Matching Example - Java @Override public boolean equals(Object obj) { if ( !(obj instanceof Point) ) { return false; } Poi...
Matching Example - Scala override def equals(o : Any) : Boolean = o match { case  p : Point => p.x == this.x && p.y == thi...
Map Take a list of X and turn it into a list of Y Lists are the same size
Map Example (1) - Java for (int i :  list ) { result.add( i * 2 ); }
Map Example (1) - Scala val result =  list .map( _ * 2 )
Map Example (2) - Java for (int n : list) { List<Integer> factors = new LinkedList<Integer>(); int p = 2; while (p <= n) {...
Map Example (2) - Scala def factorise(n : Int) : List[Int] = { val factors = scala.collection.mutable.ListBuffer[Int]() va...
Map Example (2) - Scala def factorise(n : Int, p : Int) : List[Int] = { if (n == 1) Nil else if (n % p == 0) p :: factoris...
Map Example (2) - Scala def factorise(n : Int, p : Int, factors : List[Int]) : List[Int] = { if (n == 1) factors else if (...
Map Example (2) - Scala val result = list.map(factorise)
Fold Take a list of X and turn it into a single Y by combining each element in the list left = do the operation on the lef...
Fold Example (1) - Java int total =  0 ; for (int i :  someList ) { total  += i ; }
Fold Example (1) - Scala val sum =  someList .foldLeft( 0 )( _ + _ )
Fold Example (2) - Java int successCount =  0 ; for (Message m :  messages ) { successCount += process(m) }
Fold Example (2) - Scala val successCount = messages.foldLeft( 0 )( _ + process(_) )
Spot Common Constructs Spot re-used constructs Replace with a more functional construct Try to extract out algorithms and ...
Common Constructs - Java for (Car car : cars) { if (!groups.containsKey(car.colour)) { groups.put(car.colour, new HashSet<...
Common Constructs - Scala def group[T, K](list : List[T], key : (T => K)) : Map[K, Set[T]] = { val groups = scala.collecti...
Generators For loops with multiple iterators for (x <- 1 to 10; y <- 1 to 20)
Generators Example (1) - Java for (int x = 1; x <= 10; x++) { for (int y = x + 1; y <= 10; y++) { list.add(new Pair(x, y))...
Generators Example (1) - Scala val xys =  for (x <- 1 to 10; y <- x + 1 to 10)  yield (x, y)
Generators Example (2) - Java for (Message message :  messages ) { if ( message.isHighPriority() ) { processImmediately(me...
Generators Example (2) - Scala for (message <-  messages ; if  message.isHighPriority ) { processImmediately(message) }
Exception Handling Not as forced – no checked exceptions
Exceptions Example - Java final String line; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeEx...
Exceptions Example - Scala val line = reader.readLine() // Do stuff with the line
Exceptions Example - Scala try { val line = reader.readLine() // Do stuff with the line } catch { case e : Exception => pr...
Reduce Fold but without a 0 th  value Does not work on the empty list
Reduce Example (1) - Java int currentMax =  someList.get(0) ; for (int i :  someList ) { if (i > currentMax)  { currentMax...
Reduce Example (1) - Scala val currentMax =  someList .reduceLeft(0)( max )
Random Tricks Slight break from the format. Covering a few random things that have no proper equivalent in Java.
Releasing Resources withBufferedReader(new File(&quot;readme&quot;)) { reader => reader.readLine } def withBufferedReader(...
Testing Way more fun in Scala. Make your own DSLs :)
Testing DSL Example def testSimpleAddition { &quot;1 + 1&quot; evaluates 2 &quot;2 + 3 + 4&quot; evaluates 9 } implicit de...
Some Guidelines
Some Guidelines Write small functions... …  do pass around functions
Some Guidelines Abstract out algorithms
Some Guidelines Write small classes / traits
Some Guidelines If you see unwanted state... …  you can probably remove it
Some Guidelines Immutability is King
Questions :)
Raffle! Fill in your evaluation forms Prize: free ticket to the Scala Life-off in September!
Upcoming SlideShare
Loading in...5
×

Rewriting Java In Scala

2,622

Published on

Rewriting Java In Scala

Published in: Technology, News & Politics
2 Comments
6 Likes
Statistics
Notes
  • Martin Odersky will give a Scala Workshop at Skills Matter in London on June 9-10th. Just 20 seats available... More information here: http://skillsmatter.com/course/java-jee/bject-oriented-meets-functional-an-exploaration-of-scala/wd-734
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • You can watch the video of this presentation here: http://skillsmatter.com/podcast/java-jee/re-writing-java-classes-in-scala-and-making-your-code-lovely/rl-311
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
2,622
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
2
Likes
6
Embeds 0
No embeds

No notes for slide
  • I like this. You&apos;re not testing that you can call your own API correctly... you&apos;re testing that some expression evaluates to some value. It&apos;s clear what the test is checking and it&apos;s easy to check the test is correct (assuming the internal calls are correct).
  • I like this. You&apos;re not testing that you can call your own API correctly... you&apos;re testing that some expression evaluates to some value. It&apos;s clear what the test is checking and it&apos;s easy to check the test is correct (assuming the internal calls are correct).
  • I like this. You&apos;re not testing that you can call your own API correctly... you&apos;re testing that some expression evaluates to some value. It&apos;s clear what the test is checking and it&apos;s easy to check the test is correct (assuming the internal calls are correct).
  • Transcript of "Rewriting Java In Scala"

    1. 1. Rewriting Java in Scala ...and Making Your Code Lovely
    2. 2. Relevant About Me New to Scala – Summer 2009 http://www.colinhowe.co.uk http://twitter.com/colinhowe
    3. 3. Format <ul><li>Try to keep this as interactive as possible. </li></ul><ul><li>Introduce a concept </li></ul><ul><li>Use the concept on some Java </li></ul><ul><li>Blue is used to highlight the essence </li></ul>
    4. 4. Case Classes (1) <ul><li>Provide pattern matching </li></ul><ul><li>Default toString, equals, hashCode </li></ul>
    5. 5. Case Classes Example - Java @Override public boolean equals(Object obj) { if (!(obj instanceof Point)) { return false; } Point other = (Point)obj; return other.x == x && other.y == y; } @Override public int hashCode() { return x * 17 + y; } } class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public String toString() { return &quot;Point(x: &quot; + x + &quot;, y: &quot; + y + &quot;)&quot;; }
    6. 6. Case Classes Example - Scala case class Point(x : Int, y : Int)
    7. 7. Filter Take a list of X and find only certain X E.g. find all even numbers in a list
    8. 8. Filter Example - Java Predicate evenNumbers = new Predicate() { @Override public boolean evaluate(Object o) { Integer n = (Integer)o; // Assume list contains only integers return n % 2 == 0 ; } }; CollectionUtils.filter( list , evenNumbers);
    9. 9. Filter Example - Java List<Integer> evens = new LinkedList<Integer>(); for (Integer i : list ) { if ( i % 2 == 0 ) evens.add(i); }
    10. 10. Filter Example - Scala val evens = list .filter( _ % 2 == 0 )
    11. 11. Pattern Matching Think switch on steroids result match { case &quot;A&quot; | &quot;B&quot; => println(&quot;A or B!&quot;) case &quot;C&quot; => println(&quot;Just C.&quot;) case e : Exception => println(&quot;Broken: &quot; + e) case _ => println(&quot;Anything else!&quot;) }
    12. 12. Matching Example - Java @Override public boolean equals(Object obj) { if ( !(obj instanceof Point) ) { return false; } Point other = (Point)obj; return other.x == x && other.y == y ; }
    13. 13. Matching Example - Scala override def equals(o : Any) : Boolean = o match { case p : Point => p.x == this.x && p.y == this.y case _ => false }
    14. 14. Map Take a list of X and turn it into a list of Y Lists are the same size
    15. 15. Map Example (1) - Java for (int i : list ) { result.add( i * 2 ); }
    16. 16. Map Example (1) - Scala val result = list .map( _ * 2 )
    17. 17. Map Example (2) - Java for (int n : list) { List<Integer> factors = new LinkedList<Integer>(); int p = 2; while (p <= n) { if (n % p == 0) { factors.add(p); n /= p; } else { p++; } } result.add(factors); }
    18. 18. Map Example (2) - Scala def factorise(n : Int) : List[Int] = { val factors = scala.collection.mutable.ListBuffer[Int]() var currentValue = n var p = 2 while (p <= currentValue) { if (currentValue % p == 0) { factors += p currentValue = currentValue / p } else { p = p + 1 } } factors.toList }
    19. 19. Map Example (2) - Scala def factorise(n : Int, p : Int) : List[Int] = { if (n == 1) Nil else if (n % p == 0) p :: factorise(n / p, p) else factorise(n, p + 1) } def factorise(n : Int) : List[Int] = factorise(n, 2)
    20. 20. Map Example (2) - Scala def factorise(n : Int, p : Int, factors : List[Int]) : List[Int] = { if (n == 1) factors else if (n % p == 0) factorise(n / p, p, p :: factors) else factorise(n, p + 1, factors) } def factorise(n : Int) : List[Int] = factorise(n, 2, Nil)
    21. 21. Map Example (2) - Scala val result = list.map(factorise)
    22. 22. Fold Take a list of X and turn it into a single Y by combining each element in the list left = do the operation on the left-most element first
    23. 23. Fold Example (1) - Java int total = 0 ; for (int i : someList ) { total += i ; }
    24. 24. Fold Example (1) - Scala val sum = someList .foldLeft( 0 )( _ + _ )
    25. 25. Fold Example (2) - Java int successCount = 0 ; for (Message m : messages ) { successCount += process(m) }
    26. 26. Fold Example (2) - Scala val successCount = messages.foldLeft( 0 )( _ + process(_) )
    27. 27. Spot Common Constructs Spot re-used constructs Replace with a more functional construct Try to extract out algorithms and data
    28. 28. Common Constructs - Java for (Car car : cars) { if (!groups.containsKey(car.colour)) { groups.put(car.colour, new HashSet<Car>()); } groups.get(car.colour).add(car); }
    29. 29. Common Constructs - Scala def group[T, K](list : List[T], key : (T => K)) : Map[K, Set[T]] = { val groups = scala.collection.mutable.Map[K, Set[T]]() for (t <- list) { if (!groups.contains(key(t))) { groups(key(t)) = Set[T]() } groups(key(t)) = Set[T](t) ++ groups(key(t)) } groups.toMap } val carGroups = group(cars, ((_:car).colour))
    30. 30. Generators For loops with multiple iterators for (x <- 1 to 10; y <- 1 to 20)
    31. 31. Generators Example (1) - Java for (int x = 1; x <= 10; x++) { for (int y = x + 1; y <= 10; y++) { list.add(new Pair(x, y)); } }
    32. 32. Generators Example (1) - Scala val xys = for (x <- 1 to 10; y <- x + 1 to 10) yield (x, y)
    33. 33. Generators Example (2) - Java for (Message message : messages ) { if ( message.isHighPriority() ) { processImmediately(message); } }
    34. 34. Generators Example (2) - Scala for (message <- messages ; if message.isHighPriority ) { processImmediately(message) }
    35. 35. Exception Handling Not as forced – no checked exceptions
    36. 36. Exceptions Example - Java final String line; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } // Do stuff with the line
    37. 37. Exceptions Example - Scala val line = reader.readLine() // Do stuff with the line
    38. 38. Exceptions Example - Scala try { val line = reader.readLine() // Do stuff with the line } catch { case e : Exception => println(e) }
    39. 39. Reduce Fold but without a 0 th value Does not work on the empty list
    40. 40. Reduce Example (1) - Java int currentMax = someList.get(0) ; for (int i : someList ) { if (i > currentMax) { currentMax = i ; } }
    41. 41. Reduce Example (1) - Scala val currentMax = someList .reduceLeft(0)( max )
    42. 42. Random Tricks Slight break from the format. Covering a few random things that have no proper equivalent in Java.
    43. 43. Releasing Resources withBufferedReader(new File(&quot;readme&quot;)) { reader => reader.readLine } def withBufferedReader(file : File)(op : BufferedReader => Unit) = { val reader = new BufferedReader(new FileReader(file)) try { op(reader) } finally { reader.close() } }
    44. 44. Testing Way more fun in Scala. Make your own DSLs :)
    45. 45. Testing DSL Example def testSimpleAddition { &quot;1 + 1&quot; evaluates 2 &quot;2 + 3 + 4&quot; evaluates 9 } implicit def evaluates(source : String) = new { def evaluates(value : Int) { // Invoke the expression evaluator and check the value } }
    46. 46. Some Guidelines
    47. 47. Some Guidelines Write small functions... … do pass around functions
    48. 48. Some Guidelines Abstract out algorithms
    49. 49. Some Guidelines Write small classes / traits
    50. 50. Some Guidelines If you see unwanted state... … you can probably remove it
    51. 51. Some Guidelines Immutability is King
    52. 52. Questions :)
    53. 53. Raffle! Fill in your evaluation forms Prize: free ticket to the Scala Life-off in September!

    ×