{"id":3629,"date":"2016-04-13T02:29:09","date_gmt":"2016-04-13T02:29:09","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3629"},"modified":"2016-04-13T02:29:09","modified_gmt":"2016-04-13T02:29:09","slug":"scala-match-value","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-match-value\/","title":{"rendered":"Scala: match on value"},"content":{"rendered":"<p>IF you want to match on a value in Scala you can do so simply. Here, I&#8217;m using it to handle commands from RabbitMQ in this format:<\/p>\n<pre lang=\"scala\">\nindex,garysieling.com\nupdate,garysieling.com,12345\n<\/pre>\n<p>Here is the code:<\/p>\n<pre lang=\"scala\">\nval items = message.split(\",\")\nval command = items(0);\n\ncommand match {\n  case \"index\" => {\n    val domain = items(1)\n    ...\n  }\n  case \"update\" => {\n    val id = items(1)\n    val domain = items(2)\n\n    ...\n  }\n}\n<\/pre>\n<p>You can also just match on the array itself. The nice thing about this is you can have the different match entries be of different length. Unfortunately you have to write &#8220;Array&#8221; on each line &#8211; if you leave these off they are treated like a Tuple, which is not the output of split:<\/p>\n<pre lang=\"scala\">\ncase Array(\"index\", domain: String) => {\n...\n}\ncase Array(\"update\", id: String, arg: String) => {\n}\n<\/pre>\n<p>You can also leave off the type definitions (I put these in to start so I could get it to compile). You can also remove the brackets if the right hand side is one expression:<br \/>\ncase Array(&#8220;index&#8221;, domain) => &#8230;<br \/>\ncase Array(&#8220;update&#8221;, id, arg) => &#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IF you want to match on a value in Scala you can do so simply. Here, I&#8217;m using it to handle commands from RabbitMQ in this format: index,garysieling.com update,garysieling.com,12345 Here is the code: val items = message.split(&#8220;,&#8221;) val command = items(0); command match { case &#8220;index&#8221; => { val domain = items(1) &#8230; } case &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-match-value\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala: match on value&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[244,300,480,486],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3629"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3629"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3629\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}