{"id":4611,"date":"2016-07-28T00:28:35","date_gmt":"2016-07-28T00:28:35","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4611"},"modified":"2016-07-28T00:28:35","modified_gmt":"2016-07-28T00:28:35","slug":"fixing-scala-error-reduce-java-lang-unsupportedoperationexception-empty-reduceleft","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/fixing-scala-error-reduce-java-lang-unsupportedoperationexception-empty-reduceleft\/","title":{"rendered":"Fixing scala error with reduce: java.lang.UnsupportedOperationException: empty.reduceLeft"},"content":{"rendered":"<p>You may want to reduce a list of booleans with an &#8220;and&#8221; or an &#8220;or&#8221;:<\/p>\n<pre lang=\"scala\">\nList(true, false).reduce( \n  (x, y) => x && y\n)\n<\/pre>\n<p>When you run this on an empty list, you&#8217;ll get this error:<\/p>\n<pre>\njava.lang.UnsupportedOperationException: empty.reduceLeft\n<\/pre>\n<p>To fix this, use foldLeft instead:<\/p>\n<pre lang=\"scala\">\nList(true, false).foldLeft(true)( \n  (x, y) => x && y\n)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using reduce and foldLeft in Scala effectively<\/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\/4611"}],"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=4611"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4611\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}