The Scala Build Tool (SBT) is a command-line tool that is very popular for building Scala projects. It also provides a Scala console that can be used for exploring Scala language features and its API.
The following are the SBT installation instructions for macOS using the Homebrew tool. The SBT installation will vary from one OS to the other. For more details on SBT, please refer to the official SBT page at https://www.scala-sbt.org/index.html:
Install Homebrew first, if it is not already installed:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" # Install homebrew if not already installed
Install sbt using Homebrew, as follows:
$ brew install sbt@1
To verify that SBT and Scala are installed correctly on your machine, go through the following steps:
Run the sbt command and then run the console command inside sbt to get access to the Scala console, as follows:
$ sbt [info] Loading project definition from /Users/handsonscala/project [info] Set current project to handsonscala (in build file:/Users/handsonscala/) [info] sbt server started at local:///Users/handsonscala/.sbt/1.0/server/b6ef035291e7ae427145 /sock
sbt:handsonscala> console [info] Starting scala interpreter... Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_181). Type in expressions for evaluation. Or try :help.
scala>
Run :quit to exit the Scala console. To exit sbt, run the exit command:
scala> :quit
[success] Total time: 6 s, completed Sep 16, 2018 11:29:24 AM sbt:handsonscala> exit [info] shutting down server $
Explore Scala from SBT by performing some of the popular Scala List operations. To do this, go through the following steps:
Start sbt and get access to the Scala console, as follows:
$ sbt [info] Loading project definition from /Users/handonscala/project [info] Set current project to handsonscala (in build file:/Users/handsonscala/) [info] sbt server started at local:///Users/handsonscala/.sbt/1.0/server/b6ef035291e7ae427145/sock sbt:> console [info] Starting scala interpreter... Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_181). Type in expressions for evaluation. Or try :help.
scala>
Create a Scala List of US states using the following code:
scala> val someStates = List("NJ", "CA", "IN", "MA", "NY", "AZ", "PA") someStates: List[String] = List(NJ, CA, IN, MA, NY, AZ, PA)
Examine the size of the List as follows:
scala> someStates.size res0: Int = 7
Sort the List in ascending order as follows:
scala> someStates.sorted res1: List[String] = List(AZ, CA, IN, MA, NJ, NY, PA)
Reverse the List as follows:
scala> someStates.reverse res2: List[String] = List(PA, AZ, NY, MA, IN, CA, NJ)
Join the elements of the list using a comma (,) as a separator, as shown in the following code:
Finally, exit the Scala console and quit sbt once you are done exploring, as shown in the following code:
scala> :quit
[success] Total time: 17 s, completed Sep 16, 2018 11:22:41 AM sbt:someuser> exit [info] shutting down server
Scala's List is a powerful data structure, and we will be looking at this and several other commonly used Scala data structures more detail in the later chapters. Scala List provides a comprehensive and intuitive API that makes it very easy to work with lists. We previously explored how to construct a Scala List and got an idea of some of the commonly used List APIs. The primary objective of this exercise was to make sure that the Scala command-line tools were set up and working correctly on the local computer.