Small things in Scala
Your choice of programming language most likely won’t guarantee or preclude success on your next project. But using a language that facilitates fluent expression of your thoughts and feels good to work with can be a positive influence on how efficiently you meet the needs of your customers.
Allow me to show you some of the small, but discerning, touches I’ve found in my first few months with Scala. I’ll leave you to contemplate how they might add-up to productivity on your next project.Very low ceremony
Scala’s perspective is to remove as much noise as possible so that reading code is a smooth experience.
For instance, functions and classes are public by default (else this REPL snippet would error)…..
https://gist.github.com/anonymous/5462043.js
…parentheses and dots are optional for invocation of single or zero argument functions (focus on line 20+)….
https://gist.github.com/NTCoding/5462307.js
…optional type inference for functions and optional return keyword (the last, in this case only, line of the function is the return value)….
https://gist.github.com/NTCoding/5462322.js
…and I really love underscore variables in lambdas — small touch but brilliant (c# you need this!). Lines 4 and 7 are equivalent….
https://gist.github.com/NTCoding/5462360.jsInfix & postfix operations and symbolic function names
We saw how infix operations look nice and clean in the Porsche example above by letting you omit parentheses. It might also read more fluently, and less noisy, in some cases if we could put the arguments before the method e.g. {argument} {method} {object} — the reverse of normal (line 19+).
https://gist.github.com/NTCoding/5468406.js
You can see above how adding items to the plate using postfix operators (the “>>:” function which is a member of the plate instance — e.g. plate.>>:(“tomatoes”)) and symbolic function names looks really clean and fluent. This isn’t a gimmick, but these features should be used with caution.
One example that really impresses is rapture IO that puts well-known symbols to effective use by mimicking the shell.Everything is an expression
In Erlang, everything (or almost everything) is an expression. I really like that feature, and I’m delighted Scala has it too.
It means that you can make code even more readable. As an example, your if/else statements return the value from the if body or the value from the else body — it’s not just for control flow. Have a look…
https://gist.github.com/anonymous/5468423.jsFor-comprehensions and generators
I’m going to…
- declare a function
- iterate over two arrays.
- perform a computation for each item in the first array with each item in the second array
- return the results as a new collection
I’m going to do all of that on one line of code that is as readable as most you will see you in your career.
https://gist.github.com/anonymous/5468445.jsScala won’t make you 20 years younger…
… but it has definitely given me further insight about how programming languages allow me to express my thoughts, and put a few small question marks in my head about the technologies I want to be working with on a daily basis.
Keep in mind this is just the small touches — I haven’t talked about bigger features like pattern matching, some of the much-lauded frameworks like Akka, or the vibrant community Scala has.
As I said to begin with, choice of programming language isn’t the defining factor in the success of a project. Nor am I saying C#, Java, or any other language suddenly are obsolete. Keep learning and keep applying critical evaluation is my philosophy — and have fun with it.