Giter Site home page Giter Site logo

gordonlee / programming-scala-book-code-examples Goto Github PK

View Code? Open in Web Editor NEW

This project forked from deanwampler/programming-scala-book-code-examples

0.0 0.0 0.0 23.44 MB

The code examples used in Programming Scala, 2nd and 3rd (forthcoming) Editions (O'Reilly)

License: Other

Scala 99.00% Java 0.58% Batchfile 0.04% Shell 0.38%

programming-scala-book-code-examples's Introduction

Programming Scala, 3rd Edition (Forthcoming)

README for the Code Examples

Dean Wampler

Key Dates Description
August 11, 2014 2nd edition examples
May 27, 2019 Updated for Scala 2.12 and 2.13
June 18, 2019 New support for Maven builds, courtesy of oldbig
October 12, 2019 Updated for Scala 2.13.1, sbt 1.3.2, and other dependencies. Also now compiles with JDK 11
October 13, 2019 Renamed the repo from prog-scala-2nd-ed-code-examples to programming-scala-book-code-examples
December 31, 2019 Renamed the progscala2 package to progscala3 and reworked most of the *.sc scripts for better testability and other improvements
March 1, 2020 Completed conversion to Scala 3
March 20, 2020 Started incorporating new Scala 3 syntax, idioms

Join the chat at https://gitter.im/deanwampler/programming-scala-book-code-examples

This repo contains all the code examples to be included in Programming Scala, Third Edition. (The second edition is available here.) There are also many code files in this distribution that aren't included in the book.

When the second edition was published, the examples used Scala 2.11. The code has since been updated to also compile with Scala 2.12 and 2.13. Many examples are being improved, in part so they compile with newer library versions and with the stricter compiler flags now used. (ScalaTest, in particular, has changed a lot since the second edition.)

If you want the example code for the second edition (with a few bug fixes), download the tagged 2.1.0 build or check out the release-2.1.0 branch. The latest 2.X.Y release and release-2.X.Y branch include all the updates for 2.12 and 2.13. (No more release-2.X.Y releases are planned.) The third edition code for Scala 3 will be tagged with release-3.0.0-XYZ, with XYZ starting at 001 until the edition is published. Afterwards, release-3.X.Y will be used.

How the Code Is Used in the Book

In the book's text, when an example corresponds to a file in this distribution, the listing begins with a path in a comment with the following format:

// src/main/scala/progscala3/.../filename

And similarly for Java files (yes, there are Java files!). Following the usual conventions, tests are in src/test/....

Use these comments to find the corresponding source file. This archive also contains MUnit and ScalaCheck unit tests to validate some of the code. Most of these tests are not reproduced in the text of the book, except when discussing testing itself.

Naming Conventions

The examples include "scripts" that are run with the scala command (or within SBT using the console), source files that are compiled with scalac, source files that deliberately fail to compile to demonstrate common errors, and other example files that aren't part of the build.

To keep these different kinds of files straight and to support building with SBT, the following conventions are used for the files:

  • src/main/scala/.../*.scala - All Scala 3 source files built with SBT.
  • src/test/.../*.scala - All Scala 3 test source files built and executed with SBT.
  • src/script/.../*.scala - "Script" files that won't compile with scalac, but can be interpreted with the scala interpreter.
  • src/*/scala-2/.../*.scala - All Scala 2 source files that won't compile with Scala 3. They are not built with SBT.
  • src/extra/.../*.scala - Extra examples that aren't part of the SBT build, for example, an Apache Spark example that is not built so that the Spark dependencies aren't added to the build.

Required and Optional Tools

To build and run the examples, all you need Java 8 or newer and SBT. SBT is the de-facto standard build tool for Scala. When you run SBT, it will bootstrap itself with the correct version of its jar file, Scala, and project dependencies, which are specified in the build.sbt file in the root directory and other build files in the project directory.

Follow these installation instructions.

If you want to install Scala separately and Scala's Scaladocs, go to scala-lang.org, but this isn't required.

Editors, IntelliJ, Visual Studio Code, and Other IDEs

Most editors and IDEs now have some sort of Scala support:

For other IDEs and text editors, try Scala Metals first (I've used it with Sublime Text, for example) or ENSIME. You may also need additional, third-party tools for syntax highlighting, etc.

After installing the required plugins, load this project in your IDE, which should detect and use the SBT project automatically. For eclipse, run the sbt eclipse task to generate project files, then import them.

Building the Code Examples

After installing SBT, open a command/terminal window and run the sbt test command. By default, it now uses the dotc preview for Scala 3.

You'll see lots of output as it downloads all the dependencies, compiles the code and runs the tests. You should see [success] messages at the end.

SBT is discussed in more detail in the book and the SBT website, but a few useful commands are worth mentioning here.

If you start sbt without any arguments, it puts you into an interactive mode where you can type commands. Use control-D to exit this mode. Once at the SBT prompt (sbt:Programming Scala, Third Edition - Code examples>), try the following commands, where each # starts a comment; don't type those!

help       # help on tasks and settings
clean      # delete all build outputs
compile    # compile the source, but not test code
test       # compile source and test code, if necessary and run the tests.
~test      # continuously compile and test when source changes are saved.
console    # run the Scala REPL; dependencies and code are on the CLASSPATH
tasks      # show the most common tasks (commands).
tasks -V   # REALLY show ALL tasks

The ~ prefix causes the task to be run continuously each time source code changes are saved. This promotes continuous TDD (test-driven development) and is one of my favorite features!

Outside of SBT, you could, in principle, run the script files manually at the console/terminal prompt.

scala src/script/scala/.../Foo.scala

However, many of the scripts require other project code that has been compiled (which is in target/scala-X.Y/classes) and occasionally third-party libraries that are part of the project dependencies. Hence, it's easier to test the scripts using the SBT console task. Use the REPL's :load src/script/scala/.../Foo.scala feature to load and run the script.

Feedback

I welcome feedback on the Book and these examples. Please post comments, corrections, etc. to one of the following places:

There is also a dedicated site for the book where occasional updates, clarifications, corrections, and lame excuses will be posted: programming-scala.org.

programming-scala-book-code-examples's People

Contributors

apolunin avatar benjamintanweihao avatar chicagoscala avatar deanwampler avatar erikogan avatar gitter-badger avatar mkloeckner avatar mwkang avatar oldbig avatar sounie avatar sslavic avatar yaojingguo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.