{"id":9293,"date":"2021-05-30T16:36:39","date_gmt":"2021-05-30T16:36:39","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9293"},"modified":"2024-04-13T13:19:29","modified_gmt":"2024-04-13T13:19:29","slug":"scala-scan-scala-tutorial-scan-function-example","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-scan-scala-tutorial-scan-function-example\/","title":{"rendered":"Scala &#8211; Scan"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Scala Tutorial - Scan 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 Scan Function Example<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Scan <\/strong>operation accepts the binary <a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-fold-right\/\">operation<\/a> as boundary and <a href=\"http:\/\/scala-lang.org\">returns<\/a> the incentive for every component in collections for that activity. It returns every cycle for that twofold administrator in the collections. In scan additionally we can characterize the underlying worth.<\/p>\r\n<p>he <code>scan<\/code> function in Scala is a powerful method used to perform cumulative operations on a collection, producing intermediate results at each step. It is similar to the <code>foldLeft<\/code> or <code>foldRight<\/code> functions but differs in that it returns a collection of all intermediate results, including the final result.<\/p>\r\n<p>The <code>scan<\/code> function takes an initial value and a binary operator as parameters. It applies the binary operator sequentially to each element of the collection, starting with the initial value.\u00a0<\/p>\r\n<p>The <code>scan<\/code> function is useful for generating cumulative results, exploring intermediate states of computation, and implementing dynamic programming algorithms efficiently. Understanding how to use <code>scan<\/code> effectively is essential for mastering functional programming concepts in Scala. Scala Tutorial Scan Function Example<\/p>\r\n\r\n\r\n\r\n<h3><strong>use case 1:<\/strong><\/h3>\r\n<p>In addition to its practical applications, understanding the <code>scan<\/code> function contributes to a deeper grasp of functional programming concepts in Scala, such as immutability, higher-order functions, and the use of combinators to transform data. By exploring examples and experimenting with the <code>scan<\/code> function, developers can gain insights into how functional programming techniques can be leveraged to solve complex problems concisely and elegantly in Scala. This introduction sets the stage for exploring practical examples and applications of the <code>scan<\/code> function, demonstrating its versatility and utility in data processing and algorithm design.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>\/\/ Scala program sum of elements \r\n\/\/ using scan 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 numbers\r\n        val numbers: Seq[Int] = Seq(3, 2, 1)\r\n        println(s\"Elements of numbers = $numbers\")\r\n  \r\n        \/\/find the sum of the elements using scan function\r\n        val iterations: Seq[Int] = numbers.scan(0)(_ + _)\r\n        println(\"Running total of all elements\" + s\"in the collection = $iterations\")\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 of numbers = List(3, 2, 1)\r\nRunning total of all elementsin the collection = List(0, 3, 5, 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 concatenate string \r\n\/\/ using scan 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 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 scan function\r\n        val concat : Seq[String]\r\n                    = str_elements.scan(\"\")((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 = List(, -hello, -hello-Prwa, -hello-Prwa-Tech, -hello-Prwa-Tech-Bangalore)\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(0.5, 5.0, 1)\r\n        println(s\"Elements = $seq_elements\") \r\n  \r\n        \/\/ find the multiplication 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\"multiplicaion 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, 5.0, 1.0)\r\nmultiplicaion of elements = 2.5\r\n<\/code><\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>Scala Tutorial Scan Function Example &nbsp; Scan operation accepts the binary operation as boundary and returns the incentive for every component in collections for that activity. It returns every cycle for that twofold administrator in the collections. In scan additionally we can characterize the underlying worth. he scan function in Scala is a powerful method [&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":[1513,1512,1514,1510,1516,1509,1515,1517,1518,1511],"class_list":["post-9293","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-define-scan-function","tag-describe-scan-function","tag-how-scan-is-used-in-scala","tag-scala-scan-function","tag-scala-scan-function-examples","tag-scan-function","tag-scan-function-usecases","tag-scan-method-scala","tag-scan-scala","tag-what-is-scan-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 Scan Function Example - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Scala Tutorial Scan 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 Scan Function Example - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Scala Tutorial Scan 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-scan-scala-tutorial-scan-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-30T16:36:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T13:19: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-scan-scala-tutorial-scan-function-example\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-scan-scala-tutorial-scan-function-example\/\",\"name\":\"Scala Tutorial Scan Function Example - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2021-05-30T16:36:39+00:00\",\"dateModified\":\"2024-04-13T13:19:29+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Scala Tutorial Scan Function Example - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-scan-scala-tutorial-scan-function-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-scan-scala-tutorial-scan-function-example\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-scan-scala-tutorial-scan-function-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Scan\"}]},{\"@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 Scan Function Example - Prwatech","description":"Master Scala Tutorial Scan 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 Scan Function Example - Prwatech","og_description":"Master Scala Tutorial Scan Function Example - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-scan-scala-tutorial-scan-function-example\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-05-30T16:36:39+00:00","article_modified_time":"2024-04-13T13:19: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-scan-scala-tutorial-scan-function-example\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-scan-scala-tutorial-scan-function-example\/","name":"Scala Tutorial Scan Function Example - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2021-05-30T16:36:39+00:00","dateModified":"2024-04-13T13:19:29+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Scala Tutorial Scan Function Example - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-scan-scala-tutorial-scan-function-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-scan-scala-tutorial-scan-function-example\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-scan-scala-tutorial-scan-function-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Scan"}]},{"@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\/9293","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=9293"}],"version-history":[{"count":4,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9293\/revisions"}],"predecessor-version":[{"id":11477,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9293\/revisions\/11477"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}