{"id":9239,"date":"2021-05-30T14:51:48","date_gmt":"2021-05-30T14:51:48","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9239"},"modified":"2024-04-13T12:35:04","modified_gmt":"2024-04-13T12:35:04","slug":"scala-fold-left-scala-collections-foldleft-method","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/","title":{"rendered":"Scala &#8211; Fold Left"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Scala Collections - FoldLeft Method&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 Collections FoldLeft Method<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p>The <strong>foldLeft <\/strong>strategy takes an <a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/\">acquain<\/a> twofold <a href=\"http:\/\/scala-lang.org\">administrator<\/a> work as boundary and will utilize it to implode components from the assortment.<\/p>\r\n<p>The request for navigating the components in the assortment is from left to right and subsequently the name foldLeft. The foldLeft strategy permits you to likewise determine an underlying worth.<\/p>\r\n<p>The <code>foldLeft<\/code> method is a fundamental higher-order function in Scala collections that allows for iterative aggregation or reduction of elements in a collection.<\/p>\r\n<div class=\"flex-1 overflow-hidden\">\r\n<div class=\"react-scroll-to-bottom--css-kloqy-79elbk h-full\">\r\n<div class=\"react-scroll-to-bottom--css-kloqy-1n7m0yu\">\r\n<div class=\"flex flex-col text-sm pb-9\">\r\n<div class=\"w-full text-token-text-primary\" dir=\"auto\" data-testid=\"conversation-turn-166\">\r\n<div class=\"px-4 py-2 justify-center text-base md:gap-6 m-auto\">\r\n<div class=\"flex flex-1 text-base mx-auto gap-3 juice:gap-4 juice:md:gap-6 md:px-5 lg:px-1 xl:px-5 md:max-w-3xl lg:max-w-[40rem] xl:max-w-[48rem] group final-completion\">\r\n<div class=\"relative flex w-full flex-col agent-turn\">\r\n<div class=\"flex-col gap-1 md:gap-3\">\r\n<div class=\"flex flex-grow flex-col max-w-full\">\r\n<div class=\"min-h-[20px] text-message flex flex-col items-start gap-3 whitespace-pre-wrap break-words [.text-message+&amp;]:mt-5 overflow-x-auto\" dir=\"auto\" data-message-author-role=\"assistant\" data-message-id=\"220a05a8-05d9-4c75-b712-53a0503e84ee\">\r\n<div class=\"markdown prose w-full break-words dark:prose-invert light\">\r\n<p>Here, <code>z<\/code> is the initial value (also known as the zero element) of type <code>B<\/code>, and <code>op<\/code> is a binary operator that takes an accumulated value of type <code>B<\/code> and an element of the collection of type <code>A<\/code>, and returns a new accumulated value of type <code>B<\/code>.<\/p>\r\n<p>Common use cases of <code>foldLeft<\/code> include computing the sum or product of elements, constructing new data structures (e.g., List, Map), or performing complex transformations on collections. The <code>foldLeft<\/code> method is tail-recursive, making it efficient and stack-safe for processing large collections.<\/p>\r\n<p>Understanding <code>foldLeft<\/code> is crucial for functional programming in Scala, as it encourages a declarative and concise style of coding by encapsulating iterative operations into a single higher-order function call. By leveraging <code>foldLeft<\/code>, developers can write expressive and efficient code for processing and aggregating data in Scala collections. Scala Collections FoldLeft Method<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<h3><strong>use case 1:<\/strong><\/h3>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>scala&gt; val prices: Seq[Double] = Seq(9.5, 3.0, 7)\r\n\r\nscala&gt; val sum = prices.foldLeft(0.0)(_ + _)\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=\"485\" height=\"94\" class=\"wp-image-9250\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b1.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b1.png 485w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b1-300x58.png 300w\" sizes=\"auto, (max-width: 485px) 100vw, 485px\" \/><\/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>scala&gt; val eduprwa: Seq[String] = Seq(\"Data\", \"Science\", \"Prwatech\")\r\n\r\nscala&gt; println(s\"All eduprwa = ${eduprwa.foldLeft(\"\")((a, b) =&gt; a + \" eduprwa \" + b)}\")\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=\"710\" height=\"95\" class=\"wp-image-9251\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b2.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b2.png 710w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b2-300x40.png 300w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/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>scala&gt; val prices: Seq[Double] = Seq(4.0, 2.0)\r\n\r\n\r\nscala&gt; val diff = prices.foldLeft(5.0)(_ - _)\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=\"412\" height=\"94\" class=\"wp-image-9278\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b4.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b4.png 412w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b4-300x68.png 300w\" sizes=\"auto, (max-width: 412px) 100vw, 412px\" \/><\/figure>\r\n","protected":false},"excerpt":{"rendered":"<p>Scala Collections FoldLeft Method &nbsp; The foldLeft strategy takes an acquain twofold 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 foldLeft. The foldLeft strategy permits you to likewise determine an underlying [&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":[1470,1471,1469,1474,1467,1477,1468,1472,1475,1473],"class_list":["post-9239","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-define-scala-fold-left","tag-describe-fold-left-in-scala","tag-explain-fold-left-function","tag-fold-left-in-scala","tag-fold-left-method","tag-fold-left-scala","tag-fold-left-scala-examples","tag-how-fold-left-function-is-used","tag-scala-fold-left","tag-what-is-fold-left-function"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Scala Collections FoldLeft Method - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Scala Collections FoldLeft Method - 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 Collections FoldLeft Method - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Scala Collections FoldLeft Method - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/\" \/>\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-30T14:51:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T12:35:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b1.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-fold-left-scala-collections-foldleft-method\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/\",\"name\":\"Scala Collections FoldLeft Method - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b1.png\",\"datePublished\":\"2021-05-30T14:51:48+00:00\",\"dateModified\":\"2024-04-13T12:35:04+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Scala Collections FoldLeft Method - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b1.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b1.png\",\"width\":485,\"height\":94},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Fold 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 Collections FoldLeft Method - Prwatech","description":"Master Scala Collections FoldLeft Method - 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 Collections FoldLeft Method - Prwatech","og_description":"Master Scala Collections FoldLeft Method - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-05-30T14:51:48+00:00","article_modified_time":"2024-04-13T12:35:04+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b1.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-fold-left-scala-collections-foldleft-method\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/","name":"Scala Collections FoldLeft Method - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b1.png","datePublished":"2021-05-30T14:51:48+00:00","dateModified":"2024-04-13T12:35:04+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Scala Collections FoldLeft Method - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b1.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/b1.png","width":485,"height":94},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-left-scala-collections-foldleft-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Fold 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\/9239","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=9239"}],"version-history":[{"count":5,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9239\/revisions"}],"predecessor-version":[{"id":11470,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9239\/revisions\/11470"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}