Scala + WattzOn, sitting in a tree....

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    3 Favorites

    Scala + WattzOn, sitting in a tree.... - Presentation Transcript

    1. Scala and WattzOn, Sitting in a Tree... Bay Area Scala Enthusiasts March 10, 2009
    2. Who are we? Jeremy Cloud Raffi Krikorian
    3. Who is Synthesis? • Synthesis is a software and strategy consultancy • Cambridge, MA • 7 people
    4. What is WattzOn? End User Perspective • WattzOn is a free online tool to quantify, track, compare and understand the total amount of energy needed to support all of the facets of your lifestyle
    5. My Lifestyle: 14,504 Watts
    6. What is WattzOn? Developer Perspective scala-orm WattzOn scala-utils Holmz swag
    7. What is Holmz? • Object oriented “spread-sheet” • Hierarchies of objects with properties • Some properties are computed from formulas over other objects and properties • Sophisticated formula language (DSL) • Intended as back-end for other systems
    8. Holmz Understands Units • Define a property as being “meters/second” • Insert a value in “smoots/year”, and it will auto convert • (5 [gal] * 50[MJ/L] / 4[h]) @ [W]
    9. Holmz DSL Properties can be defined as simple algebraic expressions over other properties within the same object mpg = miles / gallon
    10. Holmz DSL Properties can be defined as a reduction of properties on other objects stuff_watts = for s in creator.worksheets.profile_stuff select sum(s.watts) default 0[W]
    11. Holmz DSL Formulas can contain extractor expressions for decomposing other objects component_rollup = for [count, comp] in components let [trans_watts, _*] = item_to_country(comp, made_in) select sum(count * (comp.watts + trans_watts)), sum(count * comp.mass), sum(count * comp.disposal_watts) default 0[W], 0[kg], 0[W]
    12. Parser Combinators def cond = (or ~ opt(\"?\" ~> expr ~ (\":\" ~> expr))) ^^ { case c ~ None => c case c ~ Some(t ~ e) => Conditional(c, t, e) } def or = chainl1(and, \"||\" ~> and, success{(a:AST, b:AST)=>Or(a,b)}) def and = chainl1(rel, \"&&\" ~> rel, success{(a:AST, b:AST)=>And(a,b)})
    13. Parser Combinators • At first, appears to be line noise • Terse • Reasonably quick to write, once you understand them • Gets a bit hairy for complex grammars • Error handling is difficult • Performance is so-so
    14. Handwritten Parser • About 6x faster • About 6x the number of lines of code • Better error reporting
    15. Handwritten Parser def cond = (or ~ opt(\"?\" ~> expr ~ (\":\" ~> expr))) ^^ { case c ~ None => c case c ~ Some(t ~ e) => Conditional(c, t, e) } def cond = { ts.foldOpt(or) { case (c, SyntaxToken(\"?\", _)) => val t = expr ts.syntax(\":\") val e = expr Conditional(c, t, e) } }
    16. Handwritten Parser def or = chainl1(and, \"||\" ~> and, success{(a:AST, b:AST)=>Or(a,b)}) def or = { ts.fold(and) { case (left, SyntaxToken(\"||\", _)) => Or(left, and) } }
    17. Parser Combinators, To use or not to use • Complexity of grammar • Performance sensitivity • Error reporting • Readability a wash
    18. What is WattzOn? Developer Perspective scala-orm WattzOn scala-utils Holmz swag
    19. What is scala-orm? • native scala O/R mapping layer • lightweight and fast • XML schema in, generated scala code out • unimaginative name
    20. scala-orm schema <table name=\"EnumValue\"> <field name=\"version\" type=\"int\"/> <field name=\"enumType\" type=\"EnumType\"/> <field name=\"key\" type=\"string\"/> <field name=\"displayName\" type=\"string\"/> <field name=\"description\" type=\"string\" size=\"0,65535\"/> <field name=\"creator\" type=\"User\"/> <index fields=\"enumType, key\" unique=\"true\"/> </table>
    21. scala-orm output • data object class • data accessor object • low overhead read, write, delete methods • no reflection
    22. scala idioms • def get(id: ID)(implicit session: SdbSession): Option[T] • def getAll(ids: ID*)(implicit session: SdbSession): Stream[T]
    23. dynamic query builder def getRecentlyAnswered(n: Int): JList[WorksheetAnswers] = { DomainObject.query.where(\"domain\" -> domain). innerJoin(\"creator\", \"confirmed\" -> true). orderBy(\"createdAt\" -> false). limit(n).query.all.map(getOrUpdateAnswers) }
    24. What is WattzOn? Developer Perspective scala-orm WattzOn scala-utils Holmz swag
    25. What is scala-utils? • miscellaneous utility classes • JavaBridge
    26. JavaBridge • Interacting with java collections is cumbersome in scala
    27. Java Lists w/o JavaBridge Feels like Java 1.3 or earlier import java.util.{List => JList} def painful(jlist: JList[String]) = { val iter = jlist.iterator while (iter.hasNext) { val elt = iter.next ... }
    28. JavaBridge import java.lang.{Iterable=>JIterable} object JavaBridge { implicit def jit2sit[T](jit:JIterable[T]):Iterable[T] = { new Iterable[T] { def elements:Iterator[T] = new Iterator[T] { val baseIter = jit.iterator() def hasNext = baseIter.hasNext def next = baseIter.next } } } ... }
    29. Java Lists w/ JavaBridge import java.util.{List => JList} import JavaBridge._ def easy(jlist: JList[String]) = { for (elt <- jlist) { ... } }
    30. Java Maps w/o JavaBridge import java.util.{map => JMap} def painful(jmap: JMap[String,String]) = { val iter = jmap.entrySet.iterator while (iter.hasNext) { val entry = iter.next val key = entry.getKey val value = entry.getValue ... } }
    31. JavaBridge import java.util.{Map=>JMap} object JavaBridge { ... implicit def jmap2sit[K,V](jmap:JMap[K,V]):Iterable[(K,V)] = new Iterable[(K,V)] { def elements:Iterator[(K,V)] = new Iterator[(K,V)] { val baseIter = jmap.entrySet.iterator def hasNext = baseIter.hasNext def next = { val entry = baseIter.next() (entry.getKey(), entry.getValue()) } } } ... }
    32. Java Maps w/ JavaBridge import java.util.{map => JMap} import JavaBridge._ def easy(jmap: JMap[String,String]) = { for ((key, value) <- jmap) { ... } }
    33. Java Maps w/o JavaBridge import java.util.{HashMap=>JHashMap,Map=>JMap} def painful(): JMap[String,String] = { val attrs = new JHashMap[String,String] attrs.put(“firstName”, “Jeremy”) attrs.put(“lastName”, “Cloud” attrs }
    34. JavaBridge import java.collection.{HashMap=>JHashMap, Map=>JMap} object JavaBridge { ... def jmap[A, B](elems: (A, B)*): JMap[A,B] = { val map = new JHashMap[A,B](elems.size * 2) for ((k, v) <- elems) map.put(k, v) map } ... }
    35. Java Maps w/ JavaBridge import java.util.{Map=>JMap} import JavaBridge._ def easy(): JMap[String,String] = { jmap(“firstName” -> “Jeremy”, “lastName” -> “Cloud”) }
    36. Questions? • www.wattzon.com • www.synthesisstudios.com • jeremy.cloud@synthesisstudios.com • raffi.krikkorian@synthesisstudios.com

    + raffikrikorianraffikrikorian, 8 months ago

    custom

    1648 views, 3 favs, 0 embeds more stats

    A talk given to Scala enthusiasts at the Twitter HQ more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1648
      • 1648 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 3
    • Downloads 28
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories