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

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.

主站蜘蛛池模板: 沧源| 仪征市| 平陆县| 榆社县| 麟游县| 舞阳县| 汪清县| 平原县| 毕节市| 昌江| 阜阳市| 齐河县| 射洪县| 克拉玛依市| 五峰| 龙岩市| 开江县| 清涧县| 临夏市| 区。| 额敏县| 滦南县| 新和县| 察哈| 庆安县| 新闻| 汽车| 临夏县| 全州县| 肥东县| 健康| 牟定县| 蚌埠市| 大港区| 南充市| 山东省| 唐海县| 丰顺县| 正蓝旗| 修武县| 津南区|