Scripts, applications, and real-world workflows
1. Scripts, applications, and real-world workflows
Welcome back! In the previous videos,2. The Scala interpreter
you saw the Scala interpreter. In the previous exercises, you actually experienced another way of running Scala code:3. Scala scripts
scripts. A script is a sequence of instructions in a file that are executed sequentially. Scripts are great for smaller tasks that can fit into a single file, like sending a templated email from the command line, for example. When you clicked "Run Code" and "Submit Answer", you actually ran a Scala script! Here's how scripts work. At a command prompt, the scala command (which is the same program as the interpreter, hence the same name) executes a script by wrapping it in a template, then compiling and executing the resulting program.4. Scala scripts
If we write this in a file named game dot scala then go to a command prompt and type scala game dot scala, "Let's play Twenty-One!" is printed to output. The double forward slash is a comment -- any characters between that and the next end of line are ignored.5. Interpreted language vs. compiled language
Before we get into Scala applications, let's discuss Scala as a compiled language. Here are the definitions of an interpreter and a compiler. Pause the video and take a second to read them. The Scala interpreter "hides" the compilation step from you, the programmer, and it appears that the code is run immediately. Same thing for scripts.6. Scala applications
Scala APPLICATIONS, on the other hand, must be compiled explicitly then run explicitly by you. So two steps. Applications consist of many source files, compiled individually. They're great for more complex projects. A key benefit over scripts is that there is no lag time since applications can be precompiled, whereas scripts are compiled and executed every time.7. Scala applications
The equivalent code as our script example looks like this: ignore the object Game extends App part for now. Step 1: compile. The Scala compiler, which is named scalac, translates the code you wrote to Java bytecode. Step 2: run. That's when that compiled code is executed on a Java virtual machine.8. Scala applications
Same printed output.9. Pros and cons of compiled languages
The main benefit of compiled languages is execution speed since code doesn't need to be interpreted on the fly. The drawback: it takes time to compile code, sometimes minutes for large Scala programs. This trade-off is usually worth it, which is why companies are switching to Scala since milliseconds of load times can mean millions in added revenue.10. Scala workflows
Thus far, you've seen Scala in the command line. The second main way people work in Scala is using an IDE, an integrated development environment.11. IDE
IDEs shine when programmers need to build or work within large-scale systems. IntelliJ IDEA with the Scala plugin is the most common setup for Scala professionals.12. IntelliJ IDEA
Here's IntelliJ IDEA. Top left quadrant: file explorer. Top right quadrant: source files, like scripts, something called a worksheet, and build files (more on build files shortly). Then the bottom part has has windows for inspecting the execution of your program. In Scala courses on DataCamp, a similar IDE experience is recreated.13. sbt
Now, build files. The most popular tool for building Scala apps is called sbt, "simple build tool". sbt lets you compile, run, and test your Scala applications. There's a dot sbt file in this image. Though you won't use sbt in this course, being aware will help frame your learning in real-world workflows and set you up for future success.14. Scala kernel for Jupyter
Scala also works in Jupyter Notebooks with a Scala kernel called Almond. With this setup, the data scientist can create plots to visualize their data, do big data analysis using Apache Spark, and more. Take a peek at the animated visualizations and analyses on the Almond website linked on this slide. They're cool.15. Let's practice!
Nice work! Let's strengthen these concepts in your mind with exercises.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.