Notes on Scala

I’m taking the the Signature Track of the “Functional Programming in Scala” Coursera class and having an inordinate amount of fun. Not that I have a large need to write Scala, but I am enjoying the challenge of the material. Getting a more formal basis to my functional programming knowledge is not a bad thing.

One facet of Scala is that of “syntax is a library.” This is a powerful notion that only occurs in very high level languages such as Lisp, Haskell or Factor/Forth. Exposing the parser to the programmer is both unusual and powerful. Most languages do not support this, and this level of power is not often needed. Allowing extension of the parser, however, leads to very concise and expressive programs. One good example of this is allowing the embedding of XML parse trees directly in the program as a syntax construct. This works well — given a start sentinel, the library hands parsing of the data up to the end sentinel off to an XML parser whose behavior is well defined. Scala and Haskell also have out of the box support for simple unary and binary operators simply made up of words that are all symbols or a mix of symbols and letters beginning with a symbol.

One surprising thing that I have not encountered before is “call by name” as a first class feature of the language. This is the so-called “lazy evaluation” also present in Haskell. When a parameter to a function call requests “call by name” semantics then it is not evaluated until that value is needed in the computation. Contrast this with “call by value,” the more common paradigm, wherein function arguments are evaluated before expansion of the function. Such “lazy” constructs are available in primarily iimperative languages such as PHP and python through special object oriented systems which mock or imitate this deferred evaluation pattern. Two examples off the top of my head: Laravel’s Eloquent ORM, and Django’s Manager metaclass pattern. Both of these allow construction of complex queries whose results are not calculated until asked for. Queries are built by implicit composition of function-like objects by chaining calls to the return values.