{"id":2586,"date":"2015-09-14T11:44:09","date_gmt":"2015-09-14T11:44:09","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2586"},"modified":"2015-09-14T11:44:09","modified_gmt":"2015-09-14T11:44:09","slug":"scala-flatmap-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-flatmap-example\/","title":{"rendered":"Scala flatMap example"},"content":{"rendered":"<p>The flatMap function is similar to map, but used a little differently.<\/p>\n<p>First, lets make a couple arrays:<\/p>\n<pre lang=\"scala\">\nval x = List(1,2,3)\nval y = List(4,5,6)\n\nscala> List(x, y)\nres38: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6))\n\nscala> List(x, y).map( x => x )\nres40: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6))\n<\/pre>\n<p>Sometimes it&#8217;s preferable to get a flattened list, that looks like this:<\/p>\n<pre lang=\"Scala\">\nval z = List(1,2,3,4,5,6)\n<\/pre>\n<p>Which is what you do with flatMap:<\/p>\n<pre lang=\"scala\">\nscala> List(x, y).flatMap( x => x )\nres41: List[Int] = List(1, 2, 3, 4, 5, 6)\n<\/pre>\n<p>An important thing to understand about flatMap is that anything that looks like an empty array will disappear, like so:<\/p>\n<pre lang=\"scala\">\nList(None, Some(1), None, Some(2)).flatMap(x => x)\nres48: List[Int] = List(1, 2)\n<\/pre>\n<p>Note that this does not recurse, so you may want to call it twice:<\/p>\n<pre lang=\"scala\">\nList(\n  List(None, Some(1)), \n  List(None, Some(2))\n).flatMap(x => x)\n\nList[Option[Int]] = \n  List(None, Some(1), None, Some(2))\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The flatMap function is similar to map, but used a little differently. First, lets make a couple arrays: val x = List(1,2,3) val y = List(4,5,6) scala> List(x, y) res38: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6)) scala> List(x, y).map( x => x ) res40: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6)) Sometimes &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-flatmap-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala flatMap 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,488],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2586"}],"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=2586"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2586\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}