{"id":2541,"date":"2015-08-13T19:45:13","date_gmt":"2015-08-13T19:45:13","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2541"},"modified":"2015-08-13T19:45:13","modified_gmt":"2015-08-13T19:45:13","slug":"scala-fold-example-fold-over-tuples","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-fold-example-fold-over-tuples\/","title":{"rendered":"Scala fold example (fold over tuples)"},"content":{"rendered":"<p>The variations on &#8220;fold&#8221; let you condense a Scala collection into a single value. <\/p>\n<p>This example shows how you might find a named column in a CSV file, by calling &#8220;zip&#8221; on the list of headers and cells, then doing a lookup with foldLeft:<\/p>\n<pre lang=\"scala\">\nval searchColumn = \"Name\"\nval headers = \"Name,Quantity,Amount\"\nval row = \"Gary,10,100\"\n\nheaders.split(\",\")\n   .zip(\n     row.split(\",\")\n   ).foldLeft(\"<default value>\")(\n     (prior, dataTuple) => {\n       val (headerName, cell) = dataTuple\n       if (headerName.equals(searchColumn)) cell else prior\n     }\n   )\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The variations on &#8220;fold&#8221; let you condense a Scala collection into a single value. This example shows how you might find a named column in a CSV file, by calling &#8220;zip&#8221; on the list of headers and cells, then doing a lookup with foldLeft: val searchColumn = &#8220;Name&#8221; val headers = &#8220;Name,Quantity,Amount&#8221; val row = &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-fold-example-fold-over-tuples\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala fold example (fold over tuples)&#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],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2541"}],"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=2541"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2541\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}