{"id":2571,"date":"2015-09-11T02:56:50","date_gmt":"2015-09-11T02:56:50","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2571"},"modified":"2015-09-11T02:56:50","modified_gmt":"2015-09-11T02:56:50","slug":"scala-zipwithindex-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-zipwithindex-example\/","title":{"rendered":"Scala zipWithIndex example"},"content":{"rendered":"<p>If you&#8217;re new to Scala, the idea of immutable data structures can be a pain at times, especially when you need to replace &#8220;for (int x=0; x < arr1.length; x++)\".\n\nOne easy way to handle this is zipWithIndex:\n\n\n\n<pre lang=\"Scala\">\nval arr1 = List(&#8220;a&#8221;, &#8220;b&#8221;, &#8220;c&#8221;)\n\narr1.zipWithIndex\n<\/pre>\n<p>Note that you call this WITHOUT the parens:<\/p>\n<pre>\nres14: List[(String, Int)] = List((a,0), (b,1), (c,2))\n<\/pre>\n<p>Otherwise, you&#8217;ll get an awful type error, which indicates you probably weren&#8217;t doing what you wanted:<\/p>\n<pre>\n<console>:9: error: not enough arguments \nfor method zipWithIndex: \n(implicit bf: scala.collection.generic.CanBuildFrom\n[List[String],(A1, Int),That])That.\nUnspecified value parameter bf.\n<\/pre>\n<p>Using the above technique, you get a new list, with tuples of object + index. If you need something that doesn&#8217;t pre-compute everything, like your old &#8220;for&#8221; loop, you can do something like this:<\/p>\n<pre lang=\"scala\">\narr1\n  .view\n  .zipWithIndex\n  .flatMap(\n    x => { \n      System.out.println(x); \n      None \n    }).toList\n<\/pre>\n<pre>\n(a,0)\n(b,1)\n(c,2)\nres49: List[Nothing] = List()\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re new to Scala, the idea of immutable data structures can be a pain at times, especially when you need to replace &#8220;for (int x=0; x < arr1.length; x++)\". One easy way to handle this is zipWithIndex: val arr1 = List(&#8220;a&#8221;, &#8220;b&#8221;, &#8220;c&#8221;) arr1.zipWithIndex Note that you call this WITHOUT the parens: res14: List[(String, &hellip; \n\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-zipwithindex-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala zipWithIndex 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\/2571"}],"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=2571"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2571\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}