{"id":9282,"date":"2021-05-30T15:23:56","date_gmt":"2021-05-30T15:23:56","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9282"},"modified":"2024-04-13T12:57:47","modified_gmt":"2024-04-13T12:57:47","slug":"scala-reduce-left-scala-tutorial-reduceleft-function-example","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/","title":{"rendered":"Scala &#8211; Reduce Left"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Scala Tutorial - ReduceLeft Function Example&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6145,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;}\">Scala Tutorial ReduceLeft Function Example<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p>The <strong>reduceLeft<\/strong> method is <a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/\">relevant<\/a> to both <a href=\"http:\/\/scala-lang.org\">Scala&#8217;s<\/a> <strong>Mutable<\/strong> and <strong>Immutable<\/strong> assortment information structures.The <strong>reduceLeft<\/strong> technique takes an affiliated paired administrator work as boundary and will utilize it to implode components from the assortment. The request for navigating the components in the assortment is from left to right and subsequently the name reduceLeft. In contrast to the foldLeft technique, reduceLeft doesn&#8217;t permit you to likewise indicate an underlying worth.<\/p>\r\n<p>The <code>reduceLeft<\/code> function in Scala is a higher-order function used to iteratively combine elements of a collection from left to right using a binary operation. It is similar to the <code>reduce<\/code> function but starts the reduction process from the leftmost element of the collection.<\/p>\r\n<p>The <code>reduceLeft<\/code> function takes a binary operator as an argument and applies it to the elements of the collection sequentially from the first element to the last, accumulating a single result. The binary operator must be associative, ensuring that the reduction process is consistent regardless of the order of application.<\/p>\r\n<p>In this example, the <code>reduceLeft<\/code> function iteratively multiplies each element of the list starting from the leftmost element (<code>1<\/code>) to compute the factorial (<code>5! = 1 * 2 * 3 * 4 * 5<\/code>).<\/p>\r\n<p>The <code>reduceLeft<\/code> function is particularly useful for aggregating values and performing iterative computations on collections. It provides a functional and concise way to express data transformation operations in Scala. Understanding how to use <code>reduceLeft<\/code> effectively is essential for mastering functional programming paradigms and leveraging the power of higher-order functions in Scala. Scala Tutorial ReduceLeft Function Example<\/p>\r\n\r\n\r\n\r\n<p><strong>use case 1:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>find the sum of the elements using reduceLeft function\r\n\r\nval donutPrices: Seq[Double] = Seq(1.5, 2.0, 2.5)\r\nprintln(s\"Elements of donutPrices = $donutPrices\")\r\nval sum: Double = donutPrices.reduceLeft(_ + _)\r\n\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"492\" height=\"193\" class=\"wp-image-9283\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-569.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-569.png 492w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-569-300x118.png 300w\" sizes=\"auto, (max-width: 492px) 100vw, 492px\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong>2use case 2:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>find the cheapest donut using reduceLeft function\r\n\r\nscala&gt; val donutPrices: Seq[Double] = Seq(6.5, 6.0, 8.5)\r\nscala&gt; println(s\"Elements of donutPrices = $donutPrices\")\r\nscala&gt; println(s\"Cheapest donut price = ${donutPrices.reduceLeft(_ min _)}\")\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"606\" height=\"141\" class=\"wp-image-9284\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-570.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-570.png 606w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-570-300x70.png 300w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong>use case 3:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>find the most expensive donut using reduceLeft function\r\n\r\nscala&gt; val donutPrices: Seq[Double] = Seq(6.5, 6.0, 8.5)\r\nscala&gt; println(s\"Most expensive donut price = ${donutPrices.reduceLeft(_ max _)}\")\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"606\" height=\"87\" class=\"wp-image-9285\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-571.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-571.png 606w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-571-300x43.png 300w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/figure>\r\n","protected":false},"excerpt":{"rendered":"<p>Scala Tutorial ReduceLeft Function Example &nbsp; The reduceLeft method is relevant to both Scala&#8217;s Mutable and Immutable assortment information structures.The reduceLeft technique takes an affiliated paired administrator work as boundary and will utilize it to implode components from the assortment. The request for navigating the components in the assortment is from left to right and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[565,1698],"tags":[1495,1494,1493,1487,1491,1497,1489,1496,1488,1492],"class_list":["post-9282","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-define-reduce-left-in-scala","tag-describe-reduce-left","tag-how-reduce-left-is-used-in-scala","tag-reduce-left","tag-reduce-left-method","tag-scala-reduce-left-examples","tag-scala-reduce-left-function","tag-scala-reduce-left-use-cases","tag-scala-reduceleft","tag-what-is-reduce-left-in-scala"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Scala Tutorial ReduceLeft Function Example - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Scala Tutorial ReduceLeft Function Example - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Tutorial ReduceLeft Function Example - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Scala Tutorial ReduceLeft Function Example - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-30T15:23:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T12:57:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-569.png\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/\",\"name\":\"Scala Tutorial ReduceLeft Function Example - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-569.png\",\"datePublished\":\"2021-05-30T15:23:56+00:00\",\"dateModified\":\"2024-04-13T12:57:47+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Scala Tutorial ReduceLeft Function Example - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-569.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-569.png\",\"width\":492,\"height\":193},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Reduce Left\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scala Tutorial ReduceLeft Function Example - Prwatech","description":"Master Scala Tutorial ReduceLeft Function Example - Dive deep with our expert instructors and comprehensive curriculum.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Scala Tutorial ReduceLeft Function Example - Prwatech","og_description":"Master Scala Tutorial ReduceLeft Function Example - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-05-30T15:23:56+00:00","article_modified_time":"2024-04-13T12:57:47+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-569.png","type":"","width":"","height":""}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/","name":"Scala Tutorial ReduceLeft Function Example - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-569.png","datePublished":"2021-05-30T15:23:56+00:00","dateModified":"2024-04-13T12:57:47+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Scala Tutorial ReduceLeft Function Example - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-569.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-569.png","width":492,"height":193},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-left-scala-tutorial-reduceleft-function-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Reduce Left"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=9282"}],"version-history":[{"count":4,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9282\/revisions"}],"predecessor-version":[{"id":11473,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9282\/revisions\/11473"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}