This is a two day course. You are expected to know how to program in at least one programming language (Java, Ruby, JavaScript, etc.). The course teaches the fundamentals of using Scala as a functional programming language.
This course is meant to be run in person. There are comments in the exercises to try and point you in the right direction so you should be able to do this in your spare time if you desire. Unit tests are included to verify your solutions for each exercise.
Use ./auto/sbt test
to run the tests. The first time you run the tests, they will all fail. This is a good thing! As you complete each exercise correctly, the tests will pass.
We welcome pull requests and feedback!
Time | Topic/Exercise |
---|---|
09.00 | Start |
09.15 | Intro to FP/Scala (presentation) |
10.00 | IntroExercises |
10.30 | Intro to ADTs (presentation) |
10.50 | Morning tea |
11.00 | TypesExercises |
12.00 | Lunch (not provided) |
13.15 | ListExercises |
15.00 | Afternoon tea |
15.30 | NullExercises |
16.00 | OptionExercises pt. 1 (Safe constructors) |
17.00 | End |
Time | Topic/Exercise |
---|---|
09.15 | Intro to Error Handling (presentation) |
10.00 | OptionExercises pt. 2 |
10.50 | Morning tea |
11.00 | OptionExercises pt. 3 |
11.45 | ExceptionExercises |
12.15 | Lunch (not provided) |
13.15 | Exceptions2EitherExercises |
14.30 | Afternoon tea |
15.00 | TryExercises |
15.30 | LogParser |
16.45 | Wrap up |
17.00 | End |
At a minimum, you need:
- This repository
- Java 8 installed (even if you have a newer version)
- Docker
- Text editor (IntelliJ is recommended)
$ git clone https://github.com/wjlow/intro-to-scala.git
$ cd intro-to-scala/
Test if you have Java 8 already with java -version
.
macOS (using Homebrew)
$ brew tap caskroom/versions
$ brew cask install caskroom/versions/adoptopenjdk8
3. Install Docker
Use Docker for Mac
To run the sbt
shell using Docker:
$ ./auto/sbt
Tip: Launching SBT might take some time, so we recommend using SBT's interactive shell to run commands, instead of lauching SBT for each command.
Launch the SBT shell.
To only compile production code use:
sbt> compile
To compile production and test code use:
sbt> test:compile
To run all tests use:
sbt> test
The first time you run all the tests you will get a lot of errors! These tests will be fixed by you during the duration of the course.
In the meantime, run only a single test case at a time to keep things manageable.
To run a single test, use:
sbt> ~testOnly package.path.of.test.TestName
For example, to run only the fundamentals.level01.IntroExercisesTest test case, use:
sbt> ~testOnly fundamentals.level01.IntroExercisesTest
To run by test case name only, use:
sbt> ~testOnly *TestName
For example, to run the fundamentals.level01.IntroExercisesTest test case, use:
sbt> ~testOnly *IntroExercisesTest
The ~
watches for changes to your files and runs the command automatically. It's nice to use it to get really fast feedback as you are working on the exercises!
To stop watching changes through ~
, press Enter to return to the SBT shell prompt.
Reducing StackTraces Shown by Failed Tests
The first time you run a test case you will be greeted by a long list of StackTraces:
If you want to see all tests that pass and only the first test that fails use the LittleRed reporter with:
testOnly *TestName -- -C delight.LittleRed
If you want to see all passing and failing tests with minimal stacktraces use the Nature reporter with:
testOnly *TestName -- -C delight.Nature
For more information see Delight
To launch into a Scala REPL with all production code use:
sbt> console
Once in the console, you can import your production code as such:
import package.objectname._
For example, to use functions defined in fundamentals.level01.IntroExercises:
scala> import fundamentals.level01.IntroExercises._
scala> add(1, 2)
res0: Int = 3
To launch into a Scala REPL with all production and test code use:
sbt> test:console
Type :q
to exit from the REPL and return to SBT.
To exit the SBT shell use:
sbt> exit
IntelliJ IDEA (recommended)
-
Install and open IntelliJ
-
If running IntelliJ for the very first time, it might ask you what plugin you want to install. Select Scala, otherwise install manually: Configure -> Plugins -> Browse Repositories -> Scala
-
Restart IntelliJ to activate the plugin
-
Open IntelliJ and open this project: Open -> Select directory where project is in
-
IntelliJ will detect this as an SBT project. Select
Import SBT Project
when prompted -
In the pop-up, choose SDK -> JDK -> Java 1.8 (this step might be confusing, feel free to reach out)
-
Wait for IntelliJ to refresh the project and download dependencies (this might take a while)
-
Compile project with Cmd + F9. If you get no errors, IntelliJ setup is all done!
Tips:
-
You can run individual tests by right-clicking and then selecting Run ...ExercisesTest (or just use SBT)
-
Use Cmd + P inside the argument of a function to see what type the argument needs to be.
-
Use Ctrl + Shift + P to find out the type of a highlighted expression.
Text Editor (Vim/Sublime/Atom/Emacs)
-
Open the current directory in an editor of your choice.
-
Open the SBT shell in a terminal window.
-
Compiling - See SBT instructions on how to compile code.
-
Running Tests - See SBT instructions on how to run tests.
-
Looking up Scala API - You can also search through the Scala APIs to find any necessary methods or use a documentation browser like Dash.
-
To explore the Scala API or any of the exercises use the Scala REPL - See SBT instructions on how to jump into the REPL.