{"id":2600,"date":"2015-09-15T01:57:08","date_gmt":"2015-09-15T01:57:08","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2600"},"modified":"2015-09-15T01:57:08","modified_gmt":"2015-09-15T01:57:08","slug":"scala-foldright-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-foldright-example\/","title":{"rendered":"Scala foldRight example"},"content":{"rendered":"<p>The foldLeft, foldRight, and reduce functions let you combine a bunch of values into one. You might infer, correctly, from the names that foldLeft and foldRight iterate over a list in opposite directions.<\/p>\n<p>Interestingly, not only do these traverse the list in opposite directions, the arguments to your combiner function are flipped:<\/p>\n<pre lang=\"scala\">\nval myList = List(1, 2, 3)\n\nmyList.\n  zipWithIndex.\n  foldRight(\"start: \")( \n    (data, y) => y + \" \" + (data._1 * data._2).toString\n  )\n\nres123: String = start:  6 2 0\n\nmyList.\n  zipWithIndex.\n  foldLeft(\"start: \")( \n    (y, data) => y + \" \" + (data._1 * data._2).toString\n  )\n\nres120: String = start:  0 2 6\n<\/pre>\n<p>Unfortunately Scala differs from Javascript in a key way here, in that you need the &#8220;.&#8221; to be at the end of each line, or Scala is unable to tell that the statement continues to the next line. If you don&#8217;t do this, you will get a ton of errors:<\/p>\n<pre>\nscala>   .zipWithIndex\n<console>:1: error: illegal start of definition\n         .zipWithIndex\n<\/pre>\n<p>Note also that according to the documentation, foldRight is implemented as so (with &#8220;op&#8221; being your combiner function):<\/p>\n<pre lang=\"scala\">\nfoldRight = op(x_1, op(x_2, ... op(x_n, z)...))\n\nfoldLeft = op(...op(z, x_1), x_2, ..., x_n)\n<\/pre>\n<p>With this in mind, it definitely will not complete on infinite lists. Ideally this uses tail call recursion internally, so it may effectively behave the same as a regular loop. An interesting consequence of this implementation (over a loop) is that the recursive implementations should likely work well regardless of whether you can get the i-th item in the list quickly (i.e. a forward linked list have about the same performance characteristics with foldLeft and foldRight, if they use recursion).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The foldLeft, foldRight, and reduce functions let you combine a bunch of values into one. You might infer, correctly, from the names that foldLeft and foldRight iterate over a list in opposite directions. Interestingly, not only do these traverse the list in opposite directions, the arguments to your combiner function are flipped: val myList = &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-foldright-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala foldRight 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":[482,488],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2600"}],"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=2600"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2600\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}