SlideShare ist ein Scribd-Unternehmen logo
1 von 13
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

Weitere ähnliche Inhalte

Was ist angesagt?

Scala Style by Adform Research (Saulius Valatka)
Scala Style by Adform Research (Saulius Valatka)Scala Style by Adform Research (Saulius Valatka)
Scala Style by Adform Research (Saulius Valatka)
Vasil Remeniuk
 

Was ist angesagt? (20)

Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
Session 4#
Session 4#Session 4#
Session 4#
 
Machine Learning - Dataset Preparation
Machine Learning - Dataset PreparationMachine Learning - Dataset Preparation
Machine Learning - Dataset Preparation
 
Session 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing BasicsSession 16 - Collections - Sorting, Comparing Basics
Session 16 - Collections - Sorting, Comparing Basics
 
Session#2
Session#2Session#2
Session#2
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
 
Session 3#
Session 3#Session 3#
Session 3#
 
Introduction to Type Script by Sam Goldman, SmartLogic
Introduction to Type Script by Sam Goldman, SmartLogicIntroduction to Type Script by Sam Goldman, SmartLogic
Introduction to Type Script by Sam Goldman, SmartLogic
 
Collections - Maps
Collections - Maps Collections - Maps
Collections - Maps
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
Session 20 - Collections - Maps
Session 20 - Collections - MapsSession 20 - Collections - Maps
Session 20 - Collections - Maps
 
Oops concept
Oops conceptOops concept
Oops concept
 
Java Tutorial Lab 4
Java Tutorial Lab 4Java Tutorial Lab 4
Java Tutorial Lab 4
 
Object Class
Object ClassObject Class
Object Class
 
Java2
Java2Java2
Java2
 
Scala Style by Adform Research (Saulius Valatka)
Scala Style by Adform Research (Saulius Valatka)Scala Style by Adform Research (Saulius Valatka)
Scala Style by Adform Research (Saulius Valatka)
 
Learning Web Development with Django - Templates
Learning Web Development with Django - TemplatesLearning Web Development with Django - Templates
Learning Web Development with Django - Templates
 
Object Class
Object Class Object Class
Object Class
 
Dev Concepts: Object-Oriented Programming
Dev Concepts: Object-Oriented ProgrammingDev Concepts: Object-Oriented Programming
Dev Concepts: Object-Oriented Programming
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
 

Ähnlich wie Building a Mongo DSL in Scala at Hot Potato

Migrating from matlab to python
Migrating from matlab to pythonMigrating from matlab to python
Migrating from matlab to python
ActiveState
 
AI與大數據數據處理 Spark實戰(20171216)
AI與大數據數據處理 Spark實戰(20171216)AI與大數據數據處理 Spark實戰(20171216)
AI與大數據數據處理 Spark實戰(20171216)
Paul Chao
 
The openCypher Project - An Open Graph Query Language
The openCypher Project - An Open Graph Query LanguageThe openCypher Project - An Open Graph Query Language
The openCypher Project - An Open Graph Query Language
Neo4j
 

Ähnlich wie Building a Mongo DSL in Scala at Hot Potato (20)

Migrating from matlab to python
Migrating from matlab to pythonMigrating from matlab to python
Migrating from matlab to python
 
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
 
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
 
Spock: Test Well and Prosper
Spock: Test Well and ProsperSpock: Test Well and Prosper
Spock: Test Well and Prosper
 
Functional programming in kotlin with Arrow [Sunnytech 2018]
Functional programming in kotlin with Arrow [Sunnytech 2018]Functional programming in kotlin with Arrow [Sunnytech 2018]
Functional programming in kotlin with Arrow [Sunnytech 2018]
 
COMMitMDE'18: Eclipse Hawk: model repository querying as a service
COMMitMDE'18: Eclipse Hawk: model repository querying as a serviceCOMMitMDE'18: Eclipse Hawk: model repository querying as a service
COMMitMDE'18: Eclipse Hawk: model repository querying as a service
 
lecture1.ppt
lecture1.pptlecture1.ppt
lecture1.ppt
 
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
MongoDB Pros and Cons
MongoDB Pros and ConsMongoDB Pros and Cons
MongoDB Pros and Cons
 
Rails Tips and Best Practices
Rails Tips and Best PracticesRails Tips and Best Practices
Rails Tips and Best Practices
 
Getting Started with Java
Getting Started with JavaGetting Started with Java
Getting Started with Java
 
Functional Operations - Susan Potter
Functional Operations - Susan PotterFunctional Operations - Susan Potter
Functional Operations - Susan Potter
 
Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
 
AI與大數據數據處理 Spark實戰(20171216)
AI與大數據數據處理 Spark實戰(20171216)AI與大數據數據處理 Spark實戰(20171216)
AI與大數據數據處理 Spark實戰(20171216)
 
Class 10 Arrays
Class 10 ArraysClass 10 Arrays
Class 10 Arrays
 
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
 
The openCypher Project - An Open Graph Query Language
The openCypher Project - An Open Graph Query LanguageThe openCypher Project - An Open Graph Query Language
The openCypher Project - An Open Graph Query Language
 
MongoDB at ZPUGDC
MongoDB at ZPUGDCMongoDB at ZPUGDC
MongoDB at ZPUGDC
 
Graph Databases in the Microsoft Ecosystem
Graph Databases in the Microsoft EcosystemGraph Databases in the Microsoft Ecosystem
Graph Databases in the Microsoft Ecosystem
 

Mehr von MongoDB

Mehr von MongoDB (20)

MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 MongoDB SoCal 2020: MongoDB Atlas Jump Start MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Building a Mongo DSL in Scala at Hot Potato

  • 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

Hinweis der Redaktion