{"id":8610,"date":"2021-05-20T16:39:06","date_gmt":"2021-05-20T16:39:06","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=8610"},"modified":"2024-04-13T10:27:16","modified_gmt":"2024-04-13T10:27:16","slug":"scala-high-order-functions","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/","title":{"rendered":"Scala &#8211; High order functions"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Higher Order Functions in Scala&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;}\">Higher Order Functions in Scala<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p>In <a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-a-quick-overview\/\">Scala<\/a>,\u00a0 are the functions that take other functions as parameters or return a function as a result. This is because functions are first-class values in Scala. so we can say High order functions are the function for both methods and functions that can take functions as parameters or that return a function.<\/p>\r\n<p>\u00a0This powerful functional programming concept enables developers to write concise, expressive, and reusable code by treating functions as first-class citizens.<\/p>\r\n<p>In Scala, functions are values that can be assign to variables, passe as arguments to other functions, or return from functions. This flexibility allows for the creation of higher order functions, which enhance code modularity and enable sophisticat abstractions.<\/p>\r\n<p>Common examples of higher order functions in Scala include <code>map<\/code>, <code>filter<\/code>, <code>reduce<\/code>, and <code>fold<\/code>, which operate on collections and accept functions as arguments to perform transformations, filtering, and aggregations. By leveraging higher order functions, developers can abstract away common patterns and algorithms, making code more generic and reusable.<\/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>val salaries = Seq(20000, 70000, 40000)\r\nval doubleSalary = (x: Int) =&gt; x * 2\r\nval newSalaries = salaries.map(doubleSalary)\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=\"604\" height=\"151\" class=\"wp-image-8611\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-46.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-46.png 604w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-46-300x75.png 300w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/figure>\r\n\r\n\r\n\r\n<p><code>doubleSalary<\/code>\u00a0is a function which takes a single Int,\u00a0<code>x<\/code>, and returns\u00a0<code>x * 2<\/code>. In general, the tuple on the left of the arrow\u00a0<code>=&gt;<\/code> is a parameter list and the value of the expression on the right is what gets return. On line 3, the function <code>doubleSalary<\/code> gets appli to each element in the list of salaries.<\/p>\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>val salaries = Seq(20000, 70000, 40000)\r\nval newSalaries = salaries.map(x =&gt; x * 2)\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=\"454\" height=\"100\" class=\"wp-image-8612\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-47.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-47.png 454w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-47-300x66.png 300w\" sizes=\"auto, (max-width: 454px) 100vw, 454px\" \/><\/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 program of higher order\r\n\/\/ function\r\n\/\/ Creating object\r\nobject prwatech {\r\n\/\/ Main method\r\n    def main(arg:Array[String])\r\n    {\r\n      \/\/ Displays output by assigning \r\n        \/\/ value and calling functions\r\n        println(apply(format, 15))\r\n      \r\n    }\r\n      \r\n    \/\/ A higher order function\r\n    def apply(x: Double =&gt; String, y: Double) = x(y)\r\n  \r\n    \/\/ Defining a function for\r\n    \/\/ the format and using a\r\n    \/\/ method toString()\r\n    def format[R](z: R) = \"{\" + z.toString() + \"}\"\r\n      \r\n}<\/code><\/pre>\r\n\r\n\r\n\r\n<p>output:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>{15.0}\r\n<\/code><\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>Higher Order Functions in Scala &nbsp; In Scala,\u00a0 are the functions that take other functions as parameters or return a function as a result. This is because functions are first-class values in Scala. so we can say High order functions are the function for both methods and functions that can take functions as parameters or [&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":[1327,1332,1326,1324,1333,1329,1325,1330,1331,1328],"class_list":["post-8610","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-define-high-order-function","tag-example-program-of-high-order-functions-in-scala","tag-explain-high-order-function","tag-high-order-functions","tag-how-high-order-function-is-used-in-scala","tag-scala-high-order-function-example-programs","tag-scala-high-order-functions","tag-scala-use-cases-for-high-order-functions","tag-use-of-high-order-function","tag-what-is-high-order-functions-in-scala"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Higher Order Functions in Scala - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Higher Order Functions in Scala - 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=\"Higher Order Functions in Scala - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Higher Order Functions in Scala - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/\" \/>\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-20T16:39:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T10:27:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-46.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-high-order-functions\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/\",\"name\":\"Higher Order Functions in Scala - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-46.png\",\"datePublished\":\"2021-05-20T16:39:06+00:00\",\"dateModified\":\"2024-04-13T10:27:16+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Higher Order Functions in Scala - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-46.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-46.png\",\"width\":604,\"height\":151},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; High order functions\"}]},{\"@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":"Higher Order Functions in Scala - Prwatech","description":"Master Higher Order Functions in Scala - 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":"Higher Order Functions in Scala - Prwatech","og_description":"Master Higher Order Functions in Scala - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-05-20T16:39:06+00:00","article_modified_time":"2024-04-13T10:27:16+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-46.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-high-order-functions\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/","name":"Higher Order Functions in Scala - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-46.png","datePublished":"2021-05-20T16:39:06+00:00","dateModified":"2024-04-13T10:27:16+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Higher Order Functions in Scala - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-46.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/05\/image-46.png","width":604,"height":151},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; High order functions"}]},{"@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\/8610","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=8610"}],"version-history":[{"count":5,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8610\/revisions"}],"predecessor-version":[{"id":11451,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8610\/revisions\/11451"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=8610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=8610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=8610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}