SlideShare a Scribd company logo
Introducing Extractors


   Ayush Kumar Mishra
  Sr. Software Consultant
          Knoldus
What is Extractors
●
    An extractor in Scala is an object that has a method called
    unapply as one of its members.
●
    The purpose of that unapply method is to match a value
    and take it apart.
●
    The extractor object also defines a dual method apply for
    building values, but this is not required.
●
    Extractor is used to replace case classes patterns with
    objects that hides the actual implementation of Branch
    and Leaf.
●
    Extractors give you a way to create your own kinds of
    patterns. You can write patterns without creating an
    associated case class.


●
    Extractors play a role similar to views in functional
    programming languages
Ex:- A simple extractor for ScalaBook

     scala> object ScalaBook {
–      | def unapply(book:String):Option[String] =
–      |    if (book=="scala") Some("Scala") else None
–      |}
– defined module ScalaBook
–
– scala> "scala" match {
–    | case ScalaBook(f) => println (f+" is a great language !!!!!!")
–      | case _ => println ("Bah")
–      |}
– Scala is a great language !!!!!!
–
–
REPL

  The same ScalaBook object from above can be used to
  filter a list of Strings and only return Strings that contain
  scala books :
scala> val scalabooks = List(“java” ,”scala” ,”php”)
foods: List[java.lang.String] = List(java,scala,php)


scala> for (ScalaBook(f) <- books) yield f
res29: List[String] = List(Scala)
Extractors versus case classes

●
    Extractors break this link between data representations and
    patterns while Case Classes expose the concrete
    representation of data. This property is called
    representation independence.
●
    Using representation independence , we can change
    implementation later without affecting client code .
●
    Case classes are much easier to set up and to de-fine, and
     they require less code.
●
    If you need to expose a type to unknown clients, extractors
    might be preferable because they maintain representation
    independence.
Case classes usually lead to more efficient pattern
matches than extractors.


The Scala compiler can optimize patterns over case
classes much better than patterns over extractors. This is
because the mechanisms of case classes are fixed,
whereas an unapply or unapplySeq method in an extractor
could do almost anything.
Thank You

More Related Content

ODP
Knolx Session : Built-In Control Structures in Scala
PDF
Introducing Pattern Matching in Scala
PPTX
Java Tutorial Lab 3
PPTX
Java Tutorial Lab 5
ODP
Pattern Matching - at a glance
PPTX
Java Tutorial Lab 2
PPTX
Java Tutorial Lab 7
PPTX
Java Tutorial Lab 4
Knolx Session : Built-In Control Structures in Scala
Introducing Pattern Matching in Scala
Java Tutorial Lab 3
Java Tutorial Lab 5
Pattern Matching - at a glance
Java Tutorial Lab 2
Java Tutorial Lab 7
Java Tutorial Lab 4

What's hot (20)

PPTX
Java 103 intro to java data structures
PPTX
Java Tutorial Lab 6
PPTX
Learn ActionScript programming myassignmenthelp.net
PPTX
Understanding the components of standard template library
ODP
Functions & closures
PPTX
Java 8 Lambda Expressions
PPT
L11 array list
ODP
Java Collections
PPSX
Collections - Lists, Sets
PPT
2CPP16 - STL
PDF
Lambdas HOL
PPT
standard template library(STL) in C++
PPTX
Python dictionary
PPTX
ArrayList in JAVA
PDF
Java ArrayList Tutorial | Edureka
PDF
Covariance & Contravariance
PPTX
Collections framework in java
PPTX
Java - Collections framework
PDF
Java Collections Tutorials
PDF
Java Collection framework
Java 103 intro to java data structures
Java Tutorial Lab 6
Learn ActionScript programming myassignmenthelp.net
Understanding the components of standard template library
Functions & closures
Java 8 Lambda Expressions
L11 array list
Java Collections
Collections - Lists, Sets
2CPP16 - STL
Lambdas HOL
standard template library(STL) in C++
Python dictionary
ArrayList in JAVA
Java ArrayList Tutorial | Edureka
Covariance & Contravariance
Collections framework in java
Java - Collections framework
Java Collections Tutorials
Java Collection framework
Ad

Similar to Knolx Session: Introducing Extractors in Scala (20)

PPT
Extractors & Implicit conversions
PDF
Scala. Introduction to FP. Monads
PDF
Scala Paradigms
PDF
Frp2016 3
PPTX
Scala for curious
PDF
Practical cats
PDF
Stepping Up : A Brief Intro to Scala
PDF
Monads - Dublin Scala meetup
PPTX
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
PPTX
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
PPTX
Scala intro for Java devs 20150324
PDF
Pattern Matching in Scala
PDF
Introduction to Scala : Clueda
PPTX
Taxonomy of Scala
PDF
Programming in Scala: Notes
PPTX
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
PPT
Thesis PPT
PPT
Thesis
PPT
Scala introduction
PDF
Programming in Scala - Lecture Four
Extractors & Implicit conversions
Scala. Introduction to FP. Monads
Scala Paradigms
Frp2016 3
Scala for curious
Practical cats
Stepping Up : A Brief Intro to Scala
Monads - Dublin Scala meetup
BASE Meetup: "Analysing Scala Puzzlers: Essential and Accidental Complexity i...
Scala Up North: "Analysing Scala Puzzlers: Essential and Accidental Complexit...
Scala intro for Java devs 20150324
Pattern Matching in Scala
Introduction to Scala : Clueda
Taxonomy of Scala
Programming in Scala: Notes
Softshake 2013: 10 reasons why java developers are jealous of Scala developers
Thesis PPT
Thesis
Scala introduction
Programming in Scala - Lecture Four
Ad

Recently uploaded (20)

PPTX
Spectroscopy.pptx food analysis technology
PDF
Empathic Computing: Creating Shared Understanding
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
1. Introduction to Computer Programming.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Mushroom cultivation and it's methods.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Spectroscopy.pptx food analysis technology
Empathic Computing: Creating Shared Understanding
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A Presentation on Artificial Intelligence
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
SOPHOS-XG Firewall Administrator PPT.pptx
NewMind AI Weekly Chronicles - August'25-Week II
Advanced methodologies resolving dimensionality complications for autism neur...
1. Introduction to Computer Programming.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Mushroom cultivation and it's methods.pdf
Machine learning based COVID-19 study performance prediction
Encapsulation_ Review paper, used for researhc scholars
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
Heart disease approach using modified random forest and particle swarm optimi...
Building Integrated photovoltaic BIPV_UPV.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...

Knolx Session: Introducing Extractors in Scala

  • 1. Introducing Extractors Ayush Kumar Mishra Sr. Software Consultant Knoldus
  • 2. What is Extractors ● An extractor in Scala is an object that has a method called unapply as one of its members. ● The purpose of that unapply method is to match a value and take it apart. ● The extractor object also defines a dual method apply for building values, but this is not required. ● Extractor is used to replace case classes patterns with objects that hides the actual implementation of Branch and Leaf.
  • 3. Extractors give you a way to create your own kinds of patterns. You can write patterns without creating an associated case class. ● Extractors play a role similar to views in functional programming languages
  • 4. Ex:- A simple extractor for ScalaBook scala> object ScalaBook { – | def unapply(book:String):Option[String] = – | if (book=="scala") Some("Scala") else None – |} – defined module ScalaBook – – scala> "scala" match { – | case ScalaBook(f) => println (f+" is a great language !!!!!!") – | case _ => println ("Bah") – |} – Scala is a great language !!!!!! – –
  • 5. REPL The same ScalaBook object from above can be used to filter a list of Strings and only return Strings that contain scala books : scala> val scalabooks = List(“java” ,”scala” ,”php”) foods: List[java.lang.String] = List(java,scala,php) scala> for (ScalaBook(f) <- books) yield f res29: List[String] = List(Scala)
  • 6. Extractors versus case classes ● Extractors break this link between data representations and patterns while Case Classes expose the concrete representation of data. This property is called representation independence. ● Using representation independence , we can change implementation later without affecting client code . ● Case classes are much easier to set up and to de-fine, and they require less code. ● If you need to expose a type to unknown clients, extractors might be preferable because they maintain representation independence.
  • 7. Case classes usually lead to more efficient pattern matches than extractors. The Scala compiler can optimize patterns over case classes much better than patterns over extractors. This is because the mechanisms of case classes are fixed, whereas an unapply or unapplySeq method in an extractor could do almost anything.