Learning Scala: A self-made course outline

I’m currently learning Scala, a functional programming language on top of the JVM.

I tend to teach things as I learn them: so here’s my 45 minute routine I go through every other day

  • (Average) 25 minute video: 1 video with Coursera’s Functional Programming Course by Martin Odersky (creator of the Scala Programming Language)
  • 20 minutes reading: 1 chapter reading Programming in Scala (ebook) by Martin Odersky, Lex Spoon and Bill Veeners.

Both materials are highly recommended in learning Scala. You can also go through Twitter’s Scala School if you want to learn in a faster pace. Once you learn the language, it’s easy to pick up Scala frameworks that help you build the things you need to build.

By this rate anyone would grasp the fundamentals in 2 ½ months. It would be nice that you also try and build something with what you’ve learned. I’ve built Aclys, it’s a tiny in-memory NoSQL key/value store on top of Finagle. It’s under 90 lines of code :)

package com.aclys.start

import java.io._
import java.net.InetSocketAddress
import java.util.Properties

import com.twitter.finagle.{Service, SimpleFilter}
import org.jboss.netty.handler.codec.http._
import org.jboss.netty.handler.codec.http.HttpVersion
import org.jboss.netty.util.CharsetUtil.UTF_8
import com.twitter.util.Future
import java.net.InetSocketAddress
import com.twitter.finagle.builder.{Server, ServerBuilder}
import com.twitter.finagle.http.Http
import scala.collection.JavaConversions._

import com.twitter.util._
import com.twitter.finagle.http._
import com.twitter.finagle.http.path._
import Method._
import Status._

class AclysService() extends Service[HttpRequest, HttpResponse] {
  var data = Map[String, String]()

  def apply(arg: HttpRequest) = Future {    
    val request = Request(arg)
    val key = request.getParam("key")   

    (request.method) match {
      case Get => get(key)       
      case Post => post(key, request.contentString)        
      case Delete => delete(key)
      case _ => stdResp(NotFound)
    }
  }  

  def get(key: String): HttpResponse = {   
    if(data.contains(key)) stdResp(Ok, data(key))
    else stdResp(NotFound)     
  }

  def post(key: String, value: String): HttpResponse = {    
    if(!key.isEmpty) {
      data += (key -> value)
      stdResp(Ok)      
    } else stdResp(NotFound)
  }

  def delete(key: String): HttpResponse = {
    if(data.contains(key)) {
      data -= (key)
      stdResp(Ok)
    } else stdResp(NotFound)     
  }

  private def stdResp(status: HttpResponseStatus) = {
    val resp = Response(HttpVersion.HTTP_1_1, status) 
    resp    
  }

  private def stdResp(status: HttpResponseStatus, value: String): HttpResponse = {
    val resp = stdResp(status)        
    resp.contentType = MediaType.WwwForm
    resp.contentString = value
    resp
  }
}

object Main {
  def main(args: Array[String]) {
    val conf = new Properties
    conf.load(new FileInputStream("config.properties"))

    val aclys: Service[HttpRequest, HttpResponse] = new AclysService

    val server = ServerBuilder()
      .codec(Http().maxRequestSize(new StorageUnit(Int.MaxValue)))
      .bindTo(new InetSocketAddress(conf("server.port").toInt))
      .name("aclys")
      .build(aclys)
  }  
}

It’s good to apply what you’ve learned, supplemented by good reading, applying what you’ve learned allows you to learn faster and deeper. So set up Scala-IDE for Eclipse. This is a solid IDE that allows you to be instantly productive with Scala.

Aside from it being extensively used at work, learning Scala helps me out a lot as a person in the software industry. As I’m approaching 4 years of being in the industry managing teams, writing code in C# and/or improving UI/UX on software products, it seems like a great time to also level up my understanding in creating better solutions to several business or technology-related problems.