{"id":8460,"date":"2021-04-26T13:48:29","date_gmt":"2021-04-26T13:48:29","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=8460"},"modified":"2024-04-13T09:20:27","modified_gmt":"2024-04-13T09:20:27","slug":"scala-pattern-matching","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/","title":{"rendered":"Scala &#8211; Pattern Matching"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Pattern Matching in Scala&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6145,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;}\">Pattern Matching in Scala<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p><strong>A Pattern Matching<\/strong> is a method of checking the <a href=\"http:\/\/scala-lang.org\">given<\/a> group of tokens for the presence of the particular pattern. It is the most broadly utiliz feature in <strong><a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-a-quick-overview\/\">Scala<\/a><\/strong>. It is a procedure for checking a value against a pattern. the is like the switch statement of <strong>Java<\/strong> and <strong>C<\/strong>.<\/p>\r\n\r\n\r\n\r\n<p>In <strong>scala<\/strong>, &#8220;<strong>match<\/strong>&#8221; keyword is utilized rather than switch statement. &#8220;<strong>match<\/strong>&#8221; is constantly characterized in Scala&#8217;s root class to make its accessibility and to the all objects.<\/p>\r\n\r\n\r\n\r\n<p><strong>use case 1:<\/strong><\/p>\r\n\r\n\r\n\r\n<p>Scala&gt; import scala.util.Random<\/p>\r\n\r\n\r\n\r\n<p>scala&gt; val x: Int = Random.nextInt(1)<\/p>\r\n\r\n\r\n\r\n<p>val x: Int = 0<\/p>\r\n\r\n\r\n\r\n<p>scala&gt; x match {<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 case 0 =&gt; &#8220;zero&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0 \u00a0 \u00a0 \u00a0 1case 1 =&gt; &#8220;one&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0 case 2 =&gt; &#8220;two&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0 \u00a0 \u00a0 \u00a0 2case _ =&gt; &#8220;other&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 }<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"523\" height=\"227\" class=\"wp-image-8461\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-75.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-75.png 523w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-75-300x130.png 300w\" sizes=\"auto, (max-width: 523px) 100vw, 523px\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong>use case 2:<\/strong><\/p>\r\n\r\n\r\n\r\n<p>scala&gt; val x: Int = Random.nextInt(4)<\/p>\r\n\r\n\r\n\r\n<p>val x: Int = 3<\/p>\r\n\r\n\r\n\r\n<p>scala&gt; x match {<\/p>\r\n\r\n\r\n\r\n<p>\u00a0 \u00a0 \u00a01case 0 =&gt; &#8220;Leonardo&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 case 1 =&gt; &#8220;Tom&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0 \u00a0 \u00a02case 2 =&gt; &#8220;Christian&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 case 3 =&gt; &#8220;steve&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0 \u00a0 \u00a04case 4 =&gt; &#8220;Mark&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 }<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"464\" height=\"195\" class=\"wp-image-8462\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-76.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-76.png 464w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-76-300x126.png 300w\" sizes=\"auto, (max-width: 464px) 100vw, 464px\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong>use case 3:<\/strong><\/p>\r\n\r\n\r\n\r\n<p>scala&gt; val abc: Int = Random.nextInt(2)<\/p>\r\n\r\n\r\n\r\n<p>val abc: Int = 1<\/p>\r\n\r\n\r\n\r\n<p>scala&gt; abc match {<\/p>\r\n\r\n\r\n\r\n<p>\u00a0 \u00a0 \u00a0case 0 =&gt; &#8220;Leonardo&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0 \u00a0 \u00a0 2case 1 =&gt; &#8220;Tom&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0 \u00a0 case 2 =&gt; &#8220;Christian&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0 \u00a04case 3 =&gt; &#8220;steve&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0 \u00a0 case 4 =&gt; &#8220;Mark&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 }<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"198\" class=\"wp-image-8463\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-77.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-77.png 470w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-77-300x126.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Pattern Matching in Scala<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Pattern Matching in Scala &nbsp; A Pattern Matching is a method of checking the given group of tokens for the presence of the particular pattern. It is the most broadly utiliz feature in Scala. It is a procedure for checking a value against a pattern. the is like the switch statement of Java and C. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[565,1698],"tags":[1154,1155,1157,1152,1158,1149,1153,1148,1150,1156],"class_list":["post-8460","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-match-keyword","tag-match-keyword-scala","tag-pattern-matching","tag-pattern-matching-definition","tag-pattern-matching-definition-in-scala","tag-pattern-matching-in-scala","tag-pattern-matching-use-cases-scala","tag-scala-pattern-matching","tag-scala-pattern-matching-example","tag-what-is-pattern-matching-in-scala"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Pattern Matching in Scala - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Pattern Matching in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pattern Matching in Scala - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Pattern Matching in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-26T13:48:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T09:20:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-75.png\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/\",\"name\":\"Pattern Matching in Scala - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-75.png\",\"datePublished\":\"2021-04-26T13:48:29+00:00\",\"dateModified\":\"2024-04-13T09:20:27+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Pattern Matching in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-75.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-75.png\",\"width\":523,\"height\":227},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Pattern Matching\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pattern Matching in Scala - Prwatech","description":"Master Pattern Matching in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Pattern Matching in Scala - Prwatech","og_description":"Master Pattern Matching in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-04-26T13:48:29+00:00","article_modified_time":"2024-04-13T09:20:27+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-75.png","type":"","width":"","height":""}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/","name":"Pattern Matching in Scala - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-75.png","datePublished":"2021-04-26T13:48:29+00:00","dateModified":"2024-04-13T09:20:27+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Pattern Matching in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-75.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-75.png","width":523,"height":227},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-pattern-matching\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Pattern Matching"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=8460"}],"version-history":[{"count":6,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8460\/revisions"}],"predecessor-version":[{"id":11435,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8460\/revisions\/11435"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=8460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=8460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=8460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}