Building a Mongo DSL in Scala at Hot Potato

2,638 views
2,527 views

Published on

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

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,638
On SlideShare
0
From Embeds
0
Number of Embeds
106
Actions
Shares
0
Downloads
17
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide













  • Building a Mongo DSL in Scala at Hot Potato

    1. 1. MongoNYC May 21, 2010 1
    2. 2. 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
    3. 3. Technology Stack Design principles: Simple and stateless API • Scala / MongoDB Hosted on EC2 API Clients • Web application (Python/Django) • iPhone (Obj-C) 3
    4. 4. Three key Scala features • Options •Pattern Matching / Case classes • Implicit conversions 4
    5. 5. 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
    6. 6. 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
    7. 7. 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
    8. 8. 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
    9. 9. 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
    10. 10. 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
    11. 11. Building a DSL for Mongo Documents are the ultimate building block: • Insertion • Updates • Queries • Sorting • Map / Reduce • Indexes • Database Commands 11
    12. 12. 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
    13. 13. Thank You! We’re looking for Scala engineers! email: lincoln@hotpotato.com twitter: 11nc hotpotato: lincoln Questions? 13

    ×