{"id":442,"date":"2012-09-26T08:40:32","date_gmt":"2012-09-26T08:40:32","guid":{"rendered":"http:\/\/217.76.133.156\/?p=442"},"modified":"2012-09-26T08:40:32","modified_gmt":"2012-09-26T08:40:32","slug":"from-java-to-scala","status":"publish","type":"post","link":"https:\/\/www.josemalvarez.es\/?p=442","title":{"rendered":"From Java to Scala&#8230;"},"content":{"rendered":"<p>This week I have started a <a title=\"Coursera Scala\" href=\"https:\/\/class.coursera.org\/progfun-2012-001\">course<\/a> about <a title=\"Scala\" href=\"http:\/\/www.scala-lang.org\/\">Scala<\/a>\u00a0teached by <a title=\"Martin Odersky\" href=\"http:\/\/lampwww.epfl.ch\/~odersky\/\">Martin Odersky<\/a>\u00a0(the creator of this programming language). The course is basically focused on Functional programming with Scala and it reminds me my first steps in this paradigm when I was student of <a title=\"Labra's Home Page\" href=\"http:\/\/www.di.uniovi.es\/~labra\">Labra<\/a>&#8216;s course &#8220;Functional and Logic Programming&#8221; in which I learnt some portions of Prolog and Haskell.<\/p>\n<p>After some years using Java and encouraged by Labra I enrolled in this new language (I had performed my first steps in Scala some time ago but without specific objectives). The learning methodology is very easy: you have to follow and watch some video lectures and after that you have some assingments to be completed in the a fixed schedule, if you complete all assignments you can obtain a certificate!<\/p>\n<p>The purpose of this post is to show (my own experience) how the same function can be coded in Java or Scala (this is the typical example of moving from an imperative paradigm to a functional one). Imagine that you have to calculate the sum of an integer list (1, 3, 5)<\/p>\n<ul>\n<li>In Java, you will use something like&#8230;<\/li>\n<\/ul>\n<div>\n<pre>int sum = 0;\r\nfor (int i = 0; i&lt;l.size();i++){\r\n sum = sum + l.get(i);\r\n}\r\nreturn sum;<\/pre>\n<\/div>\n<ul>\n<li>In Scala, using a Java style&#8230;<\/li>\n<\/ul>\n<div>\n<pre>  def sum(xs: List[Int]): Int = {\r\n\tvar sum = 0\r\n\tfor (x &lt;- xs)\r\n\t\tsum = sum + x\r\n\treturn sum\r\n }<\/pre>\n<\/div>\n<ul>\n<li>In Scala, using recursion and Java style&#8230;<\/li>\n<\/ul>\n<div>\n<pre>def sum(xs: List[Int]): Int {\r\n    if (xs.isEmpty) return 0\r\n    else return xs.head + sum(xs.tail)\r\n}<\/pre>\n<\/div>\n<ul>\n<li>In Scala, using a more functional approach&#8230;<\/li>\n<\/ul>\n<div>\n<pre>def sum(xs: List[Int]): Int = xs match{\r\n    case _ if xs.isEmpty =&gt; 0\r\n    case _ =&gt; xs.head + sum (xs.tail)\r\n}<\/pre>\n<\/div>\n<ul>\n<li>In Scala, pure functional approach<\/li>\n<\/ul>\n<div>\n<pre>def sum(xs: List[Int]): Int = xs.foldLeft(0)(_+_)<\/pre>\n<\/div>\n<div>This is only to demonstrate how a good use of tools and programming techniques can ease the coding!<\/div>\n","protected":false},"excerpt":{"rendered":"<a href=\"https:\/\/www.josemalvarez.es\/?p=442\" rel=\"bookmark\" title=\"Permalink to From Java to Scala&#8230;\"><p>This week I have started a course about Scala\u00a0teached by Martin Odersky\u00a0(the creator of this programming language). The course is basically focused on Functional programming with Scala and it reminds me my first steps in this paradigm when I was student of Labra&#8216;s course &#8220;Functional and Logic Programming&#8221; in which I learnt some portions of [&hellip;]<\/p>\n<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[114,11,113,27],"class_list":{"0":"post-442","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-research-blog","7":"tag-programming","8":"tag-research","9":"tag-scala","10":"tag-software-2","11":"h-entry","12":"hentry"},"_links":{"self":[{"href":"https:\/\/www.josemalvarez.es\/index.php?rest_route=\/wp\/v2\/posts\/442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.josemalvarez.es\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.josemalvarez.es\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.josemalvarez.es\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.josemalvarez.es\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=442"}],"version-history":[{"count":2,"href":"https:\/\/www.josemalvarez.es\/index.php?rest_route=\/wp\/v2\/posts\/442\/revisions"}],"predecessor-version":[{"id":444,"href":"https:\/\/www.josemalvarez.es\/index.php?rest_route=\/wp\/v2\/posts\/442\/revisions\/444"}],"wp:attachment":[{"href":"https:\/\/www.josemalvarez.es\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.josemalvarez.es\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.josemalvarez.es\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}