{"id":9280,"date":"2021-05-30T15:06:03","date_gmt":"2021-05-30T15:06:03","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9280"},"modified":"2024-04-13T12:48:29","modified_gmt":"2024-04-13T12:48:29","slug":"scala-reduce-2-scala-tutorial-reduce-function-example","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-2-scala-tutorial-reduce-function-example\/","title":{"rendered":"Scala &#8211; Reduce"},"content":{"rendered":"\r\n<h2>Scala Tutorial Reduce Function Example<\/h2>\r\n<p>&nbsp;<\/p>\r\n<p>Function on <a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/\">assortment<\/a> information <a href=\"http:\/\/scala-lang.org\">structure<\/a> in scala that contains records, sets, guides, succession and tuples.<\/p>\r\n<p>The <code>reduce<\/code> function in Scala is a higher-order function use to combine elements of a collection into a single result by applying a binary operation repeatedly. This function is particularly useful for aggregating values, computing cumulative results, or summarizing data in a concise manner.<\/p>\r\n<p>In Scala, the <code>reduce<\/code> function takes a binary operator as an argument and applies it sequentially to elements of a collection.\u00a0<\/p>\r\n<p>In this example, <code>reduce(_ + _)<\/code> iteratively applies addition (<code>_ + _<\/code>) to combine elements of the list, resulting in the sum of all elements.<\/p>\r\n<p>Understanding how to use the <code>reduce<\/code> function is essential for functional programming in Scala, as it promotes concise and expressive code for data aggregation and computation. By leveraging <code>reduce<\/code>, developers can streamline data processing tasks and harness the power of functional programming paradigms effectively.Scala Tutorial Reduce Function Example etc<\/p>\r\n<p>Understanding how to use the <code>reduce<\/code> function is essential for functional programming in Scala, as it enables concise and elegant solutions to data aggregation tasks. By leveraging <code>reduce<\/code>, developers can perform complex computations on collections with minimal code and maximum readability.<\/p>\r\n\r\n\r\n\r\n<h3><strong>use case 1:<\/strong><\/h3>\r\n<p>In this example:<\/p>\r\n<ul>\r\n<li><code>numbers<\/code> is a <code>List<\/code> containing integers <code>[1, 2, 3, 4, 5]<\/code>.<\/li>\r\n<li>The <code>reduce<\/code> function is on the <code>numbers<\/code> list.<\/li>\r\n<li>The function <code>(x, y) =&gt; x + y<\/code> is passed as an argument to <code>reduce<\/code>.\r\n<ul>\r\n<li>Here, <code>x<\/code> and <code>y<\/code> are successive elements of the list.<\/li>\r\n<li>The function <code>x + y<\/code> is applied iteratively to combine elements from the list.<\/li>\r\n<\/ul>\r\n<\/li>\r\n<li>The result of <code>reduce<\/code> is the sum of all elements in the list, computed as:\r\n<ul>\r\n<li><code>1 + 2<\/code> (resulting in <code>3<\/code>)<\/li>\r\n<li><code>3 + 3<\/code> (resulting in <code>6<\/code>)<\/li>\r\n<li><code>6 + 4<\/code> (resulting in <code>10<\/code>)<\/li>\r\n<li><code>10 + 5<\/code> (resulting in <code>15<\/code>)<\/li>\r\n<\/ul>\r\n<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>\/\/ Scala program sum of elements \r\n\/\/ using reduce 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(3.1, 2.0, 1.5)\r\n        println(s\"Elements = $seq_elements\") \r\n  \r\n        \/\/ find the sum of the elements\r\n        \/\/ using reduce function\r\n        val sum: Double = seq_elements.reduce((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(3.1, 2.0, 1.5)\r\nSum of elements = 6.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>\/\/ Scala program to find maximum and minimum \r\n\/\/ using reduce 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(2.1, 2.0, 1.5)\r\n        println(s\"Elements = $seq_elements\")\r\n  \r\n        \/\/ find the maximum element using reduce function\r\n        val maximum : Double = seq_elements.reduce(_ max _)\r\n        println(s\"Maximum element = $maximum\")\r\n  \r\n        \/\/ find the minimum element using reduce function\r\n        val minimum : Double = seq_elements.reduce(_ min _)\r\n        println(s\"Minimum element = $minimum\")\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(2.1, 2.0, 1.5)\r\nMaximum element = 2.1\r\nMinimum element = 1.5\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 elements\r\n        val seq_elements: Seq[Double] = Seq(3.5, 2.0)\r\n        println(s\"Elements = $seq_elements\") \r\n  \r\n        \/\/ find the diff of the elements\r\n        \/\/ using reduce function\r\n        val diff: Double = seq_elements.reduce((a, b) =&gt; a - b)\r\n        println(s\"diff of elements = $diff\")\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(3.5, 2.0)\r\ndiff of elements = 1.5\r\n<\/code><\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>Scala Tutorial Reduce Function Example &nbsp; Function on assortment information structure in scala that contains records, sets, guides, succession and tuples. The reduce function in Scala is a higher-order function use to combine elements of a collection into a single result by applying a binary operation repeatedly. This function is particularly useful for aggregating values, [&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":[1485,1484,1482,1376,1382,1384,1481,1486,1381,1483],"class_list":["post-9280","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-define-reduce-function-in-scala","tag-describe-reduce-function","tag-how-reduce-function-is-used","tag-reduce-function","tag-reduce-function-in-scala","tag-reduce-method","tag-reduce-method-in-scala","tag-scala-reduce-function","tag-scala-reduce-function-use-cases","tag-what-is-the-use-of-reduce-function-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 Reduce Function Example - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Scala Tutorial Reduce 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 Reduce Function Example - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Scala Tutorial Reduce 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-2-scala-tutorial-reduce-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:06:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T12:48:29+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-reduce-2-scala-tutorial-reduce-function-example\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-2-scala-tutorial-reduce-function-example\/\",\"name\":\"Scala Tutorial Reduce Function Example - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2021-05-30T15:06:03+00:00\",\"dateModified\":\"2024-04-13T12:48:29+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Scala Tutorial Reduce Function Example - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-2-scala-tutorial-reduce-function-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-2-scala-tutorial-reduce-function-example\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-2-scala-tutorial-reduce-function-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Reduce\"}]},{\"@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 Reduce Function Example - Prwatech","description":"Master Scala Tutorial Reduce 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 Reduce Function Example - Prwatech","og_description":"Master Scala Tutorial Reduce Function Example - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-2-scala-tutorial-reduce-function-example\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-05-30T15:06:03+00:00","article_modified_time":"2024-04-13T12:48:29+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-reduce-2-scala-tutorial-reduce-function-example\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-2-scala-tutorial-reduce-function-example\/","name":"Scala Tutorial Reduce Function Example - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2021-05-30T15:06:03+00:00","dateModified":"2024-04-13T12:48:29+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Scala Tutorial Reduce Function Example - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-2-scala-tutorial-reduce-function-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-2-scala-tutorial-reduce-function-example\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-reduce-2-scala-tutorial-reduce-function-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Reduce"}]},{"@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\/9280","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=9280"}],"version-history":[{"count":5,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9280\/revisions"}],"predecessor-version":[{"id":11471,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9280\/revisions\/11471"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}