0
Beginning Scala David Pollak Silicon Valley Code Camp October 3rd, 2009
David Pollak <ul><li>Not strict, but pretty lazy
Lead developer for  Lift  web framework
Scala since November 2006, Ruby/Rails, Java/J2EE
Spreadsheet junky (writing more than using)
Wrote  Beginning Scala </li></ul>
Scala <ul><li>Fast, Compatible, JVM language
Superset of Java's object model </li><ul><li>Everything that Java's got plus better type system
100% compatible with Java objects
Everything  is an object (or a method or new) </li></ul><li>Functional </li><ul><li>Immutable data structures
Type inferencing
Super-easy to pass functions as parameters </li></ul></ul>
Scala in the Wild
At the command line
Collections <ul><li>List[T]  </li><ul><li>Immutable
Linked List of type T (String, Int, FooClass)
Very efficient for small collections </li></ul><li>Array[T] </li><ul><li>Mutable
Thin veneer on JVM Array
Upcoming SlideShare
Loading in...5
×

Beginning Scala Svcc 2009

1,218

Published on

David Pollak presents Beginning Scala at Silicon Valley Code Camp 2009

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

No Downloads
Views
Total Views
1,218
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
33
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Beginning Scala Svcc 2009"

  1. 1. Beginning Scala David Pollak Silicon Valley Code Camp October 3rd, 2009
  2. 2. David Pollak <ul><li>Not strict, but pretty lazy
  3. 3. Lead developer for Lift web framework
  4. 4. Scala since November 2006, Ruby/Rails, Java/J2EE
  5. 5. Spreadsheet junky (writing more than using)
  6. 6. Wrote Beginning Scala </li></ul>
  7. 7. Scala <ul><li>Fast, Compatible, JVM language
  8. 8. Superset of Java's object model </li><ul><li>Everything that Java's got plus better type system
  9. 9. 100% compatible with Java objects
  10. 10. Everything is an object (or a method or new) </li></ul><li>Functional </li><ul><li>Immutable data structures
  11. 11. Type inferencing
  12. 12. Super-easy to pass functions as parameters </li></ul></ul>
  13. 13. Scala in the Wild
  14. 14. At the command line
  15. 15. Collections <ul><li>List[T] </li><ul><li>Immutable
  16. 16. Linked List of type T (String, Int, FooClass)
  17. 17. Very efficient for small collections </li></ul><li>Array[T] </li><ul><li>Mutable
  18. 18. Thin veneer on JVM Array
  19. 19. Lots of methods (map, filter, toList, etc.) </li></ul></ul>
  20. 20. Collections
  21. 21. map & filter <ul><li>map </li><ul><li>Apply a function to each element in original
  22. 22. Return a new collection containing the result
  23. 23. List(1,2,3).map(_ + 1) // List(2,3,4) </li></ul><li>filter </li><ul><li>Test each element against a function that returns Boolean
  24. 24. Return a new collection containing items that passed </li></ul></ul>
  25. 25. map & filter
  26. 26. flatMap <ul><li>flatMap </li><ul><li>Flattens the collection the function returns
  27. 27. Useful for nesting operations on collections
  28. 28. &quot;99 red balloons&quot;.toList List[Char] = List(9, 9, , r, e,...)
  29. 29. List(&quot;99 red balloons&quot;, &quot;88 lines&quot;, &quot;44 women&quot;). flatMap(s => s.filter(Character.isDigit)) List[Char] = List(9, 9, 8, 8, 4, 4) </li></ul></ul>
  30. 30. foldLeft & reduceLeft <ul><li>Perform an operation on adjacent members of a collection
  31. 31. Good for: sum, product, etc.
  32. 32. List(1,2,3,4).reduceLeft(_ + _) Int = 10
  33. 33. def sq(in: Int) = in * in def sum(in: List[Int]) = in.foldLeft(0)(_ + _) def sumSq(in: List[Int]) = sum(in.map(sq)) def sqSum(in: List[Int]) = sq(sum(in)) </li></ul>
  34. 34. Parser Calculator <ul><li>object Calc extends JavaTokenParsers { def expr = term ~ rep(&quot;+&quot; ~ term | &quot;-&quot; ~ term) def term = factor ~ rep(&quot;*&quot; ~ factor | &quot;/&quot; ~ factor) def factor = floatingPointNumber | &quot;(&quot; ~ expr ~ &quot;)&quot; } </li></ul>
  35. 35. Parser Calculator <ul><li>import scala.util.parsing.combinator._ object Calc extends JavaTokenParsers { def expr = term ~ rep(&quot;+&quot; ~ term | &quot;-&quot; ~ term) ^^ { case v ~ f => f.foldLeft(v) { case (x, &quot;+&quot; ~ term) => x + term case (x, &quot;-&quot; ~ term) => x – term }} def term = factor ~ rep(&quot;*&quot;~factor | &quot;/&quot;~factor) ^^ { case v ~ f => f.foldLeft(v) { case (x, &quot;*&quot; ~ term) => x * term case (x, &quot;/&quot; ~ term) => x / term }} def factor: Parser[Double] = (floatingPointNumber ^^ (_.toDouble)) | &quot;(&quot; ~> expr <~ &quot;)&quot; } </li></ul>
  36. 36. Calculator in Action
  37. 37. Domain Specific Languages <ul><li>User.find(By(email, ” [email_address] ”), OrderBy(signupDate, Descending)) Type Safe
  38. 38. SetValById(inputName, &quot;&quot;) & SetValById(hiddenName, &quot;&quot;) & SetElemById(clrLinkName, &quot;none&quot;, &quot;style.display&quot;)
  39. 39. <button onclick={ jsonCall(&quot;submit&quot;, JsArray(ValById(inputName), ValById(hiddenName)))}>Yep</button> </li></ul>
  40. 40. DSL definitions <ul><li>This separates the library consumers from the architects
  41. 41. def find(by: QueryParam[A]*) abstract class QueryParam[A<:Mapper[A]] case class Cmp[A<:Mapper[A], T](...) extends QueryParam[A] object By { import OprEnum._ def apply[O <: Mapper[O], T, U <% T](...) = Cmp[O,T](field, Eql, Full(value), Empty) </li></ul>
  42. 42. DSL definitions <ul><li>case class SetValById(id: String, right: JsExp) extends JsCmd { def toJsCmd = &quot;document.getElementById(&quot;+id.encJs+&quot;).value = &quot;+ right.toJsCmd+&quot;;&quot;}
  43. 43. trait JsCmd extends HtmlFixer with ToJsCmd { def &(other: JsCmd): JsCmd = JsCmds.CmdPair(this, other) def toJsCmd: String } </li></ul>
  44. 44. Questions? <ul><li>Thanks for participating! </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.

×