Pattern Matching - at a glance


Published on

Pattern Matching - at a glance

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Pattern Matching - at a glance

  1. 1. Pattern Matching - at a glanceNeelkanth SachdevaSoftware ConsultantKnoldus Software LLP , New
  2. 2. 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.
  3. 3. What case modifier does ?1. Adds a factory method with the name of the class. It eliminates the need of new during instantiation.
  4. 4. 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
  5. 5. 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.
  6. 6. Pattern matching
  7. 7. The right-hand side of simplifyTop consists of amatch expression. match corresponds to switchin Java.Scala :selector match { alternatives }Java :switch (selector) { alternatives }
  8. 8. - 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.
  9. 9. Kinds of patterns
  10. 10. 1. Wildcard patternsThe wildcard pattern ( _ ) matches any objectwhatsoever.It is used as a default.
  11. 11. 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.
  12. 12. 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.
  13. 13. 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.
  14. 14. 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.
  15. 15. 6. Tuple patternsYou can match against tuples as well. A patternlike (a, b, c) matches an arbitrary 3-tuple.
  16. 16. 7.Typed patternsYou can use a typed pattern as a convenientreplacement for type tests and type casts.
  17. 17. Patterns in variable definitionsAny time you define a val or a var, you can use apattern instead of a simple identifier.
  18. 18. Patterns in for expressions
  19. 19. ReferencesProgramming in Scala by Martin Odersky