SlideShare a Scribd company logo
1 of 32
Download to read offline
Collections Pretty Slow
Scala World 2015, Penrith
BillVenners
Artima, Inc.
Escalate Software
Monday, September 21, 2015
•Work in progress
• In feature-equasets branch of scalatest repo
• Goal is to explore ways to simplify Scala
standard collections
Scalactic Collections
Monday, September 21, 2015
•Want to keep things stable
•Want to make things better
• Deprecate..., then remove
•Very rarely, break source
• Use needles not machetes
The Straight Jacket of Compatibility
Monday, September 21, 2015
// Case Insensitive String Wrapper
case class CIS(value: String) {
override def equals(other: Any): Boolean = {
other match {
case CIS(s) => s.toLowerCase == value.toLowerCase
case _ => false
}
}
override def hashCode: Int = value.toLowerCase.hashCode
}
scala> val ciSet = Set(CIS("hi"), CIS("HI"), CIS(" hi "))
ciSet: scala.collection.immutable.Set[CIS] = Set(CIS(hi), CIS( hi ))
Driving use case for “EquaSet”s:
Set with custom equality
Monday, September 21, 2015
// Whitespace Insensitive String Wrapper
case class WIS(value: String) {
override def equals(other: Any): Boolean = {
other match {
case WIS(s) => s.trim == value.trim
case _ => false
}
}
override def hashCode: Int = value.trim.hashCode
}
scala> val wiSet = Set(WIS("hi"), WIS("HI"), WIS(" hi "))
wiSet: scala.collection.immutable.Set[WIS] = Set(WIS(hi), WIS(HI))
Monday, September 21, 2015
scala> ciSet union Set(CIS("ha"), CIS("HA"), CIS(" ha "))
res1: scala.collection.immutable.Set[CIS] = Set(CIS(hi), CIS( hi ), CIS(ha), CIS( ha ))
scala> wiSet union Set(WIS("ha"),WIS("HA"),WIS(" ha "))
res2: scala.collection.immutable.Set[WIS] = Set(WIS(hi),WIS(HI),WIS(ha),WIS(HA))
scala> ciSet union wiSet
<console>:14: error: type mismatch;
found : scala.collection.immutable.Set[WIS]
required: scala.collection.GenSet[CIS]
ciSet union wiSet
^
What about union, intersect, diff?
Monday, September 21, 2015
Monday, September 21, 2015
Monday, September 21, 2015
You know how I’d do that...
Equality[E]
Collections[E]
Set[+T]
val ci: Collections[String] = ...
ci.Set[String](“hi”)
Monday, September 21, 2015
scala> import org.scalactic._
import org.scalactic._
scala> import StringNormalizations._
import StringNormalizations._
scala> val ci = Collections(lowerCased.toHashingEquality)
ci: org.scalactic.Collections[String] = org.scalactic.Collections@258c0257
scala> val wi = Collections(trimmed.toHashingEquality)
wi: org.scalactic.Collections[String] = org.scalactic.Collections@2b62cef9
scala> val ciSet = ci.Set("hi", "HI", " hi ")
ciSet: ci.immutable.inhabited.Set[String] = Set(hi, hi )
scala> val wiSet = wi.Set("hi", "HI", " hi ")
wiSet: wi.immutable.inhabited.Set[String] = Set(hi, HI)
Monday, September 21, 2015
scala> ciSet union ci.Set("ha", "HA", " ha ")
res0: ci.immutable.Set[String] = Set(hi, hi , ha, ha )
scala> wiSet union wi.Set("ha", "HA", " ha ")
res1: wi.immutable.Set[String] = Set(hi, HI, ha, HA)
scala> ciSet union wiSet
<console>:24: error: type mismatch;
found : wi.immutable.inhabited.Set[String]
required: ci.immutable.Set[?]
ciSet union wiSet
^
What about union, intersect, diff?
Monday, September 21, 2015
What about that plus sign? Set[+T]
Fruit
Orange
Valencia
Apple
Monday, September 21, 2015
scala> val orangeList = List(Orange(true), Orange(true))
orangeList: List[Orange] = List(Orange(true), Orange(true))
scala> Apple(true) :: orangeList
res1: List[Fruit] = List(Apple(true), Orange(true), Orange(true))
scala> 88 :: orangeList
res2: List[Any] = List(88, Orange(true), Orange(true))
Scala Lists are covariant
Monday, September 21, 2015
scala> val orangeSet = Set(Orange(true), Orange(true))
orangeSet: scala.collection.immutable.Set[Orange] = Set(Orange(true))
scala> orangeSet + Apple(true)
<console>:19: error: type mismatch;
found :Apple
required: Orange
orangeSet + Apple(true)
^
scala> val fruitSet = orangeSet.map(o => o: Fruit)
fruitSet: scala.collection.immutable.Set[Fruit] = Set(Orange(true))
scala> fruitSet + Apple(true)
res22: scala.collection.immutable.Set[Fruit] = Set(Orange(true),Apple(true))
Scala Sets are invariant
Monday, September 21, 2015
Intensional versus Extensional Sets
• Scala Set complects intensional and extensional,
so invariant
• Scalactic Set models extensional only,
so covariant
• Scalactic Membership models intensional only,
so contravariant
Monday, September 21, 2015
U >:T <: E
Equality[E]
Collections[E]
Set[+T]
Monday, September 21, 2015
scala> val fr = Collections[Fruit]
fr: org.scalactic.Collections[Fruit] = org.scalactic.Collections@70798f1a
scala> val valenciaSet = fr.Set(Valencia(true))
valenciaSet: fr.immutable.inhabited.Set[Valencia] = Set(Valencia(true))
scala> valenciaSet + Orange(true)
res4: fr.immutable.inhabited.Set[Orange] = Set(Valencia(true), Orange(true))
scala> valenciaSet + Apple(true)
res5: fr.immutable.inhabited.Set[Fruit] = Set(Valencia(true),Apple(true))
Scalactic Sets are covariant
(but with an upper bound)
Monday, September 21, 2015
scala> valenciaSet + 88
<console>:30: error: inferred type arguments [Any] do not conform to method +'s
type parameter bounds [U >:Valencia <: Fruit]
valenciaSet + 88
^
<console>:30: error: type mismatch;
found : Int(88)
required: U
valenciaSet + 88
^
So,Any not inferred here:
Monday, September 21, 2015
scala> val evenInts = Membership { (i: Int) => (i & 1) == 0 }
evenInts: org.scalactic.Membership[Int] = <membership>
scala> val oddInts = evenInts.complement
oddInts: org.scalactic.Membership[Int] = <membership>
scala> (evenInts(0), evenInts(1))
res0: (Boolean, Boolean) = (true,false)
scala> (oddInts(0), oddInts(1))
res1: (Boolean, Boolean) = (false,true)
scala> val allInts = oddInts union evenInts
allInts: org.scalactic.Membership[Int] = <membership>
scala> (allInts(0), allInts(1))
res2: (Boolean, Boolean) = (true,true)
Membership
has
Set
operations
intersect,
union,
diff,
complement
Monday, September 21, 2015
scala> val nonInts = Membership[AnyVal] { (o:AnyVal) => !o.isInstanceOf[Int] }
nonInts: org.scalactic.Membership[AnyVal] = <membership>
scala> nonInts(1)
res7: Boolean = false
scala> nonInts(1.0)
res9: Boolean = true
scala> nonInts: Membership[Int]
res10: org.scalactic.Membership[Int] = <membership>
scala> nonInts: Membership[Any]
<console>:12: error: type mismatch;
found : org.scalactic.Membership[AnyVal]
required: org.scalactic.Membership[Any]
nonInts: Membership[Any]
^
Scalactic
Memberships
are
contravariant
Monday, September 21, 2015
scala> Collections.default
res10: org.scalactic.Collections[Any] = org.scalactic.Collections@79eb4d3e
scala> import Collections.default._
import Collections.default._
scala> val dfSet = Set("hi", "HA", " ha ")
dfSet: org.scalactic.Collections.default.immutable.inhabited.Set[String] = Set(hi, HA, ha )
scala> dfSet + 88
res1: org.scalactic.Collections.default.immutable.inhabited.Set[Any] = Set(hi, HA, ha , 88)
Default collections like Scala standard
Monday, September 21, 2015
scala> val ciView = ciSet.map(_.length)
ciView: org.scalactic.views.inhabited.SetView[Int] = FastSetView(2,4)
scala> val wiView = wiSet.map(_.length)
wiView: org.scalactic.views.inhabited.SetView[Int] = FastSetView(2,2)
scala> ciView.force
res2: org.scalactic.Collections.default.immutable.inhabited.Set[Int] = Set(2, 4)
scala> wiView.force
res3: org.scalactic.Collections.default.immutable.inhabited.Set[Int] = Set(2)
map/flatmap/etc. are lazy
.force gives you Collections.default
Monday, September 21, 2015
scala> val wiBang = wiSet.map(_ + "!")
wiBang: org.scalactic.views.inhabited.SetView[String] = FastSetView(hi!,HI!)
scala> wiBang.force
res11: org.scalactic.Collections.default.immutable.inhabited.Set[String] = Set(hi!, HI!)
scala> wiBang.forceInto(wi)
res12: wi.immutable.inhabited.Set[String] = Set(hi!, HI!)
scala> wiBang.forceInto(ci)
res13: ci.immutable.inhabited.Set[String] = Set(hi!)
.forceInto lets you specify a non-default
Collections instance for the result
Monday, September 21, 2015
CanBuildFrom shows up rarely
• Use overriding and covariant return types
where possible
• Because transformation methods returnViews,
no need for CanBuildFrom in strict types
• Because of reduced inheritance, often don’t
need it inViews
• Sometimes, CanBuildFrom will show up in the
force and forceInto methods: e.g., MapView
• Puzzler: No BitSet in Scalactic Collections.
Monday, September 21, 2015
scala.collection.immutable
Set
«trait»
scala.collection.mutable
Set
«trait»
scala.collection
Set
«trait»
scala.collection.immutable
HashSet
scala.collection.mutable
HashSet
Rethinking Inheritance
Monday, September 21, 2015
scala.collection.immutable
Set
«trait»
scala.collection.mutable
Set
«trait»
scala.collection
Set
«trait»
scala.collection.immutable
HashSet
scala.collection.mutable
HashSet
Rethought Inheritance
Monday, September 21, 2015
• No Collections.Seq, Set, or Map that could be
either mutable or immutable
• SeqView does not extend immutable.Seq
•Views are transient helpers, and don’t have a
hierarchy
• I.e., HashSetView does not extend SetView
• Instead, there’s one XView for each X
Less Inheritance
Monday, September 21, 2015
Set
inhabited.Set
But “Inhabitedness” of immutables
modeled with Inheritance
Monday, September 21, 2015
Focus design on user experience,
not implementer experience
• No types solely for implementation in public
interface
•All types in public interface are for users
• How do you prevent bugs given code
duplication?
• Use code generation where helpful
•Test
Monday, September 21, 2015
Monday, September 21, 2015
Q => A
Monday, September 21, 2015
• Design for busy teams.
• Make it obvious, guessable, or easy to remember.
• Design for readers, then writers.
• Make errors impossible, or difficult.
• Exploit familiarity.
• Document with examples.
• Minimize redundancy.
• Maximize consistency.
• Use symbols when your users are already experts in them.
• Minimize the magic.
Simplicity in Scala Design
Monday, September 21, 2015

More Related Content

Viewers also liked

Agile Management 2013 - Nie tylko it
Agile Management 2013 - Nie tylko itAgile Management 2013 - Nie tylko it
Agile Management 2013 - Nie tylko itPiotr Burdylo
 
Metasepi team meeting #6: "Snatch-driven development"
Metasepi team meeting #6: "Snatch-driven development"Metasepi team meeting #6: "Snatch-driven development"
Metasepi team meeting #6: "Snatch-driven development"Kiwamu Okabe
 
Federated CDNs: What every service provider should know
Federated CDNs: What every service provider should knowFederated CDNs: What every service provider should know
Federated CDNs: What every service provider should knowPatrick Hurley
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
A Hitchhiker's Guide to the Inter-Cloud
A Hitchhiker's Guide to the Inter-CloudA Hitchhiker's Guide to the Inter-Cloud
A Hitchhiker's Guide to the Inter-CloudGovCloud Network
 
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...Maurice Naftalin
 
Sneaking Scala through the Back Door
Sneaking Scala through the Back DoorSneaking Scala through the Back Door
Sneaking Scala through the Back DoorDianne Marsh
 
Be careful when entering a casino (Agile by Example 2012)
Be careful when entering a casino (Agile by Example 2012)Be careful when entering a casino (Agile by Example 2012)
Be careful when entering a casino (Agile by Example 2012)Piotr Burdylo
 
Vert.x - JDD 2013 (English)
Vert.x - JDD 2013 (English)Vert.x - JDD 2013 (English)
Vert.x - JDD 2013 (English)Bartek Zdanowski
 
Protecting Java EE Web Apps with Secure HTTP Headers
Protecting Java EE Web Apps with Secure HTTP HeadersProtecting Java EE Web Apps with Secure HTTP Headers
Protecting Java EE Web Apps with Secure HTTP HeadersFrank Kim
 
Efficient Immutable Data Structures (Okasaki for Dummies)
Efficient Immutable Data Structures (Okasaki for Dummies)Efficient Immutable Data Structures (Okasaki for Dummies)
Efficient Immutable Data Structures (Okasaki for Dummies)Tom Faulhaber
 
Haskell is Not For Production and Other Tales
Haskell is Not For Production and Other TalesHaskell is Not For Production and Other Tales
Haskell is Not For Production and Other TalesKatie Ots
 
Software Dendrology by Brandon Bloom
Software Dendrology by Brandon BloomSoftware Dendrology by Brandon Bloom
Software Dendrology by Brandon BloomHakka Labs
 
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009Alexandre Morgaut
 
erlang at hover.in , Devcamp Blr 09
erlang at hover.in , Devcamp Blr 09erlang at hover.in , Devcamp Blr 09
erlang at hover.in , Devcamp Blr 09Bhasker Kode
 
O'Reilly ETech Conference: Laszlo RIA
O'Reilly ETech Conference: Laszlo RIAO'Reilly ETech Conference: Laszlo RIA
O'Reilly ETech Conference: Laszlo RIAOliver Steele
 
Jensimmons html5live-responsivedesign
Jensimmons html5live-responsivedesignJensimmons html5live-responsivedesign
Jensimmons html5live-responsivedesignJen Simmons
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance HaskellJohan Tibell
 

Viewers also liked (20)

Agile Management 2013 - Nie tylko it
Agile Management 2013 - Nie tylko itAgile Management 2013 - Nie tylko it
Agile Management 2013 - Nie tylko it
 
Metasepi team meeting #6: "Snatch-driven development"
Metasepi team meeting #6: "Snatch-driven development"Metasepi team meeting #6: "Snatch-driven development"
Metasepi team meeting #6: "Snatch-driven development"
 
Let's Get to the Rapids
Let's Get to the RapidsLet's Get to the Rapids
Let's Get to the Rapids
 
Federated CDNs: What every service provider should know
Federated CDNs: What every service provider should knowFederated CDNs: What every service provider should know
Federated CDNs: What every service provider should know
 
Monadologie
MonadologieMonadologie
Monadologie
 
A Hitchhiker's Guide to the Inter-Cloud
A Hitchhiker's Guide to the Inter-CloudA Hitchhiker's Guide to the Inter-Cloud
A Hitchhiker's Guide to the Inter-Cloud
 
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
 
Sneaking Scala through the Back Door
Sneaking Scala through the Back DoorSneaking Scala through the Back Door
Sneaking Scala through the Back Door
 
Be careful when entering a casino (Agile by Example 2012)
Be careful when entering a casino (Agile by Example 2012)Be careful when entering a casino (Agile by Example 2012)
Be careful when entering a casino (Agile by Example 2012)
 
Vert.x - JDD 2013 (English)
Vert.x - JDD 2013 (English)Vert.x - JDD 2013 (English)
Vert.x - JDD 2013 (English)
 
Protecting Java EE Web Apps with Secure HTTP Headers
Protecting Java EE Web Apps with Secure HTTP HeadersProtecting Java EE Web Apps with Secure HTTP Headers
Protecting Java EE Web Apps with Secure HTTP Headers
 
Efficient Immutable Data Structures (Okasaki for Dummies)
Efficient Immutable Data Structures (Okasaki for Dummies)Efficient Immutable Data Structures (Okasaki for Dummies)
Efficient Immutable Data Structures (Okasaki for Dummies)
 
Haskell is Not For Production and Other Tales
Haskell is Not For Production and Other TalesHaskell is Not For Production and Other Tales
Haskell is Not For Production and Other Tales
 
Software Dendrology by Brandon Bloom
Software Dendrology by Brandon BloomSoftware Dendrology by Brandon Bloom
Software Dendrology by Brandon Bloom
 
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009
 
erlang at hover.in , Devcamp Blr 09
erlang at hover.in , Devcamp Blr 09erlang at hover.in , Devcamp Blr 09
erlang at hover.in , Devcamp Blr 09
 
O'Reilly ETech Conference: Laszlo RIA
O'Reilly ETech Conference: Laszlo RIAO'Reilly ETech Conference: Laszlo RIA
O'Reilly ETech Conference: Laszlo RIA
 
Laszlo PyCon 2005
Laszlo PyCon 2005Laszlo PyCon 2005
Laszlo PyCon 2005
 
Jensimmons html5live-responsivedesign
Jensimmons html5live-responsivedesignJensimmons html5live-responsivedesign
Jensimmons html5live-responsivedesign
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 

Similar to Scala Collections Pretty Slow

Scalactic Collections
Scalactic CollectionsScalactic Collections
Scalactic Collectionsbvenners
 
Equality For All!
Equality For All!Equality For All!
Equality For All!bvenners
 
S.M.A.R.T. Biml - Standardize, Model, Automate, Reuse and Transform (SQLSatur...
S.M.A.R.T. Biml - Standardize, Model, Automate, Reuse and Transform (SQLSatur...S.M.A.R.T. Biml - Standardize, Model, Automate, Reuse and Transform (SQLSatur...
S.M.A.R.T. Biml - Standardize, Model, Automate, Reuse and Transform (SQLSatur...Cathrine Wilhelmsen
 
The Kotlin Programming Language, Svetlana Isakova
The Kotlin Programming Language, Svetlana IsakovaThe Kotlin Programming Language, Svetlana Isakova
The Kotlin Programming Language, Svetlana IsakovaVasil Remeniuk
 
Светлана Исакова «Язык Kotlin»
Светлана Исакова «Язык Kotlin»Светлана Исакова «Язык Kotlin»
Светлана Исакова «Язык Kotlin»e-Legion
 
Collections Framework
Collections FrameworkCollections Framework
Collections FrameworkSunil OS
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_FrameworkKrishna Sujeer
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With ScalaMeetu Maltiar
 
React table tutorial project setup, use table, and usefilter
React table tutorial project setup, use table, and usefilterReact table tutorial project setup, use table, and usefilter
React table tutorial project setup, use table, and usefilterKaty Slemon
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Languageleague
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDDShai Yallin
 
Introduction to .Net Driver
Introduction to .Net DriverIntroduction to .Net Driver
Introduction to .Net DriverDataStax Academy
 
Collection v3
Collection v3Collection v3
Collection v3Sunil OS
 
Should I Use Scalding or Scoobi or Scrunch?
Should I Use Scalding or Scoobi or Scrunch? Should I Use Scalding or Scoobi or Scrunch?
Should I Use Scalding or Scoobi or Scrunch? DataWorks Summit
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
Scala parallel-collections
Scala parallel-collectionsScala parallel-collections
Scala parallel-collectionsKnoldus Inc.
 
Scala parallel-collections
Scala parallel-collectionsScala parallel-collections
Scala parallel-collectionsKnoldus Inc.
 
Scalable Applications with Scala
Scalable Applications with ScalaScalable Applications with Scala
Scalable Applications with ScalaNimrod Argov
 
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapperSF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapperChester Chen
 

Similar to Scala Collections Pretty Slow (20)

Scalactic Collections
Scalactic CollectionsScalactic Collections
Scalactic Collections
 
Equality For All!
Equality For All!Equality For All!
Equality For All!
 
S.M.A.R.T. Biml - Standardize, Model, Automate, Reuse and Transform (SQLSatur...
S.M.A.R.T. Biml - Standardize, Model, Automate, Reuse and Transform (SQLSatur...S.M.A.R.T. Biml - Standardize, Model, Automate, Reuse and Transform (SQLSatur...
S.M.A.R.T. Biml - Standardize, Model, Automate, Reuse and Transform (SQLSatur...
 
The Kotlin Programming Language, Svetlana Isakova
The Kotlin Programming Language, Svetlana IsakovaThe Kotlin Programming Language, Svetlana Isakova
The Kotlin Programming Language, Svetlana Isakova
 
Светлана Исакова «Язык Kotlin»
Светлана Исакова «Язык Kotlin»Светлана Исакова «Язык Kotlin»
Светлана Исакова «Язык Kotlin»
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_Framework
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
React table tutorial project setup, use table, and usefilter
React table tutorial project setup, use table, and usefilterReact table tutorial project setup, use table, and usefilter
React table tutorial project setup, use table, and usefilter
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDD
 
Scala collection
Scala collectionScala collection
Scala collection
 
Introduction to .Net Driver
Introduction to .Net DriverIntroduction to .Net Driver
Introduction to .Net Driver
 
Collection v3
Collection v3Collection v3
Collection v3
 
Should I Use Scalding or Scoobi or Scrunch?
Should I Use Scalding or Scoobi or Scrunch? Should I Use Scalding or Scoobi or Scrunch?
Should I Use Scalding or Scoobi or Scrunch?
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
Scala parallel-collections
Scala parallel-collectionsScala parallel-collections
Scala parallel-collections
 
Scala parallel-collections
Scala parallel-collectionsScala parallel-collections
Scala parallel-collections
 
Scalable Applications with Scala
Scalable Applications with ScalaScalable Applications with Scala
Scalable Applications with Scala
 
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapperSF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
SF Scala meet up, lighting talk: SPA -- Scala JDBC wrapper
 

Recently uploaded

What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 

Recently uploaded (20)

Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 

Scala Collections Pretty Slow

  • 1. Collections Pretty Slow Scala World 2015, Penrith BillVenners Artima, Inc. Escalate Software Monday, September 21, 2015
  • 2. •Work in progress • In feature-equasets branch of scalatest repo • Goal is to explore ways to simplify Scala standard collections Scalactic Collections Monday, September 21, 2015
  • 3. •Want to keep things stable •Want to make things better • Deprecate..., then remove •Very rarely, break source • Use needles not machetes The Straight Jacket of Compatibility Monday, September 21, 2015
  • 4. // Case Insensitive String Wrapper case class CIS(value: String) { override def equals(other: Any): Boolean = { other match { case CIS(s) => s.toLowerCase == value.toLowerCase case _ => false } } override def hashCode: Int = value.toLowerCase.hashCode } scala> val ciSet = Set(CIS("hi"), CIS("HI"), CIS(" hi ")) ciSet: scala.collection.immutable.Set[CIS] = Set(CIS(hi), CIS( hi )) Driving use case for “EquaSet”s: Set with custom equality Monday, September 21, 2015
  • 5. // Whitespace Insensitive String Wrapper case class WIS(value: String) { override def equals(other: Any): Boolean = { other match { case WIS(s) => s.trim == value.trim case _ => false } } override def hashCode: Int = value.trim.hashCode } scala> val wiSet = Set(WIS("hi"), WIS("HI"), WIS(" hi ")) wiSet: scala.collection.immutable.Set[WIS] = Set(WIS(hi), WIS(HI)) Monday, September 21, 2015
  • 6. scala> ciSet union Set(CIS("ha"), CIS("HA"), CIS(" ha ")) res1: scala.collection.immutable.Set[CIS] = Set(CIS(hi), CIS( hi ), CIS(ha), CIS( ha )) scala> wiSet union Set(WIS("ha"),WIS("HA"),WIS(" ha ")) res2: scala.collection.immutable.Set[WIS] = Set(WIS(hi),WIS(HI),WIS(ha),WIS(HA)) scala> ciSet union wiSet <console>:14: error: type mismatch; found : scala.collection.immutable.Set[WIS] required: scala.collection.GenSet[CIS] ciSet union wiSet ^ What about union, intersect, diff? Monday, September 21, 2015
  • 9. You know how I’d do that... Equality[E] Collections[E] Set[+T] val ci: Collections[String] = ... ci.Set[String](“hi”) Monday, September 21, 2015
  • 10. scala> import org.scalactic._ import org.scalactic._ scala> import StringNormalizations._ import StringNormalizations._ scala> val ci = Collections(lowerCased.toHashingEquality) ci: org.scalactic.Collections[String] = org.scalactic.Collections@258c0257 scala> val wi = Collections(trimmed.toHashingEquality) wi: org.scalactic.Collections[String] = org.scalactic.Collections@2b62cef9 scala> val ciSet = ci.Set("hi", "HI", " hi ") ciSet: ci.immutable.inhabited.Set[String] = Set(hi, hi ) scala> val wiSet = wi.Set("hi", "HI", " hi ") wiSet: wi.immutable.inhabited.Set[String] = Set(hi, HI) Monday, September 21, 2015
  • 11. scala> ciSet union ci.Set("ha", "HA", " ha ") res0: ci.immutable.Set[String] = Set(hi, hi , ha, ha ) scala> wiSet union wi.Set("ha", "HA", " ha ") res1: wi.immutable.Set[String] = Set(hi, HI, ha, HA) scala> ciSet union wiSet <console>:24: error: type mismatch; found : wi.immutable.inhabited.Set[String] required: ci.immutable.Set[?] ciSet union wiSet ^ What about union, intersect, diff? Monday, September 21, 2015
  • 12. What about that plus sign? Set[+T] Fruit Orange Valencia Apple Monday, September 21, 2015
  • 13. scala> val orangeList = List(Orange(true), Orange(true)) orangeList: List[Orange] = List(Orange(true), Orange(true)) scala> Apple(true) :: orangeList res1: List[Fruit] = List(Apple(true), Orange(true), Orange(true)) scala> 88 :: orangeList res2: List[Any] = List(88, Orange(true), Orange(true)) Scala Lists are covariant Monday, September 21, 2015
  • 14. scala> val orangeSet = Set(Orange(true), Orange(true)) orangeSet: scala.collection.immutable.Set[Orange] = Set(Orange(true)) scala> orangeSet + Apple(true) <console>:19: error: type mismatch; found :Apple required: Orange orangeSet + Apple(true) ^ scala> val fruitSet = orangeSet.map(o => o: Fruit) fruitSet: scala.collection.immutable.Set[Fruit] = Set(Orange(true)) scala> fruitSet + Apple(true) res22: scala.collection.immutable.Set[Fruit] = Set(Orange(true),Apple(true)) Scala Sets are invariant Monday, September 21, 2015
  • 15. Intensional versus Extensional Sets • Scala Set complects intensional and extensional, so invariant • Scalactic Set models extensional only, so covariant • Scalactic Membership models intensional only, so contravariant Monday, September 21, 2015
  • 16. U >:T <: E Equality[E] Collections[E] Set[+T] Monday, September 21, 2015
  • 17. scala> val fr = Collections[Fruit] fr: org.scalactic.Collections[Fruit] = org.scalactic.Collections@70798f1a scala> val valenciaSet = fr.Set(Valencia(true)) valenciaSet: fr.immutable.inhabited.Set[Valencia] = Set(Valencia(true)) scala> valenciaSet + Orange(true) res4: fr.immutable.inhabited.Set[Orange] = Set(Valencia(true), Orange(true)) scala> valenciaSet + Apple(true) res5: fr.immutable.inhabited.Set[Fruit] = Set(Valencia(true),Apple(true)) Scalactic Sets are covariant (but with an upper bound) Monday, September 21, 2015
  • 18. scala> valenciaSet + 88 <console>:30: error: inferred type arguments [Any] do not conform to method +'s type parameter bounds [U >:Valencia <: Fruit] valenciaSet + 88 ^ <console>:30: error: type mismatch; found : Int(88) required: U valenciaSet + 88 ^ So,Any not inferred here: Monday, September 21, 2015
  • 19. scala> val evenInts = Membership { (i: Int) => (i & 1) == 0 } evenInts: org.scalactic.Membership[Int] = <membership> scala> val oddInts = evenInts.complement oddInts: org.scalactic.Membership[Int] = <membership> scala> (evenInts(0), evenInts(1)) res0: (Boolean, Boolean) = (true,false) scala> (oddInts(0), oddInts(1)) res1: (Boolean, Boolean) = (false,true) scala> val allInts = oddInts union evenInts allInts: org.scalactic.Membership[Int] = <membership> scala> (allInts(0), allInts(1)) res2: (Boolean, Boolean) = (true,true) Membership has Set operations intersect, union, diff, complement Monday, September 21, 2015
  • 20. scala> val nonInts = Membership[AnyVal] { (o:AnyVal) => !o.isInstanceOf[Int] } nonInts: org.scalactic.Membership[AnyVal] = <membership> scala> nonInts(1) res7: Boolean = false scala> nonInts(1.0) res9: Boolean = true scala> nonInts: Membership[Int] res10: org.scalactic.Membership[Int] = <membership> scala> nonInts: Membership[Any] <console>:12: error: type mismatch; found : org.scalactic.Membership[AnyVal] required: org.scalactic.Membership[Any] nonInts: Membership[Any] ^ Scalactic Memberships are contravariant Monday, September 21, 2015
  • 21. scala> Collections.default res10: org.scalactic.Collections[Any] = org.scalactic.Collections@79eb4d3e scala> import Collections.default._ import Collections.default._ scala> val dfSet = Set("hi", "HA", " ha ") dfSet: org.scalactic.Collections.default.immutable.inhabited.Set[String] = Set(hi, HA, ha ) scala> dfSet + 88 res1: org.scalactic.Collections.default.immutable.inhabited.Set[Any] = Set(hi, HA, ha , 88) Default collections like Scala standard Monday, September 21, 2015
  • 22. scala> val ciView = ciSet.map(_.length) ciView: org.scalactic.views.inhabited.SetView[Int] = FastSetView(2,4) scala> val wiView = wiSet.map(_.length) wiView: org.scalactic.views.inhabited.SetView[Int] = FastSetView(2,2) scala> ciView.force res2: org.scalactic.Collections.default.immutable.inhabited.Set[Int] = Set(2, 4) scala> wiView.force res3: org.scalactic.Collections.default.immutable.inhabited.Set[Int] = Set(2) map/flatmap/etc. are lazy .force gives you Collections.default Monday, September 21, 2015
  • 23. scala> val wiBang = wiSet.map(_ + "!") wiBang: org.scalactic.views.inhabited.SetView[String] = FastSetView(hi!,HI!) scala> wiBang.force res11: org.scalactic.Collections.default.immutable.inhabited.Set[String] = Set(hi!, HI!) scala> wiBang.forceInto(wi) res12: wi.immutable.inhabited.Set[String] = Set(hi!, HI!) scala> wiBang.forceInto(ci) res13: ci.immutable.inhabited.Set[String] = Set(hi!) .forceInto lets you specify a non-default Collections instance for the result Monday, September 21, 2015
  • 24. CanBuildFrom shows up rarely • Use overriding and covariant return types where possible • Because transformation methods returnViews, no need for CanBuildFrom in strict types • Because of reduced inheritance, often don’t need it inViews • Sometimes, CanBuildFrom will show up in the force and forceInto methods: e.g., MapView • Puzzler: No BitSet in Scalactic Collections. Monday, September 21, 2015
  • 27. • No Collections.Seq, Set, or Map that could be either mutable or immutable • SeqView does not extend immutable.Seq •Views are transient helpers, and don’t have a hierarchy • I.e., HashSetView does not extend SetView • Instead, there’s one XView for each X Less Inheritance Monday, September 21, 2015
  • 28. Set inhabited.Set But “Inhabitedness” of immutables modeled with Inheritance Monday, September 21, 2015
  • 29. Focus design on user experience, not implementer experience • No types solely for implementation in public interface •All types in public interface are for users • How do you prevent bugs given code duplication? • Use code generation where helpful •Test Monday, September 21, 2015
  • 31. Q => A Monday, September 21, 2015
  • 32. • Design for busy teams. • Make it obvious, guessable, or easy to remember. • Design for readers, then writers. • Make errors impossible, or difficult. • Exploit familiarity. • Document with examples. • Minimize redundancy. • Maximize consistency. • Use symbols when your users are already experts in them. • Minimize the magic. Simplicity in Scala Design Monday, September 21, 2015