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