Building a Mongo DSL in Scala at Hot Potato
 

Building a Mongo DSL in Scala at Hot Potato

on

  • 3,791 views

Presentation by Lincoln Hochberg of Hot Potato at MongoNYC on May 21

Presentation by Lincoln Hochberg of Hot Potato at MongoNYC on May 21

Statistics

Views

Total Views
3,791
Views on SlideShare
3,693
Embed Views
98

Actions

Likes
2
Downloads
15
Comments
0

4 Embeds 98

http://www.slideshare.net 48
http://www.mongodb.org 42
http://lanyrd.com 6
http://lanyrd.dev 2

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Building a Mongo DSL in Scala at Hot Potato Building a Mongo DSL in Scala at Hot Potato Presentation Transcript

  • MongoNYC May 21, 2010 1
  • Hot Potato Overview • What: Hot Potato connects audiences around shared interests • When: Founded early 2009 / PoC: Dec 2009 / Beta: 2 months ago • Where: Williamsburg, Brooklyn! • Who: Lincoln, Justin, Saadiq, Jeremy, Jace, Matt, Mike, Will 2
  • Technology Stack Design principles: Simple and stateless API • Scala / MongoDB Hosted on EC2 API Clients • Web application (Python/Django) • iPhone (Obj-C) 3
  • Three key Scala features • Options •Pattern Matching / Case classes • Implicit conversions 4
  • Options Options are scala’s answer to null. Options are a very simple, 3-part class hierarchy: • sealed abstract class Option[+A] extends Product • case final class Some[+A](val x : A) extends Option[A] • case object None extends Option[Nothing] 5
  • Options Options have some interesting methods: • def get: A Some(1).get ==> 1 None.get ==> java.util.NoSuchElementException! • def getOrElse[B>:A](default: =>B): B Some(1).getOrElse(2) ==> 1 None.getOrElse(2) ==> 2 • def map[B](f: (A)=>B): Option[B] Some(1).map(_.toString) ==> Some(“1”) None.map(_.toString) ==> None 6
  • Options An example: for (val x <- Some(3); val y <- Some(2)) yield x * y ==> Some(6) for (val x <- Some(3); val y <- None) yield x * y ==> None (for { val x <- Some(3) val y <- Some(2) } yield x * y ) getOrElse -1 ==> 6 (for { val x <- Some(3) val y <- None } yield x * y ) getOrElse -1 ==> -1 7
  • Pattern Matching Patterns can match any type of data with a first match policy. def patmatch(a: Any): Int = a match { case "uno" => 1 case d: Double => d.toInt case "three" | 3 => 3 case x: Int if x % 4 == 0 => 4 case _ => -1 } patmatch(“uno”) ==> 1 patmatch(2.3) ==> 2 patmatch(1) ==> 0 patmatch(8) ==> 4 patmatch(“three”) ==> 3 8
  • Case Classes Case classes are regular classes which export their constructor parameters and which provide a recursive decomposition mechanism via pattern matching. case class Blah(name: String, value: Option[Int]) def casematch(s: Blah): Int = s match { case Blah("one", _) => 1 case x @ Blah(_, Some(2)) => x.name.toInt case x: Blah if x.name == "three" && x.value.isDefined => x.value.get case Blah(_, Some(y)) => y case _ => -1 } casematch(Blah(“one”, None)) ==> 1 casematch(Blah(“1”, Some(2))) ==> 1 casematch(Blah(“three”, Some(2))) ==> NumberFormatException! casematch(Blah(“blah”, Some(234))) ==> 234 9
  • Implicit Conversions Implicit methods are called by the compiler when: • they are in scope • the selection is unambiguous and one level deep • doing so would resolve a compilation error Example: TimeHelpers.scala 10
  • Building a DSL for Mongo Documents are the ultimate building block: • Insertion • Updates • Queries • Sorting • Map / Reduce • Indexes • Database Commands 11
  • Putting it all together Goals • Stay close to the MongoDB Java API • Keep it flexible • Focus on document creation Key classes and objects • Collection - wraps MongoDB DBCollection • MongoAST - defines the types for building Mongo documents • MongoDSL - defines DSL syntax 12
  • Thank You! We’re looking for Scala engineers! email: lincoln@hotpotato.com twitter: 11nc hotpotato: lincoln Questions? 13