functional programming

  • In Scala a function is an object. This can be deduced from the existence of a set of traits called scala.Function, scala.Function1,... scala.Function22. The traits define the abstract method apply() (I've already written something about it) and some concrete methods. The use of this trait is like this:

    object Main extends App {
       val succ = (x: Int) => x + 1
       val anonfun1 = new Function1[Int, Int] {
         def apply(x: Int): Int = x + 1
       assert(succ(0) == anonfun1(0))

    This example is taken Scala documentation. What can a function like anonfun1 be useful for? Well, the code shows by itself: succ(0) is equal to anonfun(0). The code of succ and anonfun1 is the same: it takes an Int argument and return an Int which is the argument plus one.

  • As I've noted before, Scala is so functional that every instruction has a value, as it happens in math. Scala is also functional in avoiding the standard three-elements-loop construct:

    /* C/C++/Java classic for loop */
    for (initial condition; comparative condition; incremental instruction) {... }

    This was considered so wrong by Scala designers that they purposely avoided including this construct. Since Scala has while loops, is always possible to replace this missing construct with a classic:

    var variable = <initial state>
    while (variable != <end state>) {
       ... do something...
        variable += 1

    This is however considered bad programming in Scala. Every book would suggest another form on this task:

    for (i <- <initial> to <final>) {... }
  • Being a professional programmer since two decades (and being a programmer since three - wow!) I've learned a lot of languages. I've started when I was a child with Logo, then moved to BASIC. After that it came Pascal and later Modula-2. I've learner Tcl on Solaris, then discovered Perl and forgot everything else for a long while. I've done some Javascript on the web and some Java programming on the Android platform too. I've coded a lot in C (especially tagsistant and magma) and I've done some Joomla development in PHP.

    Each of those languages has its particularities, that's for true. Perl and Tcl are interpreted dynamic languages for fast and furious programming, while Java is bloated enough to fulfill the requirements of enterprise class bureaucracy. C is the boss in the downtown of the hardware with its slim'n'lean functions and PHP has the right mix of functions, classes and chaos to cook a web page with perfectly burnt corners.

    But all of them are imperative, or at least are usually used in the imperative way. And then enters Scala!

  • Peano axioms are the foundation of modern mathematics. The five rules that bring its name define the concept of ordered set of numbers. The axioms are:

    1. 0 is a number
    2. the successor of a number is a number
    3. two different numbers can't have the same successor
    4. 0 is not the successor of any other number
    5. every property of 0 or of any successor of a number which has that property, if a property of every number

    Peano axioms was intended to describe natural numbers: 0, 1, 2, 3, 4, 5,... But the idea of successoris open to a wide range of interpretations. Bertrand Russel writes in its Introduction to Mathematical Philosophy that if we take the notion of pair number and we use it to define what is the successor of a number, Peano axioms still hold: 0, 2, 4, 6, 8,...

    This idea can be elegantly formalized in Scala by defining a successor function for a specific number series:

  The Cog In The Machine On Which All Depends