{"id":2593,"date":"2015-09-15T01:09:35","date_gmt":"2015-09-15T01:09:35","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2593"},"modified":"2015-09-15T01:09:35","modified_gmt":"2015-09-15T01:09:35","slug":"scala-collect-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-collect-example\/","title":{"rendered":"Scala collect example"},"content":{"rendered":"<p>The official scala docs say that collect &#8220;builds a new collection by applying a partial function to all elements of this sequence on which the function is defined.&#8221;<\/p>\n<p>To understand what this means, it&#8217;s helpful to see an example of a partial function used in this way. For instance, we can make a function that returns the original value, but handling different input types. This function takes &#8220;Any&#8221; and turns it into an Int (hence the signature, [Any, Int]).<\/p>\n<p>Even though the function takes an &#8220;Any&#8221;, it would fail for many types of values; e.g. floating point values &#8211; these Scala will drop out of the final collection. This is pretty handy, because you can guarantee that this won&#8217;t error out, and you can guarantee that you only get the types of output you want.<\/p>\n<pre lang=\"scala\">\nval convertFn: PartialFunction[Any, Int] = { \n  case i: Int => i; \n  case s: String => s.toInt;\n  case Some(s: String) => s.toInt \n}\n<\/pre>\n<pre lang=\"scala\">\nList(0, 1, \"2\", \"3\", Some(4), Some(\"5\")).\n  collect(convertFn)\n\nList[Int] = List(0, 1, 2, 3, 5)\n<\/pre>\n<p>By contrast, any value we return that isn&#8217;t an Int, String, or Some(String) gets knocked out of the list &#8211; essentially this does a &#8220;map&#8221; and a &#8220;filter&#8221; at the same time.<\/p>\n<pre lang=\"Scala\">\nscala> List(6.5, None, null, Unit).collect(convertFn)\nres93: List[Int] = List()\n<\/pre>\n<p>Note, however that this isn&#8217;t something that catches errors, it&#8217;s actually interrogating the function to determine whether it is defined at a particular point; if not, the function is never called.<\/p>\n<p>To prove this, you can define a function that is not implemented for anything other than ints, and collect will fail:<\/p>\n<pre lang=\"scala\">\nList(1, \"\").collect(\n  { \n    case i: Int => i; \n    case _ => ??? \n  }\n)\n\nscala.NotImplementedError: an implementation is missing\n  at scala.Predef$.$qmark$qmark$qmark(Predef.scala:225)\n  at $anonfun$1.applyOrElse(<console>:8)\n  at scala.collection.immutable.List.collect(List.scala:303)\n  ... 33 elided\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An example of &#8220;collect&#8221; in scala<\/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":[480,482,488],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2593"}],"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=2593"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2593\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}