{"id":8445,"date":"2021-04-26T12:07:04","date_gmt":"2021-04-26T12:07:04","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=8445"},"modified":"2024-04-13T09:06:57","modified_gmt":"2024-04-13T09:06:57","slug":"scala-anonymous-functions","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-anonymous-functions\/","title":{"rendered":"Scala &#8211; Anonymous Functions"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Anonymous 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;}\">Anonymous 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>, An <strong>anonymous function<\/strong> is called a <a href=\"http:\/\/scala-lang.org\">function<\/a> literal. A function which doesn&#8217;t contain a name is called an anonymous function. An anonymous function gives a simple function definition. It is use when we need to make an <strong>inline function<\/strong>.<\/p>\r\n<p>Anonymous functions, also known as function literals or lambda expressions, are a powerful feature of functional programming in Scala. An anonymous function is a concise way to define a function without explicitly specifying a name or return type, making it ideal for short-lived or one-off operations.<\/p>\r\n<p>In Scala, anonymous functions are defin using the <code>=&gt;<\/code> syntax, which separates the function parameters from the function body. For example, <code>(x: Int) =&gt; x * x<\/code> defines an anonymous function that takes an integer <code>x<\/code> as input and returns its square.<\/p>\r\n<p>Anonymous functions can assign to variables, pass as arguments to higher-order functions, or used inline within method calls. They promote code reuse, abstraction, and functional composition by allowing developers to encapsulate behavior in a compact and readable form.<\/p>\r\n\r\n\r\n\r\n<h3><strong>use case 1:<\/strong> <strong>Anonymous Function without parameters<\/strong><\/h3>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>    \/\/ Scala program to illustrate the anonymous method\r\n    object prwatech{\r\n    def main(args: Array[String])\r\n\r\n    \/\/ Creating anonymous functions without parameter \r\n    var myfun1 = () =&gt; {\"Welcome to Prwatech !\"}\r\n    println(myfun1())\r\n\r\n    \/\/ A function which contain anonymous function as a parameter\r\n    def myfunction(fun:(String, String)=&gt; String) = \r\n    {\r\n        fun(\"BigData\", \" hadoop\")\r\n    }\r\n\r\n    \/\/ Explicit type declaration of anonymous function in another      \r\n     function\r\n    val f1 = myfunction((str1: String,\r\n                str2: String) =&gt; str1 + str2)\r\n\r\n    \/\/ Shorthand declaration using wildcard\r\n    val f2 = myfunction(_ + _)\r\n    println(f1)\r\n    println(f2)\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<p>Welcome to Prwatech !<br \/>BigData hadoop<br \/>BigData hadoop<\/p>\r\n\r\n\r\n\r\n<p><strong>use case 2:<\/strong> <strong>Anonymous function with parameter<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>    \/\/ Scala program to illustrate the anonymous method\r\n    object prwatech\r\n    {\r\n    def main(args: Array[String])\r\n    \/\/ Creating anonymous functions with multiple parameters \r\n     Assign\r\n    \/\/ anonymous functions to variables \r\n    var myfunction1 = (str1:String, str2:String) =&gt; str1 + str2\r\n\r\n    \/\/ An anonymous function is created \r\n    var myfunction2 = (_:String) + (_:String)\r\n\r\n    \/\/ Here, the variable invoke like a function call\r\n    println(myfunction1(\"Big Data\", \" Prwatech\"))\r\n    println(myfunction2(\"Data Science\", \" Prwatech\"))\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<p>Big Data Prwatech<br \/>Data Science Prwatech<\/p>\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 to illustrate the anonymous method\r\n     object prwatech\r\n     {\r\n     def main(args: Array[String])\r\n    \/\/ Creating anonymous functions\r\n    \/\/ with multiple parameters Assign\r\n    var myfunction1 = (str1:String, str2:String) =&gt; str1 + str2\r\n\r\n    \/\/ An anonymous function is created  \r\n    var myfunction2 = (_:String) + (_:String)\r\n\r\n    \/\/ one more anonymous function is created \r\n    var myfunction3 = (_:String) + (_:String)\r\n\r\n    \/\/ Here, the variable invoke like a function call\r\n    println(myfunction1(\"Big Data\", \" Prwatech\"))\r\n    println(myfunction2(\"Data Science\", \" Prwatech\"))\r\n    println(myfunction3(\"Tableau\", \" Prwatech\"))\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<p>Big Data Prwatech<br \/>Data Science Prwatech<br \/>Tableau Prwatech<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Anonymous Functions in Scala &nbsp; In Scala, An anonymous function is called a function literal. A function which doesn&#8217;t contain a name is called an anonymous function. An anonymous function gives a simple function definition. It is use when we need to make an inline function. Anonymous functions, also known as function literals or lambda [&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":[1139,1144,1141,1147,1143,1142,1146,1140,1145],"class_list":["post-8445","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-anonymous-function","tag-anonymous-function-example","tag-anonymous-function-scala","tag-anonymous-function-scala-example","tag-anonymous-function-use-cases","tag-inline-function-creation","tag-sacala-anonymous","tag-scala-anonymous-function","tag-scala-anonymous-function-use-cases"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Anonymous Functions in Scala - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Anonymous 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=\"Anonymous Functions in Scala - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Anonymous 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-anonymous-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-04-26T12:07:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T09:06:57+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=\"1 minute\" \/>\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-anonymous-functions\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-anonymous-functions\/\",\"name\":\"Anonymous Functions in Scala - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2021-04-26T12:07:04+00:00\",\"dateModified\":\"2024-04-13T09:06:57+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Anonymous Functions in Scala - Dive deep with our expert instructors and comprehensive curriculum,.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-anonymous-functions\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-anonymous-functions\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-anonymous-functions\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Anonymous 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":"Anonymous Functions in Scala - Prwatech","description":"Master Anonymous 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":"Anonymous Functions in Scala - Prwatech","og_description":"Master Anonymous Functions in Scala - Dive deep with our expert instructors and comprehensive curriculum,.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-anonymous-functions\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-04-26T12:07:04+00:00","article_modified_time":"2024-04-13T09:06:57+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-anonymous-functions\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-anonymous-functions\/","name":"Anonymous Functions in Scala - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2021-04-26T12:07:04+00:00","dateModified":"2024-04-13T09:06:57+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Anonymous Functions in Scala - Dive deep with our expert instructors and comprehensive curriculum,.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-anonymous-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-anonymous-functions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-anonymous-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Anonymous 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\/8445","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=8445"}],"version-history":[{"count":16,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8445\/revisions"}],"predecessor-version":[{"id":11431,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8445\/revisions\/11431"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=8445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=8445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=8445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}