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

R
Raffi KrikorianPlatform Team Member
Scala and WattzOn,
 Sitting in a Tree...
 Bay Area Scala Enthusiasts
         March 10, 2009
Who are we?
Jeremy Cloud   Raffi Krikorian
Who is Synthesis?

• Synthesis is a software and strategy
  consultancy
• Cambridge, MA
• 7 people
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
My Lifestyle: 14,504 Watts
What is WattzOn?
    Developer Perspective


                    scala-orm
WattzOn

                     scala-utils

 Holmz
                     swag
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
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]
Holmz DSL
Properties can be defined as simple algebraic
expressions over other properties within the
same object



     mpg = miles / gallon
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]
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]
Parser Combinators
def cond = (or ~ opt(quot;?quot; ~> expr ~ (quot;:quot; ~> expr))) ^^ {
  case c ~ None => c
  case c ~ Some(t ~ e) => Conditional(c, t, e)
}

def or = chainl1(and, quot;||quot; ~> and,
  success{(a:AST, b:AST)=>Or(a,b)})

def and = chainl1(rel, quot;&" ~> rel,
  success{(a:AST, b:AST)=>And(a,b)})
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
Handwritten Parser

• About 6x faster
• About 6x the number of lines of code
• Better error reporting
Handwritten Parser
def cond = (or ~ opt(quot;?quot; ~> expr ~ (quot;:quot; ~> expr))) ^^ {
  case c ~ None => c
  case c ~ Some(t ~ e) => Conditional(c, t, e)
}


def cond = {
  ts.foldOpt(or) {
    case (c, SyntaxToken(quot;?quot;, _)) =>
      val t = expr
      ts.syntax(quot;:quot;)
      val e = expr
      Conditional(c, t, e)
  }
}
Handwritten Parser

def or = chainl1(and, quot;||quot; ~> and,
  success{(a:AST, b:AST)=>Or(a,b)})




def or = {
  ts.fold(and) {
    case (left, SyntaxToken(quot;||quot;, _)) =>
      Or(left, and)
  }
}
Parser Combinators,
 To use or not to use
• Complexity of grammar
• Performance sensitivity
• Error reporting
• Readability a wash
What is WattzOn?
    Developer Perspective


                    scala-orm
WattzOn

                     scala-utils

 Holmz
                     swag
What is scala-orm?

• native scala O/R mapping layer
• lightweight and fast
• XML schema in, generated scala code out
• unimaginative name
scala-orm schema
<table name=quot;EnumValuequot;>
  <field name=quot;versionquot; type=quot;intquot;/>
  <field name=quot;enumTypequot; type=quot;EnumTypequot;/>
  <field name=quot;keyquot; type=quot;stringquot;/>
  <field name=quot;displayNamequot; type=quot;stringquot;/>
  <field name=quot;descriptionquot; type=quot;stringquot;
     size=quot;0,65535quot;/>
  <field name=quot;creatorquot; type=quot;Userquot;/>
  <index fields=quot;enumType, keyquot; unique=quot;truequot;/>
</table>
scala-orm output

• data object class
• data accessor object
• low overhead read, write, delete methods
• no reflection
scala idioms


•   def get(id: ID)(implicit session: SdbSession): Option[T]

•   def getAll(ids: ID*)(implicit session: SdbSession):
    Stream[T]
dynamic query builder

def getRecentlyAnswered(n: Int):
  JList[WorksheetAnswers] =
{
  DomainObject.query.where(quot;domainquot; -> domain).
    innerJoin(quot;creatorquot;, quot;confirmedquot; -> true).
    orderBy(quot;createdAtquot; -> false).
    limit(n).query.all.map(getOrUpdateAnswers)
}
What is WattzOn?
    Developer Perspective


                    scala-orm
WattzOn

                     scala-utils

 Holmz
                     swag
What is scala-utils?


• miscellaneous utility classes
• JavaBridge
JavaBridge


• Interacting with java collections is
  cumbersome in scala
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
    ...
}
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
      }
    }
  }
  ...
}
Java Lists w/ JavaBridge

import java.util.{List => JList}
import JavaBridge._

def easy(jlist: JList[String]) = {
  for (elt <- jlist) {
    ...
  }
}
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
    ...
  }
}
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())
      }
    }
  }
  ...
}
Java Maps w/ JavaBridge

import java.util.{map => JMap}
import JavaBridge._

def easy(jmap: JMap[String,String]) = {
  for ((key, value) <- jmap) {
    ...
  }
}
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
}
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
  }
  ...
}
Java Maps w/ JavaBridge

import java.util.{Map=>JMap}
import JavaBridge._

def easy(): JMap[String,String] = {
  jmap(“firstName” -> “Jeremy”,
    “lastName” -> “Cloud”)
}
Questions?

• www.wattzon.com
• www.synthesisstudios.com
• jeremy.cloud@synthesisstudios.com
• raffi.krikkorian@synthesisstudios.com
1 of 36

Recommended

Scala en by
Scala enScala en
Scala enFero Kocun
725 views66 slides
Introduction to kotlin + spring boot demo by
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoMuhammad Abdullah
113 views78 slides
Scala 2013 review by
Scala 2013 reviewScala 2013 review
Scala 2013 reviewSagie Davidovich
12.2K views76 slides
"Kotlin и rx в android" Дмитрий Воронин (Avito) by
"Kotlin и rx в android" Дмитрий Воронин  (Avito)"Kotlin и rx в android" Дмитрий Воронин  (Avito)
"Kotlin и rx в android" Дмитрий Воронин (Avito)AvitoTech
1.1K views67 slides
Polyglot JVM by
Polyglot JVMPolyglot JVM
Polyglot JVMArturo Herrero
10.4K views70 slides
​"Delegates, Delegates everywhere" Владимир Миронов by
​"Delegates, Delegates everywhere" Владимир Миронов​"Delegates, Delegates everywhere" Владимир Миронов
​"Delegates, Delegates everywhere" Владимир МироновAvitoTech
677 views86 slides

More Related Content

What's hot

Kotlin Developer Starter in Android projects by
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsBartosz Kosarzycki
5.1K views29 slides
Polyglot Programming in the JVM by
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVMAndres Almiray
1.5K views44 slides
Kotlin by
KotlinKotlin
KotlinYeldosTanikin
203 views31 slides
Functional programming in javascript by
Functional programming in javascriptFunctional programming in javascript
Functional programming in javascriptBoris Burdiliak
443 views62 slides
Programming in Scala: Notes by
Programming in Scala: NotesProgramming in Scala: Notes
Programming in Scala: NotesRoberto Casadei
2.3K views192 slides
ADG Poznań - Kotlin for Android developers by
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersBartosz Kosarzycki
2.5K views51 slides

What's hot(20)

Kotlin Developer Starter in Android projects by Bartosz Kosarzycki
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
Bartosz Kosarzycki5.1K views
Polyglot Programming in the JVM by Andres Almiray
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVM
Andres Almiray1.5K views
Functional programming in javascript by Boris Burdiliak
Functional programming in javascriptFunctional programming in javascript
Functional programming in javascript
Boris Burdiliak443 views
ADG Poznań - Kotlin for Android developers by Bartosz Kosarzycki
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
Bartosz Kosarzycki2.5K views
Develop your next app with kotlin @ AndroidMakersFr 2017 by Arnaud Giuliani
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017
Arnaud Giuliani3.2K views
(How) can we benefit from adopting scala? by Tomasz Wrobel
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel1.1K views
Swift - One step forward from Obj-C by Nissan Tsafrir
Swift -  One step forward from Obj-CSwift -  One step forward from Obj-C
Swift - One step forward from Obj-C
Nissan Tsafrir2.3K views
Java Keeps Throttling Up! by José Paumard
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!
José Paumard1.2K views
Functional Java 8 in everyday life by Andrea Iacono
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday life
Andrea Iacono4K views
An introduction to javascript by MD Sayem Ahmed
An introduction to javascriptAn introduction to javascript
An introduction to javascript
MD Sayem Ahmed1.5K views
Groovy Ast Transformations (greach) by HamletDRC
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
HamletDRC1.8K views
No JS and DartCon by anandvns
No JS and DartConNo JS and DartCon
No JS and DartCon
anandvns2K views
Kotlin hands on - MorningTech ekito 2017 by Arnaud Giuliani
Kotlin hands on - MorningTech ekito 2017Kotlin hands on - MorningTech ekito 2017
Kotlin hands on - MorningTech ekito 2017
Arnaud Giuliani476 views
Kotlin advanced - language reference for android developers by Bartosz Kosarzycki
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
Bartosz Kosarzycki5.7K views

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

Pragmatic Real-World Scala (short version) by
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
72.8K views108 slides
Pragmatic Real-World Scala by
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scalaparag978978
610 views108 slides
Groovy by
GroovyGroovy
GroovyZen Urban
1.1K views35 slides
Beginning Scala Svcc 2009 by
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009David Pollak
855 views18 slides
Domain Specific Languages In Scala Duse3 by
Domain Specific Languages In Scala Duse3Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3Peter Maas
1.4K views18 slides
Scala 2 + 2 > 4 by
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4Emil Vladev
1.4K views54 slides

Similar to Scala + WattzOn, sitting in a tree....(20)

Pragmatic Real-World Scala (short version) by Jonas Bonér
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
Jonas Bonér72.8K views
Pragmatic Real-World Scala by parag978978
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scala
parag978978610 views
Groovy by Zen Urban
GroovyGroovy
Groovy
Zen Urban1.1K views
Beginning Scala Svcc 2009 by David Pollak
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009
David Pollak855 views
Domain Specific Languages In Scala Duse3 by Peter Maas
Domain Specific Languages In Scala Duse3Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3
Peter Maas1.4K views
Scala 2 + 2 > 4 by Emil Vladev
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
Emil Vladev1.4K views
Lisp Macros in 20 Minutes (Featuring Clojure) by Phil Calçado
Lisp Macros in 20 Minutes (Featuring Clojure)Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Phil Calçado7.8K views
Scala 3camp 2011 by Scalac
Scala   3camp 2011Scala   3camp 2011
Scala 3camp 2011
Scalac941 views
Real life-coffeescript by David Furber
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
David Furber697 views
Introduction To Groovy 2005 by Tugdual Grall
Introduction To Groovy 2005Introduction To Groovy 2005
Introduction To Groovy 2005
Tugdual Grall856 views
Java script final presentation by Adhoura Academy
Java script final presentationJava script final presentation
Java script final presentation
Adhoura Academy3.8K views
Rewriting Java In Scala by Skills Matter
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
Skills Matter1.5K views
Introduction to Client-Side Javascript by Julie Iskander
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
Julie Iskander861 views

More from Raffi Krikorian

Hacking Conway's Law by
Hacking Conway's LawHacking Conway's Law
Hacking Conway's LawRaffi Krikorian
5.1K views36 slides
Re-architecting on the Fly #OReillySACon by
Re-architecting on the Fly #OReillySACon Re-architecting on the Fly #OReillySACon
Re-architecting on the Fly #OReillySACon Raffi Krikorian
6.7K views131 slides
Real-time systems at Twitter (Velocity 2012) by
Real-time systems at Twitter (Velocity 2012)Real-time systems at Twitter (Velocity 2012)
Real-time systems at Twitter (Velocity 2012)Raffi Krikorian
10.6K views42 slides
Twitter: Engineering for Real-Time (Stanford ACM 2011) by
Twitter: Engineering for Real-Time (Stanford ACM 2011)Twitter: Engineering for Real-Time (Stanford ACM 2011)
Twitter: Engineering for Real-Time (Stanford ACM 2011)Raffi Krikorian
10.7K views140 slides
Securing Your Ecosystem (FOWA Las Vegas 2011) by
Securing Your Ecosystem (FOWA Las Vegas 2011)Securing Your Ecosystem (FOWA Las Vegas 2011)
Securing Your Ecosystem (FOWA Las Vegas 2011)Raffi Krikorian
4.7K views31 slides
Developing for @twitterapi (Techcrunch Disrupt Hackathon) by
Developing for @twitterapi (Techcrunch Disrupt Hackathon)Developing for @twitterapi (Techcrunch Disrupt Hackathon)
Developing for @twitterapi (Techcrunch Disrupt Hackathon)Raffi Krikorian
7.9K views28 slides

More from Raffi Krikorian(20)

Re-architecting on the Fly #OReillySACon by Raffi Krikorian
Re-architecting on the Fly #OReillySACon Re-architecting on the Fly #OReillySACon
Re-architecting on the Fly #OReillySACon
Raffi Krikorian6.7K views
Real-time systems at Twitter (Velocity 2012) by Raffi Krikorian
Real-time systems at Twitter (Velocity 2012)Real-time systems at Twitter (Velocity 2012)
Real-time systems at Twitter (Velocity 2012)
Raffi Krikorian10.6K views
Twitter: Engineering for Real-Time (Stanford ACM 2011) by Raffi Krikorian
Twitter: Engineering for Real-Time (Stanford ACM 2011)Twitter: Engineering for Real-Time (Stanford ACM 2011)
Twitter: Engineering for Real-Time (Stanford ACM 2011)
Raffi Krikorian10.7K views
Securing Your Ecosystem (FOWA Las Vegas 2011) by Raffi Krikorian
Securing Your Ecosystem (FOWA Las Vegas 2011)Securing Your Ecosystem (FOWA Las Vegas 2011)
Securing Your Ecosystem (FOWA Las Vegas 2011)
Raffi Krikorian4.7K views
Developing for @twitterapi (Techcrunch Disrupt Hackathon) by Raffi Krikorian
Developing for @twitterapi (Techcrunch Disrupt Hackathon)Developing for @twitterapi (Techcrunch Disrupt Hackathon)
Developing for @twitterapi (Techcrunch Disrupt Hackathon)
Raffi Krikorian7.9K views
Twitter for CS10 @ Berkeley (Spring 2011) by Raffi Krikorian
Twitter for CS10 @ Berkeley (Spring 2011)Twitter for CS10 @ Berkeley (Spring 2011)
Twitter for CS10 @ Berkeley (Spring 2011)
Raffi Krikorian1K views
Twitter by the Numbers (Columbia University) by Raffi Krikorian
Twitter by the Numbers (Columbia University)Twitter by the Numbers (Columbia University)
Twitter by the Numbers (Columbia University)
Raffi Krikorian3.7K views
Twitter and the Real-Time Web by Raffi Krikorian
Twitter and the Real-Time WebTwitter and the Real-Time Web
Twitter and the Real-Time Web
Raffi Krikorian1.4K views
Twitter - Guest Lecture UC Berkeley CS10 Fall 2010 by Raffi Krikorian
Twitter - Guest Lecture UC Berkeley CS10 Fall 2010Twitter - Guest Lecture UC Berkeley CS10 Fall 2010
Twitter - Guest Lecture UC Berkeley CS10 Fall 2010
Raffi Krikorian2.4K views
Developing for @twitterapi #hack4health by Raffi Krikorian
Developing for @twitterapi #hack4healthDeveloping for @twitterapi #hack4health
Developing for @twitterapi #hack4health
Raffi Krikorian1.7K views
Intro to developing for @twitterapi (updated) by Raffi Krikorian
Intro to developing for @twitterapi (updated)Intro to developing for @twitterapi (updated)
Intro to developing for @twitterapi (updated)
Raffi Krikorian2.6K views
How to use Geolocation in your webapp @ FOWA Dublin 2010 by Raffi Krikorian
How to use Geolocation in your webapp @ FOWA Dublin 2010How to use Geolocation in your webapp @ FOWA Dublin 2010
How to use Geolocation in your webapp @ FOWA Dublin 2010
Raffi Krikorian1K views
Intro to developing for @twitterapi by Raffi Krikorian
Intro to developing for @twitterapiIntro to developing for @twitterapi
Intro to developing for @twitterapi
Raffi Krikorian10.5K views
"What's Happening" to "What's Happening Here" @ Chirp by Raffi Krikorian
"What's Happening" to "What's Happening Here" @ Chirp"What's Happening" to "What's Happening Here" @ Chirp
"What's Happening" to "What's Happening Here" @ Chirp
Raffi Krikorian1.4K views

Recently uploaded

TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc
72 views29 slides
PRODUCT LISTING.pptx by
PRODUCT LISTING.pptxPRODUCT LISTING.pptx
PRODUCT LISTING.pptxangelicacueva6
18 views1 slide
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ... by
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...Jasper Oosterveld
27 views49 slides
Future of Indian ConsumerTech by
Future of Indian ConsumerTechFuture of Indian ConsumerTech
Future of Indian ConsumerTechKapil Khandelwal (KK)
24 views68 slides
Ransomware is Knocking your Door_Final.pdf by
Ransomware is Knocking your Door_Final.pdfRansomware is Knocking your Door_Final.pdf
Ransomware is Knocking your Door_Final.pdfSecurity Bootcamp
66 views46 slides
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf by
STKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdfSTKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdf
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdfDr. Jimmy Schwarzkopf
24 views29 slides

Recently uploaded(20)

TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc72 views
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ... by Jasper Oosterveld
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf by Dr. Jimmy Schwarzkopf
STKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdfSTKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdf
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf
Five Things You SHOULD Know About Postman by Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman38 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson126 views
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software317 views
HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn26 views
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker48 views
The Forbidden VPN Secrets.pdf by Mariam Shaba
The Forbidden VPN Secrets.pdfThe Forbidden VPN Secrets.pdf
The Forbidden VPN Secrets.pdf
Mariam Shaba20 views
Piloting & Scaling Successfully With Microsoft Viva by Richard Harbridge
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft Viva
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab23 views
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by IttrainingIttraining
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
"Node.js Development in 2024: trends and tools", Nikita Galkin by Fwdays
"Node.js Development in 2024: trends and tools", Nikita Galkin "Node.js Development in 2024: trends and tools", Nikita Galkin
"Node.js Development in 2024: trends and tools", Nikita Galkin
Fwdays17 views
SAP Automation Using Bar Code and FIORI.pdf by Virendra Rai, PMP
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdf

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

  • 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
  • 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(quot;?quot; ~> expr ~ (quot;:quot; ~> expr))) ^^ { case c ~ None => c case c ~ Some(t ~ e) => Conditional(c, t, e) } def or = chainl1(and, quot;||quot; ~> and, success{(a:AST, b:AST)=>Or(a,b)}) def and = chainl1(rel, quot;&&quot; ~> 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(quot;?quot; ~> expr ~ (quot;:quot; ~> expr))) ^^ { case c ~ None => c case c ~ Some(t ~ e) => Conditional(c, t, e) } def cond = { ts.foldOpt(or) { case (c, SyntaxToken(quot;?quot;, _)) => val t = expr ts.syntax(quot;:quot;) val e = expr Conditional(c, t, e) } }
  • 16. Handwritten Parser def or = chainl1(and, quot;||quot; ~> and, success{(a:AST, b:AST)=>Or(a,b)}) def or = { ts.fold(and) { case (left, SyntaxToken(quot;||quot;, _)) => 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=quot;EnumValuequot;> <field name=quot;versionquot; type=quot;intquot;/> <field name=quot;enumTypequot; type=quot;EnumTypequot;/> <field name=quot;keyquot; type=quot;stringquot;/> <field name=quot;displayNamequot; type=quot;stringquot;/> <field name=quot;descriptionquot; type=quot;stringquot; size=quot;0,65535quot;/> <field name=quot;creatorquot; type=quot;Userquot;/> <index fields=quot;enumType, keyquot; unique=quot;truequot;/> </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(quot;domainquot; -> domain). innerJoin(quot;creatorquot;, quot;confirmedquot; -> true). orderBy(quot;createdAtquot; -> 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