2. Relevant About Me New to Scala – Summer 2009 http://www.colinhowe.co.uk http://twitter.com/colinhowe
3.
4.
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 + ")"; }
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); }
11. Pattern Matching Think switch on steroids result match { case "A" | "B" => println("A or B!") case "C" => println("Just C.") case e : Exception => println("Broken: " + e) case _ => println("Anything else!") }
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 ); }
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)
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))
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.
53. Raffle! Fill in your evaluation forms Prize: free ticket to the Scala Life-off in September!
Hinweis der Redaktion
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).