{"id":2665,"date":"2015-09-23T01:37:17","date_gmt":"2015-09-23T01:37:17","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2665"},"modified":"2015-09-23T01:37:17","modified_gmt":"2015-09-23T01:37:17","slug":"scala-headoption-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-headoption-example\/","title":{"rendered":"Scala headOption example"},"content":{"rendered":"<p>The scala collections API provides a function called &#8220;headOption&#8221;, which returns a Some(value) if there is an item in a list, and a &#8220;None&#8221; otherwise.<\/p>\n<p>Observe:<\/p>\n<pre lang=\"scala\">\nscala> List(1).headOption\nres35: Option[Int] = Some(1)\n\nscala> List().headOption\nres36: Option[Nothing] = None\n<\/pre>\n<p>It may not be immediately obvious why this is useful, especially if you&#8217;re coming from Java, or similar languages.<\/p>\n<p>However, it is quite useful, because you can use it to detect the end-of-sequence condition (without knowing why the sequence ends &#8211; it&#8217;d behave the same if it was a network socket, as if it was an array)<\/p>\n<p>For instance, lets say you decided to count the number of items in the sequence, using recursion. If you did this, you could pattern match the value of headOption to see if you were done:<\/p>\n<pre lang=\"sql\">\nval count: (Seq[Any]) => Int = \n  (values: Seq[Any]) => { \n    values.headOption match { \n      case Some(_) => 1 + count(values.tail) \n      case None => 0 \n    }\n  }\n\nscala> count(List(1,2,3))\nres37: Int = 3\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The scala collections API provides a function called &#8220;headOption&#8221;, which returns a Some(value) if there is an item in a list, and a &#8220;None&#8221; otherwise. Observe: scala> List(1).headOption res35: Option[Int] = Some(1) scala> List().headOption res36: Option[Nothing] = None It may not be immediately obvious why this is useful, especially if you&#8217;re coming from Java, or &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-headoption-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala headOption example&#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":[480,482,488],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2665"}],"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=2665"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2665\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}