Day54: Scala & Go

Posted by csiu on April 19, 2017 | with: 100daysofcode

Today I ended up speaking with someone from Hootsuite, and they were telling me how Hootsuite uses Scala and Go. I’m aware Scala is compatible with Java, but that’s about it. In today’s blog post, I will investigate Scala and Go.

I also picked this up (excuse to show my Cotyledon tomentosa aka “Bear’s Paw” succulent ), isn’t it cute?

Scala

Scala is a general-purpose object-oriented programming language and features include:

  • support for functional programming
    • currying - See reference: What is ‘Currying’ (basically transforming a function with multiple arguments to a function of single argument)
    • type inference - the automatic deduction of the data type
    • immutability - where the state of an object cannot be modified after it is created
    • lazy evaluation - evaluate expression only when its value is needed
    • pattern matching
  • runs on a Java virtual machine
  • supporting algebraic data types, covariance and contravariance
  • support for higher-order types (but not higher-rank types), and anonymous types
  • operator overloading - where different operators have different implementations depending on their arguments
  • optional and named parameters
  • raw strings - instead of always interpreting as regular expression
  • no checked exceptions
  • uses a curly-brace syntax

According to Alexy Khrabrov (2014) Scala is “the language of choice for scalable distributed systems” and Mark Lewis (2014) further comments “you can use Scala for anything that you can use Java for”.

Dummy example

In Why should I learn Scala? by Matt Hicks (2014), he compares Java and Scala. In one example, he showed creating a new list and converting the element types is more complicated in Java:

// Java: create new list
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");

// Java: convert list of strings to list of integers
List<Integer> ints = new ArrayList<Integer>();
for (String s : list) {
    ints.add(Integer.parseInt(s));
}

than it is in Scala:

/* Do same thing in Scala */
val list = List("1", "2", "3")
val ints = list.map(s => s.toInt)

Use Scala.


Go Programming Language (“golang”)

Go is a free and open source programming language created at Google in 2007 and features include:

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

  • A compiled, statically typed language in the tradition of Algol and C
  • garbage collection - automatic memory management
  • limited structural typing
  • memory safety features
  • Communicating sequential processes (CSP) -style concurrent programming features - for describing patterns of interaction in concurrent systems

According to Serdar Yegulalp (2015), the Go language is really good for Network and Web servers (which deals a lot with concurrency) and also really good for stand-alone command-line apps or scripts (since Go is fast).

Dummy example

Go by Examples (Mark McGranaghan) has a bunch of examples showcasing the Go programming language. If we want to do the same task in Go as above, the code looks like this:

package main

import "fmt"
import "strconv"

func main() {
    var t = []string{"1", "2", "3"}
    var t2 = []int{}

    for _, i := range t {
        j, err := strconv.Atoi(i)
        if err != nil {
            panic(err)
        }
        t2 = append(t2, j)
    }
    fmt.Println(t2)
}

This looks a bit more complicated, but it’s more explicit


Scala vs Go

From the following resources, it seems Scala and Go has its own area it does best in.

However, there are some people who prefers Go over Scala for the readability in more advanced code.