Day78: Installing Scala

Posted by csiu on May 13, 2017 | with: 100daysofcode

Changing gears, I want to learn Scala.


What is Scala?

Notes from Martin Odersky’s “What is Scala?”

  • Scala is an acronym for “Scalable Language
  • Scala is object-oriented, where every value is an object and every operation is a method-call
  • Scala is a functional language, containing first-class functions, a library with efficient immutable data structures, and a general preference of immutability over mutation.

In computer science, functional programming is a programming paradigm – a style of building the structure and elements of computer programs – that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

  • Scala is seamless with Java e.g. running on the JVM and freely mixing Java and Scala classes
    • Build tools: ant or maven
    • IDEs: Eclipse, IntelliJ, or Netbeans
    • Frameworks: Spring, Hibernate, Akka, Finagle, the Play web framework

A Java virtual machine (JVM) is an abstract computing machine that enables a computer to run a Java program

According to Jaimin Patel (2015) of StackOverflow:

  • Functions are objects in Scala
  • Scala is future-proof (e.g. scalable, parallelizable over multiple cores, able to distribute computing over the cloud)
  • Scala is fun (we shall see)


Scala installation

The easiest way to install Scala is using Homebrew (a package manager for macOS).

brew install scala
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 3 taps (caskroom/cask, homebrew/core, homebrew/science).
==> Updated Formulae
aws-sdk-cpp                   homebrew/science/ncview       planck
cppcheck                      homebrew/science/openimageio  plantuml
freetype ✔                    homebrew/science/qrupdate     scw
gexiv2                        homebrew/science/scram        snapraid
homebrew/science/cdo          homebrew/science/swrcfit      sonar-scanner
homebrew/science/cmor         imagemagick ✔                 source-to-image
homebrew/science/fgsl         imagemagick@6                 sourcekitten
homebrew/science/gmt          juise                         terminal-notifier
homebrew/science/igv          kafkacat                      weechat
homebrew/science/igvtools     libslax                       zsh-completions
homebrew/science/libbi        lumo

==> Downloading https://homebrew.bintray.com/bottles/scala-2.12.2.el_capitan.bottle.tar.
######################################################################## 100.0%
==> Pouring scala-2.12.2.el_capitan.bottle.tar.gz
==> Caveats
To use with IntelliJ, set the Scala home to:
  /usr/local/opt/scala/idea

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
🍺  /usr/local/Cellar/scala/2.12.2: 44 files, 19.9MB


Scala REPL

The documentation for the Scala standard library is here.

A Read–Eval–Print Loop (REPL) … is a simple, interactive computer programming environment that takes single user inputs (i.e. single expressions), evaluates them, and returns the result to the user

In the following, I work through the exercises of Chapter 1: The Basics of “Scala for the Impatient” by Cay Horstmann.

Welcome to Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_20).
Type in expressions for evaluation. Or try :help.

// What's there
scala> 3.
!=   >             floatValue      isValidInt     to               toRadians
%    >=            floor           isValidLong    toBinaryString   toShort
&    >>            getClass        isValidShort   toByte           unary_+
*    >>>           intValue        isWhole        toChar           unary_-
+    ^             isInfinite      longValue      toDegrees        unary_~
-    abs           isInfinity      max            toDouble         underlying
/    byteValue     isNaN           min            toFloat          until
<    ceil          isNegInfinity   round          toHexString      |
<<   compare       isPosInfinity   self           toInt
<=   compareTo     isValidByte     shortValue     toLong
==   doubleValue   isValidChar     signum         toOctalString

scala> 3
res0: Int = 3

// Some math
scala> math.sqrt(3)
res1: Double = 1.7320508075688772

scala> res1 * res1
res2: Double = 2.9999999999999996

// You cannot change a 'val' (but you can change a 'var')
scala> res2 = "new value"
<console>:12: error: reassignment to val
       res2 = "new value"
            ^

scala> "carzy" * 3
res3: String = carzycarzycarzy

scala> 10 max 2
res4: Int = 10

scala> 2^1024
res5: Int = 1026

// Importing libraries
scala> import BigInt._
import BigInt._

scala> import util._
import util._

// Using imported libraries to generate random numbers & strings
scala> probablePrime(100, Random)
res6: scala.math.BigInt = 931214232309913689243681745999

scala> probablePrime(100, Random).toString(36)
res7: String = 36f0q6212ljn9groeish

// Subsetting a string
scala> val s = "some string"
s: String = some string

scala> s(0)
res8: Char = s

scala> s.last
res9: Char = g

scala> s.take(2)
res10: String = so

scala> s.drop(2)
res11: String = me string

scala> s.takeRight(3)
res12: String = ing

scala> s.dropRight(3)
res13: String = some str