SlideShare a Scribd company logo
Scala @ TomTomEric Bowmaneric.bowman@tomtom.com26 May 2010
What’s He Building In There?Not just PNDsTraffic ServicesLocation ServicesAddress & Local SearchTons of Data -> Tons of AnalysisDeep Commitment to Server-Side Java2
3
4
5Company Confidential21,345 pages
40 reams of paper
5 laptops
8 keyboards
1 back operation
30,000 strands of hair6
7
Tutorial on Good Lisp Programming Style8“Most algorithms can be characterized as:  Searching (some find find-if mismatch)
  Sorting (sort merge remove-duplicates)
  Filtering (remove remove-if mapcan)
  Mapping (map mapcarmapc)
  Combining (reduce mapcan)
  Counting (count count-if)These functions abstract common control patterns. Code that uses them is:  Concise
  Self-documenting
  Easy to understand
  Often reusable
  Usually efficient”    From Peter Norvig’sTutorial on Good Lisp Programming Style, 1993         (http://www.norvig.com/luv-slides.ps)
But...“Lisp is good for:Exploratory programmingRapid prototypingMinimizing time-to-marketSingle-programmer (or single-digit team) projectsSource-to-source or data-to-data transformation” (ibid.)9
I LikeStatic typesRefactoring ToolsLazy SundaysPaying Some Kid To Cut The GrassSpreadsheetsNot Flying The Plane10
What is this Scala thing?11
Scala Is...An escaped research language from EPFL targeting JVM and CLRObject-oriented and functionalStatically typedLisp, ML, Haskell, Erlang, etc.DSL-friendly12
Bi-Scalar Disorder13
Functional Programming?Functions as first class objectsImmutabilityClosuresBinding free variables to enclosing lexical scope Higher-order Functionsfunctions as input and/or outputA different set of idioms...14
Keep Backfor (int i = 1; i <= 256; i++) {   if (array[i-1] == ...15
LOLCrash16
IdiomsReduce cognitive overheadReduce bugsMake intention clearMini-Patterns17
Java Idioms++C++No more 0xDEADBEEFLeads to lots of loops and copies, if you’re doing it rightHard programs get complex doing common thingsNested loops begin to look Harmful...18
19
For Comprehensionsfor(inti=0; i<100; i++) { ... }for (i <- 0 until 100) { ... /* do something with i */ }(0.until(100)).foreach(i => 			/* something with i */)20
For ComprehensionsLists algorithmic “sweet spot”Syntactic Sugar for:foreachmapfilterflatMap21
The Easy Onesfor (i <- 1 to 6) yield i * 2(1 to 6).map(_ * 2)	(2,4,6,8,10,12)for (i <- 1 to 6 if i % 2 == 0) yield i(1 to 6).filter(_ % 2 == 0)	(2,4,6)for (i <- 1 to 6) { println(i + “ “) }(1 to 6).foreach { i => print(i + “ “) }	1 2 3 4 5 622
A Harder One...List<Integer> array = new ArrayList<Integer>();for (i = 1; i <= 3; i++) {  for (j = i; j <= 3; j++) {array.add(j);  }}System.out.println(array);[1, 2, 3, 2, 3, 3]for (i <- 1 to 3; j <- i to 3) yield j(1, 2, 3, 2, 3, 3)(1 to 3).flatMap(i => (i to 3).map(j => j))   23
flatMapSubtle thing...“Applies the given function f to each element, then concatenates the results”Turns a list of lists into a listList(List(1,2,3), List(4,5,6)).flatMap(x => x)List(1, 2, 3, 4, 5, 6)List(“tom”, “tom”).flatMap(_.capitalize).mkStringTomTom“Special sauce” for nested looping constructs(Equivalent to Haskell’s monadic “bind”)24
IteratorIteratorpackage org.hyperic.sigar.util; public static class IteratorIteratorimplements java.util.Iterator { private java.util.ArrayListiterators; public IteratorIterator() {} public void add(java.util.Iteratoriterator) {} public booleanhasNext() {} public java.lang.Object next() {} 	public void remove() {} }25
flatMapflatMapdef foo(arg: Iterable[Int]) {    ... Do something with arg}val some: Iterable[Int] = getSomeIterablefoo(some)val more: Iterable[Int] = getMorefoo(List(some, more).flatMap(x => x))26
Real-Life Example“mntstamarg”Each term has aliasesNeed all permutations27
Java Versionstatic List<String> aliasReplace(String place) {         String[] bits = nospaces.split(place);         List<String> result = new ArrayList<String>();         List<StringBuilder> allAliases = new ArrayList<StringBuilder>(); for (String bit : bits) {             String[] ales = aliases.get(bit); if (ales != null) { 	            if (allAliases.size() == 0) { allAliases.add(new StringBuilder(bit)); for (String alias : ales) { allAliases.add(new StringBuilder(alias));                     }                 } else {                     List<StringBuilder> clones = new              ArrayList<StringBuilder>(); for (StringBuilder a : allAliases) { clones.add(new StringBuilder(a).append(" ").append(                                 bit)); for (String alias : ales) { clones.add(new StringBuilder(a).append(" ").append(                                     alias));                         }                     } allAliases = clones;                 }             } else { if (allAliases.size() == 0) { allAliases.add(new StringBuilder(bit));                 } else { for (StringBuilder b : allAliases) { b.append(" ").append(bit);                     }                 }             }         } for (StringBuildermunge: allAliases) { result.add(munge.toString());         } return result;     }28
29

More Related Content

What's hot (20)

PDF
Haskell for data science
John Cant
 
PDF
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
PDF
The Design of the Scalaz 8 Effect System
John De Goes
 
PPT
Euro python2011 High Performance Python
Ian Ozsvald
 
PDF
Introduction to functional programming using Ocaml
pramode_ce
 
PDF
Fun with Kotlin
Egor Andreevich
 
PPT
Python 101 language features and functional programming
Lukasz Dynowski
 
PDF
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
GECon_Org Team
 
PDF
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
Yauheni Akhotnikau
 
PDF
A tour of Python
Aleksandar Veselinovic
 
PDF
Java Performance Puzzlers
Doug Hawkins
 
PDF
Orthogonal Functional Architecture
John De Goes
 
PDF
Procedural Programming: It’s Back? It Never Went Away
Kevlin Henney
 
PDF
Halogen: Past, Present, and Future
John De Goes
 
PDF
Taming Asynchronous Transforms with Interstellar
Jens Ravens
 
PDF
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
John De Goes
 
PDF
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Fwdays
 
PDF
Sneaking inside Kotlin features
Chandra Sekhar Nayak
 
PDF
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Philip Schwarz
 
PPTX
Lambda выражения и Java 8
Alex Tumanoff
 
Haskell for data science
John Cant
 
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
The Design of the Scalaz 8 Effect System
John De Goes
 
Euro python2011 High Performance Python
Ian Ozsvald
 
Introduction to functional programming using Ocaml
pramode_ce
 
Fun with Kotlin
Egor Andreevich
 
Python 101 language features and functional programming
Lukasz Dynowski
 
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
GECon_Org Team
 
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
Yauheni Akhotnikau
 
A tour of Python
Aleksandar Veselinovic
 
Java Performance Puzzlers
Doug Hawkins
 
Orthogonal Functional Architecture
John De Goes
 
Procedural Programming: It’s Back? It Never Went Away
Kevlin Henney
 
Halogen: Past, Present, and Future
John De Goes
 
Taming Asynchronous Transforms with Interstellar
Jens Ravens
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
John De Goes
 
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Fwdays
 
Sneaking inside Kotlin features
Chandra Sekhar Nayak
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
Philip Schwarz
 
Lambda выражения и Java 8
Alex Tumanoff
 

Viewers also liked (7)

PDF
TomTom Q1 2014 Financial Results
Ludovic Privat
 
PDF
Allan Rasmussen TomTom Maps June 16th 2014 - INSPIRE Conference
Ludovic Privat
 
PPTX
Tom Tom
Mohamed Abdulla
 
PDF
TOM TOM
Alejo Colorado
 
PPT
TomTom Presentation.
alfiepanda
 
DOC
Mba Tomtom merger TeleAtlas
Roel_Kock
 
PDF
TomTom Dynamic Routing
Dr. Heiko Schilling, eMBA
 
TomTom Q1 2014 Financial Results
Ludovic Privat
 
Allan Rasmussen TomTom Maps June 16th 2014 - INSPIRE Conference
Ludovic Privat
 
TomTom Presentation.
alfiepanda
 
Mba Tomtom merger TeleAtlas
Roel_Kock
 
TomTom Dynamic Routing
Dr. Heiko Schilling, eMBA
 
Ad

Similar to Scala @ TomTom (20)

PDF
Real Time Big Data Management
Albert Bifet
 
ODP
Scala introduction
Alf Kristian Støyle
 
ODP
Scala 2 + 2 > 4
Emil Vladev
 
PPTX
What is new in Java 8
Sandeep Kr. Singh
 
PDF
Refactoring to Macros with Clojure
Dmitry Buzdin
 
PPTX
Groovy
Zen Urban
 
PDF
Introduction To Lisp
kyleburton
 
PDF
Design Patterns - Compiler Case Study - Hands-on Examples
Ganesh Samarthyam
 
ODP
Scala as a Declarative Language
vsssuresh
 
PDF
Scala in Places API
Łukasz Bałamut
 
PPT
SDC - Einführung in Scala
Christian Baranowski
 
PPT
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
PDF
Monadologie
league
 
PDF
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
PDF
Pragmatic Real-World Scala
parag978978
 
PPT
JBUG 11 - Scala For Java Programmers
Tikal Knowledge
 
PDF
Introduction to Scalding and Monoids
Hugo Gävert
 
PPTX
C++11 - A Change in Style - v2.0
Yaser Zhian
 
ODP
Clojure basics
Knoldus Inc.
 
PDF
Spark workshop
Wojciech Pituła
 
Real Time Big Data Management
Albert Bifet
 
Scala introduction
Alf Kristian Støyle
 
Scala 2 + 2 > 4
Emil Vladev
 
What is new in Java 8
Sandeep Kr. Singh
 
Refactoring to Macros with Clojure
Dmitry Buzdin
 
Groovy
Zen Urban
 
Introduction To Lisp
kyleburton
 
Design Patterns - Compiler Case Study - Hands-on Examples
Ganesh Samarthyam
 
Scala as a Declarative Language
vsssuresh
 
Scala in Places API
Łukasz Bałamut
 
SDC - Einführung in Scala
Christian Baranowski
 
Scala presentation by Aleksandar Prokopec
Loïc Descotte
 
Monadologie
league
 
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
Pragmatic Real-World Scala
parag978978
 
JBUG 11 - Scala For Java Programmers
Tikal Knowledge
 
Introduction to Scalding and Monoids
Hugo Gävert
 
C++11 - A Change in Style - v2.0
Yaser Zhian
 
Clojure basics
Knoldus Inc.
 
Spark workshop
Wojciech Pituła
 
Ad

Recently uploaded (20)

PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PDF
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PPTX
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PDF
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PDF
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PDF
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PDF
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
Bitkom eIDAS Summit | European Business Wallet: Use Cases, Macroeconomics, an...
Carsten Stoecker
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
TrustArc Webinar - Navigating APAC Data Privacy Laws: Compliance & Challenges
TrustArc
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
Kubernetes - Architecture & Components.pdf
geethak285
 
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 

Scala @ TomTom

  • 2. What’s He Building In There?Not just PNDsTraffic ServicesLocation ServicesAddress & Local SearchTons of Data -> Tons of AnalysisDeep Commitment to Server-Side Java2
  • 3. 3
  • 4. 4
  • 6. 40 reams of paper
  • 11. 7
  • 12. Tutorial on Good Lisp Programming Style8“Most algorithms can be characterized as: Searching (some find find-if mismatch)
  • 13. Sorting (sort merge remove-duplicates)
  • 14. Filtering (remove remove-if mapcan)
  • 15. Mapping (map mapcarmapc)
  • 16. Combining (reduce mapcan)
  • 17. Counting (count count-if)These functions abstract common control patterns. Code that uses them is: Concise
  • 19. Easy to understand
  • 20. Often reusable
  • 21. Usually efficient” From Peter Norvig’sTutorial on Good Lisp Programming Style, 1993 (http://www.norvig.com/luv-slides.ps)
  • 22. But...“Lisp is good for:Exploratory programmingRapid prototypingMinimizing time-to-marketSingle-programmer (or single-digit team) projectsSource-to-source or data-to-data transformation” (ibid.)9
  • 23. I LikeStatic typesRefactoring ToolsLazy SundaysPaying Some Kid To Cut The GrassSpreadsheetsNot Flying The Plane10
  • 24. What is this Scala thing?11
  • 25. Scala Is...An escaped research language from EPFL targeting JVM and CLRObject-oriented and functionalStatically typedLisp, ML, Haskell, Erlang, etc.DSL-friendly12
  • 27. Functional Programming?Functions as first class objectsImmutabilityClosuresBinding free variables to enclosing lexical scope Higher-order Functionsfunctions as input and/or outputA different set of idioms...14
  • 28. Keep Backfor (int i = 1; i <= 256; i++) { if (array[i-1] == ...15
  • 30. IdiomsReduce cognitive overheadReduce bugsMake intention clearMini-Patterns17
  • 31. Java Idioms++C++No more 0xDEADBEEFLeads to lots of loops and copies, if you’re doing it rightHard programs get complex doing common thingsNested loops begin to look Harmful...18
  • 32. 19
  • 33. For Comprehensionsfor(inti=0; i<100; i++) { ... }for (i <- 0 until 100) { ... /* do something with i */ }(0.until(100)).foreach(i => /* something with i */)20
  • 34. For ComprehensionsLists algorithmic “sweet spot”Syntactic Sugar for:foreachmapfilterflatMap21
  • 35. The Easy Onesfor (i <- 1 to 6) yield i * 2(1 to 6).map(_ * 2) (2,4,6,8,10,12)for (i <- 1 to 6 if i % 2 == 0) yield i(1 to 6).filter(_ % 2 == 0) (2,4,6)for (i <- 1 to 6) { println(i + “ “) }(1 to 6).foreach { i => print(i + “ “) } 1 2 3 4 5 622
  • 36. A Harder One...List<Integer> array = new ArrayList<Integer>();for (i = 1; i <= 3; i++) { for (j = i; j <= 3; j++) {array.add(j); }}System.out.println(array);[1, 2, 3, 2, 3, 3]for (i <- 1 to 3; j <- i to 3) yield j(1, 2, 3, 2, 3, 3)(1 to 3).flatMap(i => (i to 3).map(j => j)) 23
  • 37. flatMapSubtle thing...“Applies the given function f to each element, then concatenates the results”Turns a list of lists into a listList(List(1,2,3), List(4,5,6)).flatMap(x => x)List(1, 2, 3, 4, 5, 6)List(“tom”, “tom”).flatMap(_.capitalize).mkStringTomTom“Special sauce” for nested looping constructs(Equivalent to Haskell’s monadic “bind”)24
  • 38. IteratorIteratorpackage org.hyperic.sigar.util; public static class IteratorIteratorimplements java.util.Iterator { private java.util.ArrayListiterators; public IteratorIterator() {} public void add(java.util.Iteratoriterator) {} public booleanhasNext() {} public java.lang.Object next() {} public void remove() {} }25
  • 39. flatMapflatMapdef foo(arg: Iterable[Int]) { ... Do something with arg}val some: Iterable[Int] = getSomeIterablefoo(some)val more: Iterable[Int] = getMorefoo(List(some, more).flatMap(x => x))26
  • 40. Real-Life Example“mntstamarg”Each term has aliasesNeed all permutations27
  • 41. Java Versionstatic List<String> aliasReplace(String place) { String[] bits = nospaces.split(place); List<String> result = new ArrayList<String>(); List<StringBuilder> allAliases = new ArrayList<StringBuilder>(); for (String bit : bits) { String[] ales = aliases.get(bit); if (ales != null) { if (allAliases.size() == 0) { allAliases.add(new StringBuilder(bit)); for (String alias : ales) { allAliases.add(new StringBuilder(alias)); } } else { List<StringBuilder> clones = new ArrayList<StringBuilder>(); for (StringBuilder a : allAliases) { clones.add(new StringBuilder(a).append(" ").append( bit)); for (String alias : ales) { clones.add(new StringBuilder(a).append(" ").append( alias)); } } allAliases = clones; } } else { if (allAliases.size() == 0) { allAliases.add(new StringBuilder(bit)); } else { for (StringBuilder b : allAliases) { b.append(" ").append(bit); } } } } for (StringBuildermunge: allAliases) { result.add(munge.toString()); } return result; }28
  • 42. 29
  • 43. Scala Versiondef alias(query: String, aliases: String => List[String]): List[String] = { def recurse(prefix: List[String], remainder: List[String]): List[List[String]] = { remainder match { case Nil => prefix :: Nilcase head :: tail => aliases(head).flatMap(term => recurse(term :: prefix, tail)) }recurse(Nil,query.split(“\\s”).toList.reverse).map( _.mkString(“ “))}30
  • 44. 31
  • 45. Yeah yeahyeah but“Perl Whitespace Law” “Each line of perl should be surrounded by whitespace equivalent to what it would take to achieve the same functionality in a normal programming language.” -- Don HopkinsIf it compiles, it nearly works. Really.Visual Plane-Oriented ProgrammingI ♥ Idioms“But in Java, each little part is so very simple...”32
  • 46. 33
  • 48. “Weak developers will move heaven and earth to do the wrong thing. You can’t limit the damage they do by locking up the sharp tools. They’ll just swing the blunt tools harder.” – Glenn Vandenburg35
  • 49. @TomTomTestingMiddleware “Smart Content Switch”We needed it quickly...B2B/B2G Traffic Query EngineClustering AlgorithmDSL Templating EngineContent CleanupNext-Generation Geocoder36
  • 50. How To Sneak It InStart with TestingScalaCheckIS AWESOME.Specs, ScalaTest testing DSLsStart with something low riskOk, well, we didn’t do thatPrepare for steep learning curve...followed by a productivity hockey stick37
  • 51. Another Example8000 lines of broken Java -> 400 lines of broken Scala ->hyp.take(1).flatMap(_.dropDistricts) match { case Nil => hyp case head => {hyp.tail.foldLeft(head) {case (run: List[Hypothesis], h: Hypothesis) => { run.flatMap(_.merge(h)) match { case Nil => run case newRun => newRun.removeDuplicates} } } }}*Includes suggested improvements by Martin Odersky, I hope38
  • 52. TestingFunctional architect wrote a bunch of test cases, like:Requirement R.17:D1 -> N1, L1 -> N1, N1, D2 -> L2, L3, N3, D3 should cluster as (L1,N1,D1), (L2,D2), (L3), (N3), (D3)Vim-macro’d into:it should “satisfy R.17” in { cluster(D1 -> N1,L1 -> N1,N1,D2 -> L2,L3,N3,D3) should equal {groups(group(L1,N1,D1),group(L2,D2),group(L3),group(N3),group(D3)))}39
  • 53. ScalaCheckThe Dog’s BollocksSteep Learning Curveobject BinarySpecification extends Properties(“bin”) { specify(“toBinary”,Prop.forAllNoShrink( for{n <- Gen.choose(0d, 0.99999999d) m <- Gen.choose(20,31) } yield Pair(n, m)) { p => Math.abs(toDecimal(toBinary(p._1, p._2)) – p._1 < 1e-10 })}40
  • 54. So little time...TraitsCase classesPattern matchingStructural typesSelf typesXMLOptionCovariance/Contravariance@specialized41
  • 55. This slide left intentionally blank.