In addition to the standard parts of the Scala language, you can add to your Scala programs code libraries that you or other people have written using the import
keyword.
To use the import
keyword in a Jupyter notebook, you first need to make sure that your notebook can find the library you want to import.
If the library you want to import is one of the places Jupyter notebooks look by default, you can jump straight to step 2 of this configuration, but if you want to add a repository where your notebook should look for repositories, you can use the somewhat convoluted syntax in the following cell. It may not be worth your while analyzing this syntax closely: just copy it and reuse it...
// 1. Add maven repository where we can find our libraries
val myBT = coursierapi.MavenRepository.of("https://dl.bintray.com/neelsmith/maven")
interp.repositories() ++= Seq(myBT)
In a Jupyter notebook running the almond.sh kernel, you can configure your notebook to identify a library version as int he following cell. You probably should understand the syntax of the syntax in back-ticks. The first section before the double colon identifies the group or organization responsible for the library (edu.holycross.shot
). The section between the double colon and single colon is the name of the library (pleiades
). The final section is a three-part version number.
The libraries used in this notebook follow a convention called semantic versioning. The left-most digit changes only when the programming interface (API) changes; the middle digit changes when new functionality is added without breaking the API; the right-most digit changes when bug fixes are added without adding new functionality. (For more on semantic versioning, see https://semver.org/.)
// 2. Make libraries available with `$ivy` imports:
import $ivy.`edu.holycross.shot::pleiades:1.1.0`
At this point, your notebook is fully configured, and you can use the pleiades
library the same way you would in any Scala program with the import
keyword. You can either import specifically listed classes, or, as here, use the underscore _
(or "fill-in-the-blank notation") to include all classes in the library.
import edu.holycross.shot.pleiades._
Now all the classes of the pleiades
library are imported. Here's a quick example of something you're probably glad you don't have to write from scatch yourself.
First: let's load data from the pleiades project into Scala classes.
val pleiades = PleiadesDataSource.loadFromUrls()
The output from the previous cell tells us that we created an object of the Pleiades
class.
Since I've already looked up the documentation of the Pleiades
class, I know it has a method that lets me find data in the Pleiades data set by searching for a place name.
val athens = pleiades.lookupName("Athens")
The result is a Vector of PleiadesPlace
objects that include an id nuber, a description, and possibly a geographic location.
We're sure our new library is working correctly: there is definitely not a Pleiades
class in the standard libraries of the Scala language!