July 25th, 2014

Scala, Functional Programming and Me

A brief history of time, featuring me, Scala and 2 Open Source projects

— Matthew Fellows —

Why Scala?

Over the past few months I’ve taken a personal interest in functional programming, and I found myself drawn into Scala. Why Scala? Well, there was the hype (and gripes), but after playing with it for a bit, I realised the potential: harness the awesome power and community of the JVM, the elegance of Ruby, the benefits of FP (like immutability & concurrent programming) and the safety net of a compile-time, type-safe platform.

Whilst functional concepts like recursion and map-reduce have never really bothered me, and my experiences with Ruby has somewhat prepared me, I must admit it has been quite a learning curve: Contravariance, Currying & Monads oh my!

I felt that in order to grok Scala and its ecosystem, what better way than to create something!

Parallel threads

The Twelve Factors

In a parallel thread, at Melbourne IT we have been quietly working away to improve our shipping processes, and the Twelve Factor App principles are something that I believe are important in building the foundations of scalable, reliable and consistent distributed platforms. In the Ruby world, we have tools like Foreman and dotenv – both are great tools for rapid development and aligning to the Twelve Factor principles – but Scala is missing these.

So I set out with the simple task to create a Scala port of dotenv, and after some seriously dirty JVM hacks I have managed to create an SBT plugin that does just that.

REST easy

In the other thread, a similar transformation to RESTful, stateless services is brewing and is another particular interest of mine. Yes, there is a metric tonne of REST frameworks out there – and I am under no illusion that my framework will be the Next Big Thing – but I still decided it would be beneficial to create my own RESTful framework from first principles. This is more a learning exercise than a serious contribution to the Scala community. But hey, you never know…

That being said, until yesterday, there wasn’t a serious competitor that had all of the ingredients I wanted:

  1. CRUD ‘for free’
  2. Out-of-the-box in/out JSON validation and messaging
  3. API Metrics
  4. API Key Authentication + OAuth2.0 support
  5. Caching / Memoization strategies
  6. Be completely reactive from top-to-toe (don’t want to see any idle threads!)

I have currently implemented 1, 2, 4.1, 6 and am starting a new branch on 3 as we speak. I chose the Scalatra micro-framework as the platform to build upon, as I really like Sinatra in the Ruby world, and this project seems to have done a reasonable job of improving on in Scala.

And yes, an admission OK, I can’t really do things in parallel, but I can do so concurrently.

That was totally worth it

In doing the above, I now have a greater understanding and appreciation of the Scala OSS ecosystem – from Build (SBT, Travis-CI) to Test (ScalaTest) to Publish (Bintray, Sonatype/Central). Shipping to prod is still a TBA, but I have managed to get a basic Heroku instance running so we’ll call that a win.

More importantly, I’m starting to enjoy programming in it, and that is why I know it will only be up from here…

Mission accomplished?

The Projects

Just in case you flew by the links above, here are the 2 projects mentioned in order of appearance:

  • https://github.com/mefellows/sbt-dotenv
  • https://github.com/mefellows/respite

Feedback welcome :)