{"id":2626,"date":"2015-09-17T12:44:33","date_gmt":"2015-09-17T12:44:33","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2626"},"modified":"2015-09-17T12:44:33","modified_gmt":"2015-09-17T12:44:33","slug":"scala-unzip-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-unzip-example\/","title":{"rendered":"Scala unzip example"},"content":{"rendered":"<p>The unzip takes a sequence of tuples, and gives you two lists made from the values in the tuples.<\/p>\n<p>To make this simple to see, we&#8217;ll make a list, and then do zipWithIndex:<\/p>\n<pre lang=\"scala\">\nList(\"a\", \"b\", \"c\")\nres30: List[String] = List(a, b, c)\n\nList(\"a\", \"b\", \"c\").zipWithIndex\nres31: List[(String, Int)] = List((a,0), (b,1), (c,2))\n<\/pre>\n<p>The result list has three tuples, with our original list values and the index.<\/p>\n<p>If we just add &#8220;unzip&#8221;, we&#8217;ll get the list data back:<\/p>\n<pre lang=\"scala\">\nList(\"a\", \"b\", \"c\").zipWithIndex.unzip\n\nres32: (List[String], List[Int]) = \n  (List(a, b, c),List(0, 1, 2))\n<\/pre>\n<p>To get this in a form you&#8217;d actually want, you can just un-tuple it, and you&#8217;ll get the two lists you wanted originally:<\/p>\n<pre lang=\"scala\">\nvar (ids, indexes) = \n  List(\"a\", \"b\", \"c\").zipWithIndex.unzip\nids: List[String] = List(a, b, c)\nindexs: List[Int] = List(0, 1, 2)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The unzip takes a sequence of tuples, and gives you two lists made from the values in the tuples. To make this simple to see, we&#8217;ll make a list, and then do zipWithIndex: List(&#8220;a&#8221;, &#8220;b&#8221;, &#8220;c&#8221;) res30: List[String] = List(a, b, c) List(&#8220;a&#8221;, &#8220;b&#8221;, &#8220;c&#8221;).zipWithIndex res31: List[(String, Int)] = List((a,0), (b,1), (c,2)) The result list &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-unzip-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala unzip 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\/2626"}],"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=2626"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2626\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}