Beginning Scala Svcc 2009
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Beginning Scala Svcc 2009

  • 1,699 views
Uploaded on

David Pollak presents Beginning Scala at Silicon Valley Code Camp 2009

David Pollak presents Beginning Scala at Silicon Valley Code Camp 2009

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,699
On Slideshare
1,694
From Embeds
5
Number of Embeds
1

Actions

Shares
Downloads
31
Comments
0
Likes
2

Embeds 5

http://squadzik.blogspot.com 5

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

Transcript

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