SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Domain Specific Languages #DUSE-3
Peter Maas  ~ Developer at Ebay Classifieds, working at the Marktplaats.nl migration. Likes programming languages. whoami
Agenda ,[object Object]
What makes Scala a good language for DSLs
Internal DSL
External DSL ACTUAL CODE!
Questions? Feel free to interrupt!
So, what is a DSL ,[object Object]
A language which feels natural to a specialist in the targeted domain
Internal / External ,[object Object]
Gradle
Routing configuration in Apache Camel ,[object Object],[object Object]
Velocity
Why is Scala good at that ,[object Object]
Function objects (closures, currying etc)
Default arguments
Operator overloading
Implicit conversions

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
IO Streams, Files and Directories
IO Streams, Files and DirectoriesIO Streams, Files and Directories
IO Streams, Files and Directories
 
Learn Ruby 2011 - Session 5 - Looking for a Rescue
Learn Ruby 2011 - Session 5 - Looking for a RescueLearn Ruby 2011 - Session 5 - Looking for a Rescue
Learn Ruby 2011 - Session 5 - Looking for a Rescue
 
Briefly Rust
Briefly RustBriefly Rust
Briefly Rust
 
Introducing Ruby
Introducing RubyIntroducing Ruby
Introducing Ruby
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
What You Need to Know About Lambdas - Jamie Allen (Typesafe)
What You Need to Know About Lambdas - Jamie Allen (Typesafe)What You Need to Know About Lambdas - Jamie Allen (Typesafe)
What You Need to Know About Lambdas - Jamie Allen (Typesafe)
 
Why Haskell
Why HaskellWhy Haskell
Why Haskell
 
Format String
Format StringFormat String
Format String
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
How Xslate Works
How Xslate WorksHow Xslate Works
How Xslate Works
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)P6 OO vs Moose (&Moo)
P6 OO vs Moose (&Moo)
 
Clojure 7-Languages
Clojure 7-LanguagesClojure 7-Languages
Clojure 7-Languages
 
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
 
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur..."How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
 

Andere mochten auch (6)

you
youyou
you
 
Autoforms
AutoformsAutoforms
Autoforms
 
Spring MVC introduction HVA
Spring MVC introduction HVASpring MVC introduction HVA
Spring MVC introduction HVA
 
Introduction To Scala
Introduction To ScalaIntroduction To Scala
Introduction To Scala
 
Grails @ Battle of the Geeks 2007
Grails @ Battle of the Geeks 2007Grails @ Battle of the Geeks 2007
Grails @ Battle of the Geeks 2007
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 

Ähnlich wie Domain Specific Languages In Scala Duse3

Embedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaEmbedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for Java
Jevgeni Kabanov
 
RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsRailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMs
Lourens Naudé
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
intelliyole
 
Java Intro
Java IntroJava Intro
Java Intro
backdoor
 
30 5 Database Jdbc
30 5 Database Jdbc30 5 Database Jdbc
30 5 Database Jdbc
phanleson
 
Ruby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrādeRuby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrāde
Raimonds Simanovskis
 
Eff Plsql
Eff PlsqlEff Plsql
Eff Plsql
afa reg
 

Ähnlich wie Domain Specific Languages In Scala Duse3 (20)

C to perl binding
C to perl bindingC to perl binding
C to perl binding
 
Scala + WattzOn, sitting in a tree....
Scala + WattzOn, sitting in a tree....Scala + WattzOn, sitting in a tree....
Scala + WattzOn, sitting in a tree....
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
Embedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaEmbedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for Java
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsRailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMs
 
Fun with errors? - Clojure Finland Meetup 26.3.2019 Tampere
Fun with errors? - Clojure Finland Meetup 26.3.2019 TampereFun with errors? - Clojure Finland Meetup 26.3.2019 Tampere
Fun with errors? - Clojure Finland Meetup 26.3.2019 Tampere
 
From Query Plan to Query Performance: Supercharging your Apache Spark Queries...
From Query Plan to Query Performance: Supercharging your Apache Spark Queries...From Query Plan to Query Performance: Supercharging your Apache Spark Queries...
From Query Plan to Query Performance: Supercharging your Apache Spark Queries...
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
Code documentation
Code documentationCode documentation
Code documentation
 
Sqladria 2009 SRC
Sqladria 2009 SRCSqladria 2009 SRC
Sqladria 2009 SRC
 
Java Intro
Java IntroJava Intro
Java Intro
 
Adventurous Merb
Adventurous MerbAdventurous Merb
Adventurous Merb
 
닷넷 개발자를 위한 패턴이야기
닷넷 개발자를 위한 패턴이야기닷넷 개발자를 위한 패턴이야기
닷넷 개발자를 위한 패턴이야기
 
30 5 Database Jdbc
30 5 Database Jdbc30 5 Database Jdbc
30 5 Database Jdbc
 
Writing DSL with Applicative Functors
Writing DSL with Applicative FunctorsWriting DSL with Applicative Functors
Writing DSL with Applicative Functors
 
Sax Dom Tutorial
Sax Dom TutorialSax Dom Tutorial
Sax Dom Tutorial
 
Ruby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrādeRuby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrāde
 
Eff Plsql
Eff PlsqlEff Plsql
Eff Plsql
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

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...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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
 
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
 
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)
 
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
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
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
 

Domain Specific Languages In Scala Duse3

  • 2. Peter Maas ~ Developer at Ebay Classifieds, working at the Marktplaats.nl migration. Likes programming languages. whoami
  • 3.
  • 4. What makes Scala a good language for DSLs
  • 7. Questions? Feel free to interrupt!
  • 8.
  • 9. A language which feels natural to a specialist in the targeted domain
  • 10.
  • 12.
  • 14.
  • 19. Built-in support for building parsers
  • 20. Due to it's functional nature it is good at handling syntax trees.
  • 21. Concise? val toGreet = " Marktplaats developers"  "Hello" . + ( toGreet )   "Hello" + toGreet   "Hello" . concat ( toGreet ) "Hello" concat toGreet   // methods are objects...   val greetMethod = "hello %s".format(_:String) greetMethod(toGreet)
  • 22. Implicit conversions class StringReducer(s:String) { def acronym = s.filter(_.isUpper) } implicit def string2StringReducer(s:String) = new StringReducer(s) "Sun Certified Java Programmer".acronym == "SCJP"
  • 23. Or... case class Route(source:String, target:String) class RouteBuilder(s:String) { def --> (target:String) = Route(s, target) } implicit def string2Route(s:String) = new RouteBuilder(s) "a" --> "b" == Route( "a" , "b" )
  • 24. Let's build a DSL we all know SQL http://github.com/p3t0r/scala-sql-dsl
  • 25. Pure Scala // Simple query with sorting SQL select "*" from ("user") order Asc("name") // More complex query with and/or including precedence SQL select "*" from ("user") where (("name","peter") and (("active", true) or ("role", "admin"))) Overriding the '=' operator was a no-go ;-)
  • 27. case class Where( val clauses: Clause*) trait Clause { def and (otherField: Clause): Clause = And(this, otherField) def or (otherField: Clause): Clause = Or(this, otherField) } case class StringEquals(val f: String, val value: String) extends Clause case class NumberEquals(val f: String, val value: Number) extends Clause case class BooleanEquals(val f: String, val value: Boolean) extends Clause case class In(val field: String, val values: String*) extends Clause case class And(val lClause:Clause, val rClause:Clause) extends Clause case class Or(val lClause:Clause, val rClause:Clause) extends Clause AST for the Where clause
  • 28. Some Implicits & Helpers object QueryBuilder { implicit def tuple2field(t: (String, String)): StringEquals = StringEquals(t._1, t._2) implicit def tuple2field(t: (String, Int)): NumberEquals = NumberEquals(t._1, t._2) implicit def tuple2field(t: (String, Boolean)): BooleanEquals = BooleanEquals(t._1, t._2) implicit def from2query(f: From): Query = Query(f.operation.get, f, Option(Where())) /** entrypoint for starting a select query */ def select(fields:String*) = Select(fields:_*) def select(symbol: Symbol): Select = symbol match { case 'all => select("*") case _ => throw new RuntimeException("Only 'all allowed as symbol") } def in(field: String, values: String*) = In(field, values: _*) }
  • 29. That looks like a lot of code :( The entire builder is only 54 lines, including whitespace and comments! >> CONTINUE IN IDEA <<
  • 30. And external? class SQLParser extends JavaTokenParsers { def query:Parser[Query] = operation ~ from ~ opt(where) ~ opt(order) ^^ { case operation ~ from ~ where ~ order => Query(operation, from, where, order) } def operation:Parser[Operation] = { (&quot;select&quot; | &quot;update&quot; | &quot;delete&quot;) ~ repsep(ident, &quot;,&quot;) ^^ { case &quot;select&quot; ~ f => Select(f:_*) case _ => throw new IllegalArgumentException(&quot;Operation not implemented&quot;) } } def from:Parser[From] = &quot;from&quot; ~> ident ^^ (From(_)) def where:Parser[Where] = &quot;where&quot; ~> rep(clause) ^^ (Where(_:_*)) /* more in idea */ }