programming

  • Scala inherited the concept of extensions from Java. However the syntax is much clearer then its predecessor counterpart:

    val url = new URL("http://www.strumentiresistenti.org/nonSuchFile.html")
    try {
     ... something...
    } catch {
      case _: MalformedURLException => println("Where did you send me?")
      case ex: IOException => ex.printStackTrace()
    }

    This example is taken with some minor modifications from the wonderful book written by Cay S. Horstmann "Scala for the impatient". This book is such a wonderful introduction to Scala that Martin Odersky (Scala creator) asked Horstmann to freely release the first chapters to speed up Scala's adoption. I'm reading it and I really recommend it to everyone.

  • 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>) {... }
  • One of the very first data structures used in every language is the array. Scala provides two types of arrays, implemented using the JVM facilities: the Array class and the ArrayBuffer class. The first one is used when the length of the array is known in advance, while the second one provides a dynamically growing type of array.

    Here I'm going to declare a new Array:

    scala> val ary = Array[Int](1, 2, 3)
    ary: Array[Int] = Array(1, 2, 3)
  • One of the fundamental tools of the functional programmer toolbox is the concept of recursion. It can be defined briefly as calling a function from inside itself. Recursive functions exist in many languages, even outside the domain of FP. To name one of the oldest, the C language allows recursive calls. Recursive functions are a cleaner and more elegant way to express problems without using loops. The only caveat is never forget to check for a termination condition to avoid endless looping. Let's see how the factorial of a number can be calculated in Scala with a naif recursive call:

    scala> def f(x: Int): Int = if (x == 1) 1 else x * f(x - 1)
    f: (x: Int)Int

    scala> f(2)
    res0: Int = 2

    scala> f(3)
    res1: Int = 6

    scala> f(4)
    res2: Int = 24
  • 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!

  • Sometimes Scala can cause you an excruciating confusion, even with the simplest constructs. Would you be able to answer the title question? Why Array(100) is not a new Array(100)?

    I've already scraped the surface of companion objectsin another post. Companion objects happen to be the reason of the difference between Array(100) and new Array(100). The first translates into a call to Array.apply(100), which is a call to the apply() method of the object Array, the companion of the Array class. As is customary to Scala, the apply() method of a companion object instantiates its class. However the set of parameters is not forced to be the same in length, types of meaning. Actually it happens that the apply() method takes a variable-length list of object to be put inside the newly createArray instance. So:

    scala> val a1 = Array(100)
    a1: Array[Int] = Array(100)

    is equivalent to:

    scala> val a2 = new Array[Int](1)
    a2: Array[Int] = Array(0)

    scala> a2(0) = 100

    scala> a2
    res1: Array[Int] = Array(100)

    Much more compact and quick. The real comfort provided by the companion object' apply() method is:

    scala> val a3 = Array(Array(10,20), Array(30,40))
    a3: Array[Array[Int]] = Array(Array(10, 20), Array(30, 40))

     


  The Cog In The Machine On Which All Depends