Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Rewriting Java In Scala

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige

Hier ansehen

1 von 53 Anzeige
Anzeige

Weitere Verwandte Inhalte

Diashows für Sie (20)

Andere mochten auch (20)

Anzeige

Ähnlich wie Rewriting Java In Scala (20)

Weitere von Skills Matter (20)

Anzeige

Aktuellste (20)

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!

Hinweis der Redaktion

  • 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).

×