{"id":9118,"date":"2021-05-30T13:09:05","date_gmt":"2021-05-30T13:09:05","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9118"},"modified":"2024-04-13T12:22:35","modified_gmt":"2024-04-13T12:22:35","slug":"scala-fold-scala-tutorial-fold-function-example","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-scala-tutorial-fold-function-example\/","title":{"rendered":"Scala &#8211; Fold"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Scala Tutorial - Fold 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 Fold Function Example<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Fold <\/strong>takes a twofold activity which <a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/\">consolidates<\/a> every one of the components from the assortment and returns a solitary worth. The folds permits to characterize an underlying worth. <a href=\"http:\/\/scala-lang.org\">Because<\/a> of this property, fold can likewise oversee void assortments. On the off chance that the collection is unfilled, the value introduced turns into the last answer. Because of this we can likewise return an alternate worth from the arrangement of assortment utilizing beginning worth of some other datatype.<\/p>\r\n<p>The <code>fold<\/code> function in Scala is a fundamental higher-order function used to combine elements of a collection into a single result by applying a binary operation. Understanding <code>fold<\/code> is essential for functional programming in Scala, as it enables concise and expressive ways to process collections.<\/p>\r\n<p>The <code>fold<\/code> function takes an initial value (often referred to as an accumulator) and a binary operation as parameters. It then sequentially combines each element of the collection with the accumulator using the specified binary operation. The result is the final accumulated value after processing all elements of the collection.<\/p>\r\n<p>In this example, <code>fold(0)(_ + _)<\/code> starts with an initial accumulator value of <code>0<\/code> and adds each element of the List to the accumulator using the addition operation (<code>_ + _<\/code>). The result (<code>sum<\/code>) will be <code>15<\/code>, which is the sum of all elements in the List.<\/p>\r\n<p>By mastering the <code>fold<\/code> function, Scala developers can efficiently perform complex transformations and aggregations on collections, leading to more concise and functional code.Scala Tutorial Fold Function Example<\/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>\/\/ Scala program sum of elements \r\n\/\/ using fold function \r\n\/\/ Creating object\r\nobject prwatech {\r\n\/\/ Main method\r\n    def main(arg:Array[String])\r\n    {\r\n        \/\/ initialize a sequence of elements\r\n        val seq_elements: Seq[Double] = Seq(.5, 2.0, 1.1)\r\n        println(s\"Elements = $seq_elements\") \r\n  \r\n        \/\/ find the sum of the elements using fold function\r\n        val sum: Double = seq_elements.fold(0.0)((a, b) =&gt; a + b)\r\n        println(s\"Sum of elements = $sum\")\r\n    }\r\n}\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>output:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Elements = List(0.5, 2.0, 1.1)\r\nSum of elements = 3.6\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>use case 2:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>object prwatech {\r\n\/\/ Main method\r\n    def main(arg:Array[String])\r\n    {\r\n        \/\/ initialize a sequence of elements\r\n        val seq_elements: Seq[Double] = Seq(.5, 2.0, 1.1)\r\n        println(s\"Elements = $seq_elements\") \r\n  \r\n        \/\/ find the difference of the elements using fold function\r\n        val sum: Double = seq_elements.fold(0.0)((a, b) =&gt; a - b)\r\n        println(s\"difference of elements = $sum\")\r\n    }\r\n}\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>output:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Elements = List(0.5, 2.0, 1.1)\r\ndifference of elements = -3.6\r\n<\/code><\/pre>\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>object prwatech {\r\n\/\/ Main method\r\n    def main(arg:Array[String])\r\n    {\r\n        \/\/ initialize a sequence of strings\r\n        val str_elements : Seq[String] = Seq(\"hello\", \"Prwa\", \"Tech\", \"Bangalore\")\r\n        println(s\"Elements = $str_elements\")\r\n  \r\n        \/\/ Concatenate strings with fold function\r\n        val concat: String = str_elements.fold(\"\")(\r\n                                (a, b) =&gt; a + \"-\" + b)\r\n        println(s\"After concatenation = $concat\")\r\n    }\r\n}\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>output:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Elements = List(hello, Prwa, Tech, Bangalore)\r\nAfter concatenation = -hello-Prwa-Tech-Bangalore\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Scala Tutorial Fold Function Example &nbsp; Fold takes a twofold activity which consolidates every one of the components from the assortment and returns a solitary worth. The folds permits to characterize an underlying worth. Because of this property, fold can likewise oversee void assortments. On the off chance that the collection is unfilled, the value [&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":[1436,1435,1428,1429,1437,1431,1432,1434,1430,1433],"class_list":["post-9118","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-describe-fold-function","tag-describe-fold-scala","tag-fold","tag-fold-function","tag-fold-function-examples-in-scala","tag-fold-method-in-scala","tag-fold-scala","tag-how-fold-is-used-in-scala","tag-scala-fold","tag-what-is-fold-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 Fold Function Example - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Scala Tutorial Fold 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 Fold Function Example - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Scala Tutorial Fold 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-fold-scala-tutorial-fold-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-30T13:09:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T12:22:35+00:00\" \/>\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-scala-tutorial-fold-function-example\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-scala-tutorial-fold-function-example\/\",\"name\":\"Scala Tutorial Fold Function Example - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2021-05-30T13:09:05+00:00\",\"dateModified\":\"2024-04-13T12:22:35+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Scala Tutorial Fold Function Example - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-scala-tutorial-fold-function-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-scala-tutorial-fold-function-example\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-scala-tutorial-fold-function-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Fold\"}]},{\"@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 Fold Function Example - Prwatech","description":"Master Scala Tutorial Fold 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 Fold Function Example - Prwatech","og_description":"Master Scala Tutorial Fold Function Example - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-scala-tutorial-fold-function-example\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-05-30T13:09:05+00:00","article_modified_time":"2024-04-13T12:22:35+00:00","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-scala-tutorial-fold-function-example\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-scala-tutorial-fold-function-example\/","name":"Scala Tutorial Fold Function Example - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2021-05-30T13:09:05+00:00","dateModified":"2024-04-13T12:22:35+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Scala Tutorial Fold Function Example - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-scala-tutorial-fold-function-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-scala-tutorial-fold-function-example\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-scala-tutorial-fold-function-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Fold"}]},{"@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\/9118","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=9118"}],"version-history":[{"count":6,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9118\/revisions"}],"predecessor-version":[{"id":11468,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9118\/revisions\/11468"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}