{"id":4314,"date":"2016-06-04T01:03:11","date_gmt":"2016-06-04T01:03:11","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4314"},"modified":"2016-06-04T01:03:11","modified_gmt":"2016-06-04T01:03:11","slug":"scala-collect-option","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-collect-option\/","title":{"rendered":"Scala collect Option"},"content":{"rendered":"<p>To use the collect function in Scala, you need something list-like. This can be a real list, or an Option, which is like a list, but it only has one or zero items.<\/p>\n<p>Collect works a lot like map. In this case, we&#8217;ll take items from a list, and compute the length of each: <\/p>\n<pre lang=\"scala\">\nval convertFn: PartialFunction[Any, Int] = { \n  case s: String => s.length; \n  case Some(s: String) => s.length\n}\n<\/pre>\n<p>We can call this on an Option, and get results we&#8217;d expect:<\/p>\n<pre lang=\"scala\">\nOption(\"12345\").collect(\n  convertFn\n)\n\nres14: Option[Int] = Some(5)\n<\/pre>\n<p>If we call it on None, we get None back:<\/p>\n<pre lang=\"scala\">\nNone.collect(convertFn)\nres23: Option[Int] = None\n<\/pre>\n<p>If you call this on a list, it works great:<\/p>\n<pre lang=\"scala\">\nList(\n  Option(\"aaa\"),\n  Option(\"bb\"),\n  None\n).collect(\n  convertFn\n)\n\nres25: List[Int] = List(3, 2)\n<\/pre>\n<p>And, this works great, because it removes  all the Nones.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using &#8220;collect&#8221; and &#8220;Option&#8221; at the same time<\/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],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4314"}],"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=4314"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4314\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}