官术网_书友最值得收藏!

Installing command-line tools

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:

  1. 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
  1. 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:

  1. 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>
  1. 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:

  1. 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>
  1. 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)
  1. Examine the size of the List as follows:
scala> someStates.size
res0: Int = 7
  1. Sort the List in ascending order as follows:
scala> someStates.sorted
res1: List[String] = List(AZ, CA, IN, MA, NJ, NY, PA)
  1. Reverse the List as follows:
scala> someStates.reverse
res2: List[String] = List(PA, AZ, NY, MA, IN, CA, NJ)
  1. Join the elements of the list using a comma (,) as a separator, as shown in the following code:
scala> someStates.mkString(",")
res3: String = NJ,CA,IN,MA,NY,AZ,PA
  1. Perform sort and join operations as a chain, as shown in the following code:
scala> someStates.sorted.mkString(",")
res4: String = AZ,CA,IN,MA,NJ,NY,PA
  1. 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.

主站蜘蛛池模板: 临澧县| 康保县| 梧州市| 盘山县| 无锡市| 育儿| 古丈县| 新竹县| 德州市| 日喀则市| 株洲市| 拜泉县| 合川市| 康保县| 海门市| 平湖市| 从化市| 元氏县| 建德市| 祁阳县| 柞水县| 科技| 维西| 阿尔山市| 广饶县| 来凤县| 新兴县| 白玉县| 自贡市| 平利县| 武城县| 永泰县| 黄梅县| 兰考县| 四会市| 高唐县| 壶关县| 清水河县| 津南区| 临洮县| 广东省|