{"id":9155,"date":"2021-05-30T13:23:28","date_gmt":"2021-05-30T13:23:28","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9155"},"modified":"2024-04-13T12:16:11","modified_gmt":"2024-04-13T12:16:11","slug":"scala-fold-right","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/","title":{"rendered":"Scala &#8211; Fold Right"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Scala Collections - FoldRight 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 FoldRight Method<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p>The <strong>foldRight<\/strong> work is relevant to both <a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-a-quick-overview\/\">Scala&#8217;s<\/a> Mutable and Immutable assortment information structures.<\/p>\r\n\r\n\r\n\r\n<p>The foldRight technique takes an affiliated <a href=\"http:\/\/scala-lang.org\">parallel<\/a> 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 option to left and thus the name foldRight. The foldRight technique permits you to likewise determine an underlying worth.<\/p>\r\n<p>The <code>foldRight<\/code> is a method in Scala collections is a higher-order function that facilitates iterative processing and aggregation of elements from right to left within a collection. This method is define on sequences (e.g., List, Vector) and operates by recursively applying a binary operator to combine elements of the collection starting from the rightmost element.<\/p>\r\n<p>The signature of <code>foldRight<\/code> is <code>foldRight[B](z: B)(op: (A, B) =&gt; B): B<\/code>, where:<\/p>\r\n<ul>\r\n<li><code>z<\/code> is an initial value of type <code>B<\/code> used as the starting accumulator.<\/li>\r\n<li><code>op<\/code> is a binary operator that takes an element of the collection of type <code>A<\/code> and an accumulator of type <code>B<\/code>, and produces a new accumulator of type <code>B<\/code>.<\/li>\r\n<\/ul>\r\n<p>The <code>foldRight<\/code> method processes elements in reverse order compared to <code>foldLeft<\/code>, making it suitable for operations that require a right-associative combining function (e.g., constructing new collections, performing recursive computations).Scala Collections FoldRight Method<\/p>\r\n\r\n\r\n\r\n<h4><strong>use case 1:<\/strong><\/h4>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>The code below shows how to sum all the donut prices from the sequence by using the foldRight method.\r\n\r\nscala&gt; val prices: Seq[Double] = Seq(1.5, 2.0, 2.5)\r\nscala&gt; val sum = prices.foldRight(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=\"452\" height=\"100\" class=\"wp-image-9183\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-503.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-503.png 452w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-503-300x66.png 300w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/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>Create a String of all  using foldRight function\r\nscala&gt; val eduprwa: Seq[String] = Seq(\"Data\", \"Science\", \"Prwatech\")\r\nscala&gt; println(s\"All eduprwa = ${eduprwa.foldRight(\"\")((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=\"606\" height=\"86\" class=\"wp-image-9186\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-506.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-506.png 606w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-506-300x43.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>scala&gt; val prices: Seq[Int] = Seq(1, 2, 2)\r\nscala&gt; val sum = prices.foldRight(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=\"434\" height=\"110\" class=\"wp-image-9187\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-507.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-507.png 434w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-507-300x76.png 300w\" sizes=\"auto, (max-width: 434px) 100vw, 434px\" \/><\/figure>\r\n","protected":false},"excerpt":{"rendered":"<p>Scala Collections FoldRight Method &nbsp; The foldRight work is relevant to both Scala&#8217;s Mutable and Immutable assortment information structures. The foldRight technique takes an affiliated parallel 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 option to left 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":[1445,1443,1438,1439,1444,1441,1440,1447,1446,1442],"class_list":["post-9155","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-define-scala-fold-right","tag-describe-about-scala-fold-right","tag-fold-right","tag-fold-right-scala","tag-how-scala-fold-right-function-is-used","tag-scala-fold-right","tag-scala-fold-right-function","tag-scala-fold-right-function-examples","tag-scala-fold-right-use-cases","tag-what-is-scala-fold-right"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Scala Collections FoldRight Method - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Scala Collections FoldRight 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 FoldRight Method - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Scala Collections FoldRight 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-right\/\" \/>\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-30T13:23:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T12:16:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-503.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-right\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/\",\"name\":\"Scala Collections FoldRight Method - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-503.png\",\"datePublished\":\"2021-05-30T13:23:28+00:00\",\"dateModified\":\"2024-04-13T12:16:11+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Scala Collections FoldRight Method - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-503.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-503.png\",\"width\":452,\"height\":100},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Fold Right\"}]},{\"@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 FoldRight Method - Prwatech","description":"Master Scala Collections FoldRight 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 FoldRight Method - Prwatech","og_description":"Master Scala Collections FoldRight Method - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-05-30T13:23:28+00:00","article_modified_time":"2024-04-13T12:16:11+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-503.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-right\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/","name":"Scala Collections FoldRight Method - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-503.png","datePublished":"2021-05-30T13:23:28+00:00","dateModified":"2024-04-13T12:16:11+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Scala Collections FoldRight Method - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-503.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-503.png","width":452,"height":100},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Fold Right"}]},{"@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\/9155","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=9155"}],"version-history":[{"count":4,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9155\/revisions"}],"predecessor-version":[{"id":11465,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9155\/revisions\/11465"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}