{"id":8473,"date":"2021-04-27T16:17:58","date_gmt":"2021-04-27T16:17:58","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=8473"},"modified":"2024-04-13T09:01:56","modified_gmt":"2024-04-13T09:01:56","slug":"scala-lambda-function","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lambda-function\/","title":{"rendered":"Scala &#8211; Lambda Function"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Lambda Expressions 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;}\">Lambda Expressions in Scala<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p><strong>A Lambda<\/strong> <a href=\"https:\/\/prwatech.in\/blog\/#\">Expression<\/a> is an expression <a href=\"http:\/\/scala-lang.org\">that<\/a> uses an <strong>Anonymous function <\/strong>rather than value or variable. Lambda expressions are more helpful when there is a simple function to be utilized in one place. These expressions are quicker and more expressive than characterizing an entire function. We can make our lambda expressions reusable for any sort of transformations.<\/p>\r\n<p>Lambda expressions, also known as anonymous functions or function literals, are a fundamental feature of functional programming in Scala. A lambda expression is a concise way to define a function without explicitly specifying a name. Instead, it defines the function&#8217;s behavior inline, often as a parameter to another function or as a standalone expression.<\/p>\r\n<p>In Scala, lambda expressions are created using the <code>=&gt;<\/code> syntax, which separates the function parameters from the function body. For example, <code>(x: Int) =&gt; x * 2<\/code> is a lambda expression that takes an integer <code>x<\/code> as input and returns <code>x<\/code> multiplied by <code>2<\/code>.<\/p>\r\n<p>Lambda expressions are commonly used with higher-order functions like <code>map<\/code>, <code>filter<\/code>, and <code>reduce<\/code> to process collections in a functional style. They enable functional programming paradigms such as immutability and referential transparency by treating functions as first-class citizens.<\/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>object prwatech {\r\n\/\/ Main method\r\ndef main(args:Array[String])\r\n{\r\n\/\/ lambda expression\r\nval ex1 = (x:Int) =&gt; x + 4\r\n\r\n\/\/ with multiple parameters\r\nval ex2 = (x:Int, y:Int) =&gt; x * y\r\n\r\nprintln(ex1(7))\r\nprintln(ex2(2, 3))\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>11<br \/>6<\/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>object eduprwa {\r\ndef main(args:Array[String])\r\n{\r\n\/\/ list of numbers\r\nval a = List(1, 2, 3, 4, 5, 6, 7)\r\n\/\/ squaring each element of the list\r\nval res = a.map( (x:Int) =&gt; x * x )\r\n\/* OR\r\nval res = a.map( x=&gt; x * x )\r\n*\/\r\nprintln(res)\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>val a = List(1, 2, 3, 4, 5, 6, 7)<\/p>\r\n\r\n\r\n\r\n<p>use case 3:<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>\/\/ Scala program to apply\r\n\/\/ transformation on collection\r\n  object lambda {\r\n\/\/ Main method\r\n  def main(args:Array[String])\r\n  {\r\n\/\/ list of numbers\r\n  val x1 = List(1, 2, 0, 6, 7, 8)\r\n  val x2 = List(10, 20, 30)\r\n\/\/ reusable lambda\r\n    val func = (x:Int) =&gt; x * x\r\n\r\n    \/\/ squaring each element of the lists\r\n    val res1 = x1.map( func )\r\n    val res2 = x2.map( func )\r\n\r\n    println(res1)\r\n    println(res2)\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>List(1, 4, 0, 36, 49, 64)<br \/>List(100, 400, 900)<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Lambda Expressions in Scala &nbsp; A Lambda Expression is an expression that uses an Anonymous function rather than value or variable. Lambda expressions are more helpful when there is a simple function to be utilized in one place. These expressions are quicker and more expressive than characterizing an entire function. We can make our 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":[1175,1172,1166,1173,1169,1170,1171,1176,1167,1168],"class_list":["post-8473","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-explain-lambda-function-in-scala","tag-lambda-definition","tag-lambda-function","tag-lambda-programming-examples","tag-lambda-use-cases-in-scala","tag-scala-lambda","tag-scala-lambda-definition","tag-scala-lambda-examples","tag-scala-lambda-function","tag-what-is-lambda-function"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Lambda Expressions in Scala - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Lambda Expressions in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\" \/>\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=\"Lambda Expressions in Scala - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Lambda Expressions in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lambda-function\/\" \/>\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-27T16:17:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T09:01:56+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-lambda-function\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lambda-function\/\",\"name\":\"Lambda Expressions in Scala - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2021-04-27T16:17:58+00:00\",\"dateModified\":\"2024-04-13T09:01:56+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Lambda Expressions in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lambda-function\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lambda-function\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lambda-function\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Lambda Function\"}]},{\"@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":"Lambda Expressions in Scala - Prwatech","description":"Master Lambda Expressions in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","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":"Lambda Expressions in Scala - Prwatech","og_description":"Master Lambda Expressions in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lambda-function\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-04-27T16:17:58+00:00","article_modified_time":"2024-04-13T09:01:56+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-lambda-function\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lambda-function\/","name":"Lambda Expressions in Scala - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2021-04-27T16:17:58+00:00","dateModified":"2024-04-13T09:01:56+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Lambda Expressions in Scala - Dive deep with our expert instructors and comprehensive curriculum, Enroll now.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lambda-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lambda-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-lambda-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Lambda Function"}]},{"@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\/8473","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=8473"}],"version-history":[{"count":5,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8473\/revisions"}],"predecessor-version":[{"id":11429,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8473\/revisions\/11429"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=8473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=8473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=8473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}