• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Pattern Matching - at a glance
 

Pattern Matching - at a glance

on

  • 2,188 views

Pattern Matching - at a glance

Pattern Matching - at a glance

Statistics

Views

Total Views
2,188
Views on SlideShare
891
Embed Views
1,297

Actions

Likes
0
Downloads
8
Comments
0

6 Embeds 1,297

http://blog.knoldus.com 940
http://neelkanthsachdeva.wordpress.com 347
http://feedwrangler.net 3
http://www.linkedin.com 3
http://localhost 2
http://knoldus.wordpress.com 2

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Pattern Matching - at a glance Pattern Matching - at a glance Presentation Transcript

    • Pattern Matching - at a glanceNeelkanth SachdevaSoftware ConsultantKnoldus Software LLP , New Delhineelkanthsachdeva.wordpress.comneelkanth@knoldus.com@NeelSachdeva
    • What is Case class ?Before diving in to pattern matching lets talkabout case classes briefly.Classes with case modifier are called caseClasses. Using the modifier makes the Scalacompiler add some syntactic conveniences toyour class.
    • What case modifier does ?1. Adds a factory method with the name of the class. It eliminates the need of new during instantiation.
    • 2. The second syntactic convenience is that all arguments in the parameter list of a case class implicitly get a val prefix, so they are maintained as fields
    • 3. The compiler adds “natural” implementations of following methods to your class.- toString,- hashCode- equals4. The compiler adds a copy method to your class for making modified copies.
    • Pattern matching
    • The right-hand side of simplifyTop consists of amatch expression. match corresponds to switchin Java.Scala :selector match { alternatives }Java :switch (selector) { alternatives }
    • - A pattern match includes a sequence of alternatives, each starting with the keyword case.- Each alternative includes a pattern and one or more expressions, which will be evaluated if the pattern matches.- An arrow symbol => separates the pattern from the expressions.- A match expression is evaluated by trying each of the patterns in the order they are written. The first pattern that matches is selected, and the part following the arrow is selected and executed.
    • Kinds of patterns
    • 1. Wildcard patternsThe wildcard pattern ( _ ) matches any objectwhatsoever.It is used as a default.
    • 2. Constant patternsA constant pattern matches only itself. Any literalmay be used as a constant.For example, 5, true, and "hello" are all constantpatterns. Also, any val or singleton object can beused as a constant.
    • 3. Variable patternsA variable pattern matches any object, just like aWildcard. The default case uses a variablepattern so that it has a name for the value, nomatter what it is.
    • 4. Constructor patternsA constructor pattern looks like :BinOp("+", Number(0) , Number(0))It consists of a name BinOp and then a numberof patterns within parenthese. Assuming the name designates acase class, such a pattern means to first check that the objectis a member of the named case class, and then to check that theconstructor parameters of the object match the extrapatterns supplied.
    • 5.Sequence patternsYou can match against sequence types like Listor Array just like you match against caseclasses.Use the same syntax, but now you can specifyany number of elements within the pattern.
    • 6. Tuple patternsYou can match against tuples as well. A patternlike (a, b, c) matches an arbitrary 3-tuple.
    • 7.Typed patternsYou can use a typed pattern as a convenientreplacement for type tests and type casts.
    • Patterns in variable definitionsAny time you define a val or a var, you can use apattern instead of a simple identifier.
    • Patterns in for expressions
    • ReferencesProgramming in Scala by Martin Odersky