SlideShare une entreprise Scribd logo
1  sur  25
Télécharger pour lire hors ligne
Scala Collections




iBAT Session > June 17' 2011 > Meetu Maltiar
An Example
package com.inphina.collections.person

class Person(val name: String, val age: Int) {

 }

Somewhere in the code:
val (minors, adults) = people partition (_.age < 18)

Three concepts:
   - Simple pattern matching
   - An Infix method call
   - A function value
The Scala Way Of Collections
scala> val ys = List(1, 2, 3)
ys: List[Int] = List(1, 2, 3)

scala> val xs: Seq[Int] = ys
xs: Seq[Int] = List(1, 2, 3)

scala> xs map (_ + 1)
res0: Seq[Int] = List(2, 3, 4)

scala> ys map (_ + 1)
res1: List[Int] = List(2, 3, 4)
Collection Properties
Object-oriented

Generic: List[T], Map[K, V]

optionally persistent: scala.collections.immutable

Higher-order with methods like foreach, map, filter

uniform return type principle: operations return
collections of same type as their left operand
The Uniform Return Type Principle
Bulk operations return collections of the same type (constructor) as
their left operand.

scala> val ys = List(1,2,3)
ys: List[Int] = List(1, 2, 3)

scala> val xs: Seq[Int] = ys
xs: Seq[Int] = List(1, 2, 3)

scala> xs map(_ + 1)
res0: Seq[Int] = List(2, 3, 4)

scala> ys map(_ + 1)
res1: List[Int] = List(2, 3, 4)
Using Collections: Map and Filter
scala> val xs = List(1, 2, 3)
xs: List[Int] = List(1, 2, 3)

scala> val ys = xs map (x => x + 1)
ys: List[Int] = List(2, 3, 4)

scala> val ys = xs map (_ + 1)
ys: List[Int] = List(2, 3, 4)

scala> val zs = ys filter (_ % 2 == 0)
zs: List[Int] = List(2 , 4)

scala> val as = ys map (0 to _)
as: List[scala.collection.immutable.Range.Inclusive] =
List(Range(0, 1), Range(0, 1, 2), Range(0, 1, 2, 3))
Using Collections: flatMap and groupBy
scala> val bs = as.flatten
bs: List[Int] = List(0, 1, 0, 1, 2, 0, 1, 2, 3)

scala> val bs = ys flatMap (0 to _)
bs: List[Int] = List(0, 1, 0, 1, 2, 0, 1, 2, 3)

scala> val fruit = Vector("apples", "oranges", "ananas")
fruit: scala.collection.immutable.Vector[java.lang.String] =
Vector(apples, oranges, ananas)

scala> fruit groupBy (_.head)
res3:
scala.collection.immutable.Map[Char,scala.collection.immutable.Ve
ctor[java.lang.String]] = Map(a -> Vector(apples, ananas), o ->
Vector(oranges))
Using Collections: For Notation

scala> for (x <- xs) yield x + 1                   // map
res7: Seq[Int] = List(2, 3, 4)

scala> for (x <- res7 if x % 2 == 0 ) yield x      // filter
res8: Seq[Int] = List(2, 4)

scala> for (x <- xs; y <- 0 to x) yield y          // flatMap
res9: Seq[Int] = List(0, 1, 0, 1, 2, 0, 1, 2, 3)
String also a Collection

Even String is also a collection that means we can
apply higher order functions on it.

scala> val aString = "hello world"
aString: java.lang.String = hello world

scala> aString map (_.toUpper)
res12: String = HELLO WORLD
Using Maps
scala> val m = Map(1 -> "ABC", 2 -> "DEF", 3 -> "GHI")
m: scala.collection.immutable.Map[Int,java.lang.String] = Map(1 -> ABC,
2 -> DEF, 3 -> GHI)

scala> m(2)
res10: java.lang.String = DEF

scala> m + (4 -> "JKL")
res11: scala.collection.immutable.Map[Int,java.lang.String] = Map(1 ->
ABC, 2 -> DEF, 3 -> GHI, 4 -> JKL)

scala> m map {case (k, v) => (v, k)}
res12: scala.collection.immutable.Map[java.lang.String,Int] = Map(ABC
-> 1, DEF -> 2, GHI -> 3)
Scala Collection Hierarchy

All Collection classes are in scala.collection or one of its sub-
packages mutable, immutable and generic

Root collections in the package scala.collection define the
same interface as immutable collections and mutable
collections add some modification operations to make it
mutable

The generic package contains building block for implementing
Collections.
Scala.Collection Hierarchy
Scala.Collection.Immutable
Scala.Collection.Mutable
Overview Of Collections
Traversable
   Iterable
        Seq
             IndexedSeq
             LinearSeq
             mutable.Buffer
             Range
        Set
            SortedSet
            immutable.HashSet
            mutable.HashSet
            mutable.LinkedHashSet
            BitSet
        Map
            SortedMap
            immutable.HashMap
            mutable.HashMap
            mutable.LinkedHashMap
Commonality In collections
All classes are quite common. For instance, every
kind of collection can be created by same uniform
Syntax
   Traversable(1, 2, 3)
   Iterable("x", "y", "z")
   Map("x" -> 24, "y" -> 25, "z" -> 26)
   Set(Color.red, Color.green, Color.blue)
   SortedSet("hello", "world")
   Buffer(x, y, z)
   IndexedSeq(1.0, 2.0)
   LinearSeq(a, b, c)

The same principle applies with specific collection implementations
   List(1, 2, 3)
   HashMap("x" -> 24, "y" -> 25, "z" -> 26)
Commonality In collections...
All these Collections get displayed with toString in the same way
they are written above

All collections support the API provided by Traversable but
specializes types wherever it makes sense

map method in class Traversable returns another Traversable as
its result. But this result type is overridden in subclasses

     scala> List(1, 2, 3) map (_ + 1)
     res0: List[Int] = List(2, 3, 4)
     scala> Set(1, 2, 3) map (_ * 2)
     res0: Set[Int] = Set(2, 4, 6)

This behavior in the collections libraries is called the uniform return type principle.
Trait Traversable
Top of collection hierarchy. Its abstract method is foreach:
def foreach[U](f: Elem => U)

Traversable also provides lot of concrete methods they fall in
following categories
  Addition: ++, appends two traversables together
  Map operations: map, flatMap, and collect
  Conversions: toArray, toList, toIterable, toSeq, toIndexedSeq, toStream, toSet, toMap,
  Copying operations: copyToBuffer and copyToArray
  Size info operations: isEmpty, nonEmpty, size, and hasDefiniteSize
  Element retrieval operations: head, last, headOption, lastOption, and find
  Sub-Collection retrieval operations: tail, init, slice, take, drop, takeWhile,
                                             dropWhile, filter, filterNot, withFilter
  Subdivision operations: splitAt, span, partition, groupBy
  Element tests: exists, forall, count
  Folds: foldLeft, foldRight, /:, :, reduceLeft, reduceRight
  Specific Folds: sum, product, min, max
  String Operations: mkString, addString, stringPrefix
Trait Iterable

This is the next Trait in Collection hierarchy. All methods in this trait
are defined in terms of an abstract method iterator, which returns
collection results one by one.

The foreach method from trait Traversable is implemented in
Iterable in terms of iterator

def foreach[U](f: Elem => U): Unit = {
  val it = iterator
  while (it.hasNext) f(it.next())
}
An Example ..
Task: Phone keys has mnemonics attached to them

val mnemonics = Map(
        '2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL", '6' -> "MNO",
        '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ")

Assume that you are given a dictionary dict as a list of words. Design a
class Coder with a method translate such that
    new Coder(dict).translate(phoneNumber)

Produces all phrases of words which can serve as mnemonics for the
phone number

Example The phone number “7225276257” should have a mnemonic
  Scala rocks
as one element of the list of solution phrases
An Example
Creating a mnemonics Map
val mnemonics = Map('2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ")

Creating reverse map with upper code
val upperCode: Map[Char, Char] = for ((digit, str) <- mnemonics; ltr <- str)
yield (ltr -> digit)

Creating a word for which digit string will be generated
val word = "java"

Gettng a digit string from a word eg; "java" -> "5282"
word.toUpperCase map (upperCode(_))
Everything is a library

Collections feel they are language constructs
Language does not contain any collection related
constructs
   - no collection types
   - no collection literals
   - no collection operators
Everything is in library
They are extensible
Conclusion

De-emphasize destructive updates

Focus on Transformers that map collections to collections

Have complete range of persistent collections
Next Steps

Making and extending Scala Collection

Parallel Collections:
Move from Mutable → Persistent → Parallel
References



Martin Odersky's talk at Parleys

Scala Collection documentation

Contenu connexe

Tendances

Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In ScalaKnoldus Inc.
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from javaIndicThreads
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance HaskellJohan Tibell
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaVladimir Kostyukov
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Philip Schwarz
 
7 Habits For a More Functional Swift
7 Habits For a More Functional Swift7 Habits For a More Functional Swift
7 Habits For a More Functional SwiftJason Larsen
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Mattersromanandreg
 
Effective way to code in Scala
Effective way to code in ScalaEffective way to code in Scala
Effective way to code in ScalaKnoldus Inc.
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Bryan O'Sullivan
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsKirill Kozlov
 

Tendances (19)

ScalaBlitz
ScalaBlitzScalaBlitz
ScalaBlitz
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from java
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
 
ScalaMeter 2014
ScalaMeter 2014ScalaMeter 2014
ScalaMeter 2014
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
 
Map, Reduce and Filter in Swift
Map, Reduce and Filter in SwiftMap, Reduce and Filter in Swift
Map, Reduce and Filter in Swift
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
 
7 Habits For a More Functional Swift
7 Habits For a More Functional Swift7 Habits For a More Functional Swift
7 Habits For a More Functional Swift
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Effective way to code in Scala
Effective way to code in ScalaEffective way to code in Scala
Effective way to code in Scala
 
Meet scala
Meet scalaMeet scala
Meet scala
 
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 

En vedette

Spark - The Ultimate Scala Collections by Martin Odersky
Spark - The Ultimate Scala Collections by Martin OderskySpark - The Ultimate Scala Collections by Martin Odersky
Spark - The Ultimate Scala Collections by Martin OderskySpark Summit
 
Type Parameterization
Type ParameterizationType Parameterization
Type ParameterizationKnoldus Inc.
 
Effective Programming In Scala
Effective Programming In ScalaEffective Programming In Scala
Effective Programming In ScalaHarsh Sharma
 
Simple Scala DSLs
Simple Scala DSLsSimple Scala DSLs
Simple Scala DSLslinxbetter
 
So various polymorphism in Scala
So various polymorphism in ScalaSo various polymorphism in Scala
So various polymorphism in Scalab0ris_1
 
Real-World Scala Design Patterns
Real-World Scala Design PatternsReal-World Scala Design Patterns
Real-World Scala Design PatternsNLJUG
 
Scala Implicits - Not to be feared
Scala Implicits - Not to be fearedScala Implicits - Not to be feared
Scala Implicits - Not to be fearedDerek Wyatt
 
Types by Adform Research, Saulius Valatka
Types by Adform Research, Saulius ValatkaTypes by Adform Research, Saulius Valatka
Types by Adform Research, Saulius ValatkaVasil Remeniuk
 
Variance in scala
Variance in scalaVariance in scala
Variance in scalaLyleK
 
Scala Types of Types @ Lambda Days
Scala Types of Types @ Lambda DaysScala Types of Types @ Lambda Days
Scala Types of Types @ Lambda DaysKonrad Malawski
 
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...Data Con LA
 
Python and Bigdata - An Introduction to Spark (PySpark)
Python and Bigdata -  An Introduction to Spark (PySpark)Python and Bigdata -  An Introduction to Spark (PySpark)
Python and Bigdata - An Introduction to Spark (PySpark)hiteshnd
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in ScalaPatrick Nicolas
 
Introduction to Spark Internals
Introduction to Spark InternalsIntroduction to Spark Internals
Introduction to Spark InternalsPietro Michiardi
 
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...Databricks
 
DTCC '14 Spark Runtime Internals
DTCC '14 Spark Runtime InternalsDTCC '14 Spark Runtime Internals
DTCC '14 Spark Runtime InternalsCheng Lian
 
Demystifying Scala Type System
Demystifying Scala Type SystemDemystifying Scala Type System
Demystifying Scala Type SystemDavid Galichet
 
Tuning and Debugging in Apache Spark
Tuning and Debugging in Apache SparkTuning and Debugging in Apache Spark
Tuning and Debugging in Apache SparkPatrick Wendell
 

En vedette (20)

Spark - The Ultimate Scala Collections by Martin Odersky
Spark - The Ultimate Scala Collections by Martin OderskySpark - The Ultimate Scala Collections by Martin Odersky
Spark - The Ultimate Scala Collections by Martin Odersky
 
Colecciones en Scala
Colecciones en ScalaColecciones en Scala
Colecciones en Scala
 
Type Parameterization
Type ParameterizationType Parameterization
Type Parameterization
 
Effective Programming In Scala
Effective Programming In ScalaEffective Programming In Scala
Effective Programming In Scala
 
Simple Scala DSLs
Simple Scala DSLsSimple Scala DSLs
Simple Scala DSLs
 
So various polymorphism in Scala
So various polymorphism in ScalaSo various polymorphism in Scala
So various polymorphism in Scala
 
Scala’s implicits
Scala’s implicitsScala’s implicits
Scala’s implicits
 
Real-World Scala Design Patterns
Real-World Scala Design PatternsReal-World Scala Design Patterns
Real-World Scala Design Patterns
 
Scala Implicits - Not to be feared
Scala Implicits - Not to be fearedScala Implicits - Not to be feared
Scala Implicits - Not to be feared
 
Types by Adform Research, Saulius Valatka
Types by Adform Research, Saulius ValatkaTypes by Adform Research, Saulius Valatka
Types by Adform Research, Saulius Valatka
 
Variance in scala
Variance in scalaVariance in scala
Variance in scala
 
Scala Types of Types @ Lambda Days
Scala Types of Types @ Lambda DaysScala Types of Types @ Lambda Days
Scala Types of Types @ Lambda Days
 
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
Big Data Day LA 2016/ Hadoop/ Spark/ Kafka track - Data Provenance Support in...
 
Python and Bigdata - An Introduction to Spark (PySpark)
Python and Bigdata -  An Introduction to Spark (PySpark)Python and Bigdata -  An Introduction to Spark (PySpark)
Python and Bigdata - An Introduction to Spark (PySpark)
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in Scala
 
Introduction to Spark Internals
Introduction to Spark InternalsIntroduction to Spark Internals
Introduction to Spark Internals
 
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...
 
DTCC '14 Spark Runtime Internals
DTCC '14 Spark Runtime InternalsDTCC '14 Spark Runtime Internals
DTCC '14 Spark Runtime Internals
 
Demystifying Scala Type System
Demystifying Scala Type SystemDemystifying Scala Type System
Demystifying Scala Type System
 
Tuning and Debugging in Apache Spark
Tuning and Debugging in Apache SparkTuning and Debugging in Apache Spark
Tuning and Debugging in Apache Spark
 

Similaire à Scala collections

Introduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkIntroduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkAngelo Leto
 
Short Reference Card for R users.
Short Reference Card for R users.Short Reference Card for R users.
Short Reference Card for R users.Dr. Volkan OBAN
 
Functional programming with_scala
Functional programming with_scalaFunctional programming with_scala
Functional programming with_scalaRaymond Tay
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavVyacheslav Arbuzov
 
R command cheatsheet.pdf
R command cheatsheet.pdfR command cheatsheet.pdf
R command cheatsheet.pdfNgcnh947953
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.jstimourian
 
Multiclass Logistic Regression: Derivation and Apache Spark Examples
Multiclass Logistic Regression: Derivation and Apache Spark ExamplesMulticlass Logistic Regression: Derivation and Apache Spark Examples
Multiclass Logistic Regression: Derivation and Apache Spark ExamplesMarjan Sterjev
 
Scala parallel-collections
Scala parallel-collectionsScala parallel-collections
Scala parallel-collectionsKnoldus Inc.
 

Similaire à Scala collections (20)

Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
Practical cats
Practical catsPractical cats
Practical cats
 
Introduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkIntroduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with spark
 
Reference card for R
Reference card for RReference card for R
Reference card for R
 
Short Reference Card for R users.
Short Reference Card for R users.Short Reference Card for R users.
Short Reference Card for R users.
 
Functional programming with_scala
Functional programming with_scalaFunctional programming with_scala
Functional programming with_scala
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
 
R command cheatsheet.pdf
R command cheatsheet.pdfR command cheatsheet.pdf
R command cheatsheet.pdf
 
@ R reference
@ R reference@ R reference
@ R reference
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.js
 
R gráfico
R gráficoR gráfico
R gráfico
 
tidyr.pdf
tidyr.pdftidyr.pdf
tidyr.pdf
 
Multiclass Logistic Regression: Derivation and Apache Spark Examples
Multiclass Logistic Regression: Derivation and Apache Spark ExamplesMulticlass Logistic Regression: Derivation and Apache Spark Examples
Multiclass Logistic Regression: Derivation and Apache Spark Examples
 
Frp2016 3
Frp2016 3Frp2016 3
Frp2016 3
 
Scala parallel-collections
Scala parallel-collectionsScala parallel-collections
Scala parallel-collections
 
Scala Introduction
Scala IntroductionScala Introduction
Scala Introduction
 
Statistics lab 1
Statistics lab 1Statistics lab 1
Statistics lab 1
 
R교육1
R교육1R교육1
R교육1
 
20170509 rand db_lesugent
20170509 rand db_lesugent20170509 rand db_lesugent
20170509 rand db_lesugent
 

Plus de Inphina Technologies (13)

Scala test
Scala testScala test
Scala test
 
Easy ORMness with Objectify-Appengine
Easy ORMness with Objectify-AppengineEasy ORMness with Objectify-Appengine
Easy ORMness with Objectify-Appengine
 
Cloud Foundry Impressions
Cloud Foundry Impressions Cloud Foundry Impressions
Cloud Foundry Impressions
 
Cloud slam2011 multi-tenancy
Cloud slam2011 multi-tenancyCloud slam2011 multi-tenancy
Cloud slam2011 multi-tenancy
 
Google appenginemigrationcasestudy
Google appenginemigrationcasestudyGoogle appenginemigrationcasestudy
Google appenginemigrationcasestudy
 
Preparing yourdataforcloud
Preparing yourdataforcloudPreparing yourdataforcloud
Preparing yourdataforcloud
 
Multi-Tenancy in the Cloud
Multi-Tenancy in the CloudMulti-Tenancy in the Cloud
Multi-Tenancy in the Cloud
 
Inphina at a glance
Inphina at a glanceInphina at a glance
Inphina at a glance
 
Inphina cloud
Inphina cloudInphina cloud
Inphina cloud
 
Multi-tenancy in the cloud
Multi-tenancy in the cloudMulti-tenancy in the cloud
Multi-tenancy in the cloud
 
Testing your application on Google App Engine
Testing your application on Google App EngineTesting your application on Google App Engine
Testing your application on Google App Engine
 
Preparing your data for the cloud
Preparing your data for the cloudPreparing your data for the cloud
Preparing your data for the cloud
 
Getting started with jClouds
Getting started with jCloudsGetting started with jClouds
Getting started with jClouds
 

Dernier

Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
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...Miguel Araújo
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
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 Processorsdebabhi2
 
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 Nanonetsnaman860154
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
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 AutomationSafe Software
 
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 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
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.pdfEnterprise Knowledge
 
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 DevelopmentsTrustArc
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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 2024Rafal Los
 
[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.pdfhans926745
 

Dernier (20)

Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
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
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
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
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
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
 
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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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
 
[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
 

Scala collections

  • 1. Scala Collections iBAT Session > June 17' 2011 > Meetu Maltiar
  • 2. An Example package com.inphina.collections.person class Person(val name: String, val age: Int) { } Somewhere in the code: val (minors, adults) = people partition (_.age < 18) Three concepts: - Simple pattern matching - An Infix method call - A function value
  • 3. The Scala Way Of Collections scala> val ys = List(1, 2, 3) ys: List[Int] = List(1, 2, 3) scala> val xs: Seq[Int] = ys xs: Seq[Int] = List(1, 2, 3) scala> xs map (_ + 1) res0: Seq[Int] = List(2, 3, 4) scala> ys map (_ + 1) res1: List[Int] = List(2, 3, 4)
  • 4. Collection Properties Object-oriented Generic: List[T], Map[K, V] optionally persistent: scala.collections.immutable Higher-order with methods like foreach, map, filter uniform return type principle: operations return collections of same type as their left operand
  • 5. The Uniform Return Type Principle Bulk operations return collections of the same type (constructor) as their left operand. scala> val ys = List(1,2,3) ys: List[Int] = List(1, 2, 3) scala> val xs: Seq[Int] = ys xs: Seq[Int] = List(1, 2, 3) scala> xs map(_ + 1) res0: Seq[Int] = List(2, 3, 4) scala> ys map(_ + 1) res1: List[Int] = List(2, 3, 4)
  • 6. Using Collections: Map and Filter scala> val xs = List(1, 2, 3) xs: List[Int] = List(1, 2, 3) scala> val ys = xs map (x => x + 1) ys: List[Int] = List(2, 3, 4) scala> val ys = xs map (_ + 1) ys: List[Int] = List(2, 3, 4) scala> val zs = ys filter (_ % 2 == 0) zs: List[Int] = List(2 , 4) scala> val as = ys map (0 to _) as: List[scala.collection.immutable.Range.Inclusive] = List(Range(0, 1), Range(0, 1, 2), Range(0, 1, 2, 3))
  • 7. Using Collections: flatMap and groupBy scala> val bs = as.flatten bs: List[Int] = List(0, 1, 0, 1, 2, 0, 1, 2, 3) scala> val bs = ys flatMap (0 to _) bs: List[Int] = List(0, 1, 0, 1, 2, 0, 1, 2, 3) scala> val fruit = Vector("apples", "oranges", "ananas") fruit: scala.collection.immutable.Vector[java.lang.String] = Vector(apples, oranges, ananas) scala> fruit groupBy (_.head) res3: scala.collection.immutable.Map[Char,scala.collection.immutable.Ve ctor[java.lang.String]] = Map(a -> Vector(apples, ananas), o -> Vector(oranges))
  • 8. Using Collections: For Notation scala> for (x <- xs) yield x + 1 // map res7: Seq[Int] = List(2, 3, 4) scala> for (x <- res7 if x % 2 == 0 ) yield x // filter res8: Seq[Int] = List(2, 4) scala> for (x <- xs; y <- 0 to x) yield y // flatMap res9: Seq[Int] = List(0, 1, 0, 1, 2, 0, 1, 2, 3)
  • 9. String also a Collection Even String is also a collection that means we can apply higher order functions on it. scala> val aString = "hello world" aString: java.lang.String = hello world scala> aString map (_.toUpper) res12: String = HELLO WORLD
  • 10. Using Maps scala> val m = Map(1 -> "ABC", 2 -> "DEF", 3 -> "GHI") m: scala.collection.immutable.Map[Int,java.lang.String] = Map(1 -> ABC, 2 -> DEF, 3 -> GHI) scala> m(2) res10: java.lang.String = DEF scala> m + (4 -> "JKL") res11: scala.collection.immutable.Map[Int,java.lang.String] = Map(1 -> ABC, 2 -> DEF, 3 -> GHI, 4 -> JKL) scala> m map {case (k, v) => (v, k)} res12: scala.collection.immutable.Map[java.lang.String,Int] = Map(ABC -> 1, DEF -> 2, GHI -> 3)
  • 11. Scala Collection Hierarchy All Collection classes are in scala.collection or one of its sub- packages mutable, immutable and generic Root collections in the package scala.collection define the same interface as immutable collections and mutable collections add some modification operations to make it mutable The generic package contains building block for implementing Collections.
  • 15. Overview Of Collections Traversable Iterable Seq IndexedSeq LinearSeq mutable.Buffer Range Set SortedSet immutable.HashSet mutable.HashSet mutable.LinkedHashSet BitSet Map SortedMap immutable.HashMap mutable.HashMap mutable.LinkedHashMap
  • 16. Commonality In collections All classes are quite common. For instance, every kind of collection can be created by same uniform Syntax Traversable(1, 2, 3) Iterable("x", "y", "z") Map("x" -> 24, "y" -> 25, "z" -> 26) Set(Color.red, Color.green, Color.blue) SortedSet("hello", "world") Buffer(x, y, z) IndexedSeq(1.0, 2.0) LinearSeq(a, b, c) The same principle applies with specific collection implementations List(1, 2, 3) HashMap("x" -> 24, "y" -> 25, "z" -> 26)
  • 17. Commonality In collections... All these Collections get displayed with toString in the same way they are written above All collections support the API provided by Traversable but specializes types wherever it makes sense map method in class Traversable returns another Traversable as its result. But this result type is overridden in subclasses scala> List(1, 2, 3) map (_ + 1) res0: List[Int] = List(2, 3, 4) scala> Set(1, 2, 3) map (_ * 2) res0: Set[Int] = Set(2, 4, 6) This behavior in the collections libraries is called the uniform return type principle.
  • 18. Trait Traversable Top of collection hierarchy. Its abstract method is foreach: def foreach[U](f: Elem => U) Traversable also provides lot of concrete methods they fall in following categories Addition: ++, appends two traversables together Map operations: map, flatMap, and collect Conversions: toArray, toList, toIterable, toSeq, toIndexedSeq, toStream, toSet, toMap, Copying operations: copyToBuffer and copyToArray Size info operations: isEmpty, nonEmpty, size, and hasDefiniteSize Element retrieval operations: head, last, headOption, lastOption, and find Sub-Collection retrieval operations: tail, init, slice, take, drop, takeWhile, dropWhile, filter, filterNot, withFilter Subdivision operations: splitAt, span, partition, groupBy Element tests: exists, forall, count Folds: foldLeft, foldRight, /:, :, reduceLeft, reduceRight Specific Folds: sum, product, min, max String Operations: mkString, addString, stringPrefix
  • 19. Trait Iterable This is the next Trait in Collection hierarchy. All methods in this trait are defined in terms of an abstract method iterator, which returns collection results one by one. The foreach method from trait Traversable is implemented in Iterable in terms of iterator def foreach[U](f: Elem => U): Unit = { val it = iterator while (it.hasNext) f(it.next()) }
  • 20. An Example .. Task: Phone keys has mnemonics attached to them val mnemonics = Map( '2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL", '6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ") Assume that you are given a dictionary dict as a list of words. Design a class Coder with a method translate such that new Coder(dict).translate(phoneNumber) Produces all phrases of words which can serve as mnemonics for the phone number Example The phone number “7225276257” should have a mnemonic Scala rocks as one element of the list of solution phrases
  • 21. An Example Creating a mnemonics Map val mnemonics = Map('2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL", '6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ") Creating reverse map with upper code val upperCode: Map[Char, Char] = for ((digit, str) <- mnemonics; ltr <- str) yield (ltr -> digit) Creating a word for which digit string will be generated val word = "java" Gettng a digit string from a word eg; "java" -> "5282" word.toUpperCase map (upperCode(_))
  • 22. Everything is a library Collections feel they are language constructs Language does not contain any collection related constructs - no collection types - no collection literals - no collection operators Everything is in library They are extensible
  • 23. Conclusion De-emphasize destructive updates Focus on Transformers that map collections to collections Have complete range of persistent collections
  • 24. Next Steps Making and extending Scala Collection Parallel Collections: Move from Mutable → Persistent → Parallel
  • 25. References Martin Odersky's talk at Parleys Scala Collection documentation