Rewriting Java In Scala

  • 2,541 views
Uploaded on

Rewriting Java In Scala

Rewriting Java In Scala

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • 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
    Are you sure you want to
    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
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
2,541
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
0
Comments
2
Likes
6

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • I like this. You're not testing that you can call your own API correctly... you're testing that some expression evaluates to some value. It's clear what the test is checking and it's easy to check the test is correct (assuming the internal calls are correct).
  • I like this. You're not testing that you can call your own API correctly... you're testing that some expression evaluates to some value. It's clear what the test is checking and it's easy to check the test is correct (assuming the internal calls are correct).
  • I like this. You're not testing that you can call your own API correctly... you're testing that some expression evaluates to some value. It's clear what the test is checking and it's easy to check the test is correct (assuming the internal calls are correct).

Transcript

  • 1. Rewriting Java in Scala ...and Making Your Code Lovely
  • 2. Relevant About Me New to Scala – Summer 2009 http://www.colinhowe.co.uk http://twitter.com/colinhowe
  • 3. Format
    • Try to keep this as interactive as possible.
    • Introduce a concept
    • Use the concept on some Java
    • Blue is used to highlight the essence
  • 4. Case Classes (1)
    • Provide pattern matching
    • Default toString, equals, hashCode
  • 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 "Point(x: " + x + ", y: " + y + ")"; }
  • 6. Case Classes Example - Scala case class Point(x : Int, y : Int)
  • 7. Filter Take a list of X and find only certain X E.g. find all even numbers in a list
  • 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. Filter Example - Java List<Integer> evens = new LinkedList<Integer>(); for (Integer i : list ) { if ( i % 2 == 0 ) evens.add(i); }
  • 10. Filter Example - Scala val evens = list .filter( _ % 2 == 0 )
  • 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. 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. 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. Map Take a list of X and turn it into a list of Y Lists are the same size
  • 15. Map Example (1) - Java for (int i : list ) { result.add( i * 2 ); }
  • 16. Map Example (1) - Scala val result = list .map( _ * 2 )
  • 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. 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. 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. 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. Map Example (2) - Scala val result = list.map(factorise)
  • 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. Fold Example (1) - Java int total = 0 ; for (int i : someList ) { total += i ; }
  • 24. Fold Example (1) - Scala val sum = someList .foldLeft( 0 )( _ + _ )
  • 25. Fold Example (2) - Java int successCount = 0 ; for (Message m : messages ) { successCount += process(m) }
  • 26. Fold Example (2) - Scala val successCount = messages.foldLeft( 0 )( _ + process(_) )
  • 27. Spot Common Constructs Spot re-used constructs Replace with a more functional construct Try to extract out algorithms and data
  • 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. 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. Generators For loops with multiple iterators for (x <- 1 to 10; y <- 1 to 20)
  • 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. Generators Example (1) - Scala val xys = for (x <- 1 to 10; y <- x + 1 to 10) yield (x, y)
  • 33. Generators Example (2) - Java for (Message message : messages ) { if ( message.isHighPriority() ) { processImmediately(message); } }
  • 34. Generators Example (2) - Scala for (message <- messages ; if message.isHighPriority ) { processImmediately(message) }
  • 35. Exception Handling Not as forced – no checked exceptions
  • 36. Exceptions Example - Java final String line; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } // Do stuff with the line
  • 37. Exceptions Example - Scala val line = reader.readLine() // Do stuff with the line
  • 38. Exceptions Example - Scala try { val line = reader.readLine() // Do stuff with the line } catch { case e : Exception => println(e) }
  • 39. Reduce Fold but without a 0 th value Does not work on the empty list
  • 40. Reduce Example (1) - Java int currentMax = someList.get(0) ; for (int i : someList ) { if (i > currentMax) { currentMax = i ; } }
  • 41. Reduce Example (1) - Scala val currentMax = someList .reduceLeft(0)( max )
  • 42. Random Tricks Slight break from the format. Covering a few random things that have no proper equivalent in Java.
  • 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. Testing Way more fun in Scala. Make your own DSLs :)
  • 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. Some Guidelines
  • 47. Some Guidelines Write small functions... … do pass around functions
  • 48. Some Guidelines Abstract out algorithms
  • 49. Some Guidelines Write small classes / traits
  • 50. Some Guidelines If you see unwanted state... … you can probably remove it
  • 51. Some Guidelines Immutability is King
  • 52. Questions :)
  • 53. Raffle! Fill in your evaluation forms Prize: free ticket to the Scala Life-off in September!