{"id":2575,"date":"2015-09-12T01:25:36","date_gmt":"2015-09-12T01:25:36","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2575"},"modified":"2015-09-12T01:25:36","modified_gmt":"2015-09-12T01:25:36","slug":"scala-lambda-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-lambda-example\/","title":{"rendered":"Scala Lambda Example"},"content":{"rendered":"<p>Lambdas are just anonymous functions, although typically they are expected to be side-effect free (more of a calculus-style function that does a computation)<\/p>\n<p>For instance, in Javascript you might do this:<\/p>\n<pre lang=\"javascript\">\nfunction x() {\n}\n\n...or....\n\nvar y = function() { }\n\n<\/pre>\n<p>The equivalent in Scala might be:<\/p>\n<pre lang=\"scala\">\nval y = () => {}\n<\/pre>\n<pre lang=\"Scala\"> \ny()\ny: () => Unit = <function0>\n<\/pre>\n<p>So you can see the type it returns is &#8220;Unit&#8221; which appears approximately equivalent to &#8220;void&#8221;, however unlike &#8220;void&#8221; in Java you can declare things as Unit (although with only a single value):<\/p>\n<pre lang=\"Scala\">\nval x: Unit = ()\n<\/pre>\n<p>Whether this is useful is questionable.<\/p>\n<p>You could, for instance make a lambda that doubles a value, and use it like so (the forms listed are all equivalent):<\/p>\n<pre lang=\"scala\">\nval y = (x: Int) => x * 2\n\nList(1, 2, 3).map(y)\nList(1, 2, 3) map y\nList(1, 2, 3).map((x: Int) => x * 2)\n<\/pre>\n<p>All returning:<\/p>\n<pre>\nres11: List[Int] = List(2, 4, 6)\n<\/pre>\n<p>There isn&#8217;t a way to make the lambda type-less (or generic):<\/p>\n<pre>\nval y = (x) => x\n\n<console>:8: error: missing parameter type\n       val y = (x) => x\n<\/pre>\n<p>That said, you can cheat this a couple ways, either with Any (most similar to void* in C):<\/p>\n<pre lang=\"Scala\">\nval y = (x: Any) => x\n\ny: Any => Any = <function1>\n<\/pre>\n<p>Or, instead of making a lambda, make a method (T being a generic type here):<\/p>\n<pre lang=\"Scala\">\ndef y[T] = (x: T) => x \n<\/pre>\n<p>Which does actually work:<\/p>\n<pre>\ny: [T]=> T => T\n\nscala> y(1)\nres7: Int = 1\n<\/pre>\n<p>This is basically defining a method in some object (less obvious if you&#8217;re typing the Scala REPL), although it might otherwise behave similarly to lamdbas. This is familiar, in that it is similar to methods in Java.<\/p>\n<p>In the shorter lambdas, the lack of a &#8220;return&#8221; feels very natural, but as these get longer it starts to feel weird. This is especially true if the code gets longer &#8211; when there are multiple statements, the result of the last one is implicitly returned (when writing code, it can be helpful to define the return type of the function, so you get an error if you accidentally return the wrong thing)<\/p>\n<p>You should resist the temptation to add &#8220;return&#8221; to your functions. The authors of Scala made the questionable decision to change the traditional meaning of return, such that it takes you out of all lambdas, and up out of the most recent method on the stack &#8211; which is likely quite a ways up, and not at all what you&#8217;d expect.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lambdas are just anonymous functions, although typically they are expected to be side-effect free (more of a calculus-style function that does a computation) For instance, in Javascript you might do this: function x() { } &#8230;or&#8230;. var y = function() { } The equivalent in Scala might be: val y = () => {} y() &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/scala-lambda-example\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Scala Lambda Example&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[480,488],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2575"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2575"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2575\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}