46,44 €
Discover unique features and powerful capabilities of Scala Programming as you build projects in a wide range of domains
Key Features
Book Description
Scala is a type-safe JVM language that incorporates object-oriented and functional programming (OOP and FP) aspects. This book gets you started with essentials of software development by guiding you through various aspects of Scala programming, helping you bridge the gap between learning and implementing. You will learn about the unique features of Scala through diverse applications and experience simple yet powerful approaches for software development.
Scala Programming Projects will help you build a number of applications, beginning with simple projects, such as a financial independence calculator, and advancing to other projects, such as a shopping application and a Bitcoin transaction analyzer. You will be able to use various Scala features, such as its OOP and FP capabilities, and learn how to write concise, reactive, and concurrent applications in a type-safe manner. You will also learn how to use top-notch libraries such as Akka and Play and integrate Scala apps with Kafka, Spark, and Zeppelin, along with deploying applications on a cloud platform.
By the end of the book, you will not only know the ins and outs of Scala, but you will also be able to apply it to solve a variety of real-world problems
What you will learn
Who this book is for
If you are an amateur programmer who wishes to learn how to use Scala, this book is for you. Knowledge of Java will be beneficial, but not necessary, to understand the concepts covered in this book.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 460
Veröffentlichungsjahr: 2018
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Aaron LazarAcquisition Editor: Alok DhuriContent Development Editor: Zeeyan PinheiroTechnical Editor: Gaurav GalaCopy Editor: Safis EditingProject Coordinator: Vaidehi SawantProofreader: Safis EditingIndexer: Tejal Daruwale SoniGraphics: Alishon MendonsaProduction Coordinator: Nilesh Mohite
First published: September 2018
Production reference: 1280918
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78839-764-3
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Mikaël Valot is Principal Software Engineer at IHS Markit in London, UK. He is the lead developer of a strategic market risk solution for banking regulation.
He has over 15 years of experience in the financial industry of the UK, Switzerland, and France. He has a Diplôme d'Ingénieur in Computing (equivalent to an M.Sc.) from Telecom Nancy, France.
After years of working with Java, he started developing professionally with Scala in 2010, and never looked back. He was a speaker at Scala Exchange 2015.
When he is not coding in Scala, Mikaël likes to dabble with Haskell, the Robotic Operating System, and deep learning.
He strongly believes that functional programming with strong typing skills is the best way to write safe and scalable programs.
Nicolas Jorand is a senior developer. He worked for the finance industry for about 15 years before switching to the energy industry. He is a freelancer enjoying a partial time at Romande Energy, a Swiss utility company providing exclusively green electricity. Nicolas is a full-stack developer, playing with microcontrollers, developing standard web user and 3D interfaces on Unity, developing software to animate a humanoid robot (Nao) and finally, working with Scala on integration and backend software. All these projects are done with the same leitmotif; "In the dev process, get the issues as early as possible."
Vlad Patryshev is currently working as a Lead Data Engineer at Salesforce; he is also a professor of Logic at Santa Clara University.
He is an organizer of two meetups, Bay Area Categories and Types, and Scala Bay, the latter dedicated to Scala language.
Vlad's education is in math, but he spent most of his life working in software. 7 years at Borland, 3.5 years at Google.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Scala Programming Projects
Packt Upsell
Why subscribe?
Packt.com
Contributors
About the authors
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Writing Your First Program
Setting up your environment
Installing the Java SDK
Installing IntelliJ IDEA
Using the basic features
SBT synchronization
Build
Running the Scala Console
Using the Scala Console and Worksheet
Using the Scala Console
Declaring variables
Types
Declaring and calling functions
Side effects
If...else expression
Class
Using the worksheet
Class inheritance
Subclass assignment
Overriding methods
Abstract class
Trait
Scala class hierarchy
Case class
Companion object
Creating my first project
Creating the project
Creating the Main object
Writing the first unit test
Implementing another feature
Implementing the Main method
Summary
Developing a Retirement Calculator
Project overview
Calculating the future capital
Writing a unit test for the accumulation phase
Implementing futureCapital
Refactoring the production code
Writing a test for the decumulation phase
Simulating a retirement plan
Writing a failing unit test
Working with tuples
Implementing simulatePlan
Calculating when you can retire
Writing a failing test for nbOfMonthsSaving
Writing the function body
Understanding tail-recursion
Ensuring termination
Using market rates
Defining an algebraic data type
Filtering returns for a specific period
Pattern matching
Refactoring simulatePlan
Loading market data
Writing the unit test using the column selection mode
Loading the file with Source
Loading inflation data
Computing real returns
Packaging the application
Creating the App object
Packaging the application
Summary
Handling Errors
Setup
Using exceptions
Throwing exceptions
Catching exceptions
Using the finally block
Ensuring referential transparency
Defining pure functions
Best practices
Showing how exceptions break referential transparency
Using Option
Manipulating instances of Option
Composing transformations with for... yield
Refactoring the retirement calculator to use Option
Using Either
Manipulating Either
Refactoring the retirement calculator to use Either
Refactoring nbOfMonthsSavings
Refactoring monthlyRate
Refactoring simulatePlan
Refactoring SimulatePlanApp
Using ValidatedNel
Adding the cats dependency
Introducing NonEmptyList
Introducing Validated
Refactoring the retirement calculator to use ValidatedNel
Adding unit tests
Implementing parsing functions
Implementing SimulatePlanApp.strSimulatePlan
Refactoring SimulatePlanApp.strMain
Summary
Questions
Further reading
Advanced Features
Project setup
Strictness and laziness
Strict val
lazy val
By-name parameters
Lazy data structures
Covariance and contravariance
InvariantDecoder
CovariantDecoder
Contravariant encoder
Covariance in collections
Currying and partially applied functions
Function value
Currying
Partially applied functions
Implicits
Implicit parameters
Implicit parameter usage
Passing a timeout
Passing an application context
Examples in the SDK
breakOut
executionContext
Implicit conversion
Implicit class
How are implicits resolved?
Summary
Type Classes
Understanding type classes
Type class recipe
Common type classes
scala.math.Ordering
org.scalactic.Equality
cats.Semigroup
Laws
Usage examples
cats.Monoid
Laws
Usage examples
Higher-kinded types
Arity
Higher-order function
Higher-kinded types
cats.Functor
Laws
Usage examples
cats.Apply
Laws
Usage examples
cats.Applicative
Laws
Usage examples
cats.Monad
Laws
Usage examples
Summary
Online Shopping - Persistence
Creating the project
Persistence
Setting up Slick
Setting up the database
Database test
Product test
Cart test
Future
Getting a concrete value
Waiting on a Future
Callback
Composing Futures
Using for-comprehension
Execution context
Rounding up Futures
Database creation
Data Access Object creation
Running the test
Deploying the application
Setting up an account
Deploying your application
Heroku configuration
Summary
Online Shopping - REST API
The REST API
Writing the tests
Defining the routes
Running the test
Checking the API
Implementing the API with persistence
Completing the product tests
Implementing the product API
Product list
Encoding JSON with Circe
Action definition 
Adding a product
Completing the cart test
Logging in
Unit test
Implementation
Passing the cookie
Listing products in cart
Swagger
Installing Swagger
Declaring endpoints
Running the application
Login
List of products
Cart endpoints
Deploying on Heroku
Summary
Online Shopping - User Interface
Learning objectives
Setting up
Defining the layout
Creating the layout
Building the page
Main layout
Product list panel
Cart panel
Introducing the UI manager
Adding jQuery to our project
Calling our API
Setting the main method
Initializing the user interface
Implementing UI actions
Adding a product to the cart
Removing a product from the cart
Updating the quantity
Deploying the user interface
Debugging the interface
Summary
Interactive Browser
Actors
Setting up
Implementing the server side
Creating the web socket route
Implementing BrowserManager
Handling WebSocket
A diagram of Actors
Implementing the client side
Adding the web socket
Notifying the user
Extending jQuery
Summary
Fetching and Persisting Bitcoin Market Data
Setting up the project
Understanding Apache Spark
RDD, DataFrame, and Dataset
Spark SQL
Dataframe
Dataset
Exploring the Spark API with the Scala console
Transforming rows using map
Transforming rows using select
Execution model
Implementing the transaction batch producer
Calling the Bitstamp REST API
Parsing the JSON response
Unit testing jsonToHttpTransaction
Implementing jsonToHttpTransaction
Unit testing httpToDomainTransactions
Implementing httpToDomainTransactions
Saving transactions
Introducing the Parquet format
Writing transactions in Parquet
Using the IO Monad
Putting it all together
Testing processOneBatch
Implementing processOneBatch
Implementing processRepeatedly
Implementing BatchProducerApp
Running the application with spark-submit
Installing Apache Spark
Packaging the assembly JAR
Running spark-submit
Summary
Batch and Streaming Analytics
Introduction to Zeppelin
Installing Zeppelin
Starting Zeppelin
Testing Zeppelin
Structure of a notebook
Writing a paragraph
Drawing charts
Analyzing transactions with Zeppelin
Drawing our first chart
Drawing more charts
Introducing Apache Kafka
Topics, partitions, and offsets
Producing data into Kafka
Consuming data from Kafka
Consumer group
Offset management
Connecting to Kafka
Streaming transactions to Kafka
Subscribing with Pusher
Deserializing live transactions
Converting to transaction and serializing
Putting it all together
Running StreamingProducerApp
Introducing Spark Streaming
Analyzing streaming transactions with Zeppelin
Reading transactions from Kafka
Writing to an in-memory sink
Drawing a scatter chart
Aggregating streaming transactions
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think
Scala is a type-safe JVM language that incorporates object-oriented programming (OOP) and functional programming (FP) aspects. This book gets you started with the essentials of software development by guiding you through the different aspects of Scala programming, helping you bridge the gap between learning and implementing. You will learn about the unique features of Scala through diverse applications and encounter simple yet powerful approaches for software development. You will see how to use the basic tools, set up the environment, and write Scala programs.
Scala Programming Projects will help you build a number of applications, beginning with a simple project, such as a financial independence calculator, and advancing to other projects, such as a shopping application or a Bitcoin transaction analyzer. You will be able to use various Scala features, such as its OOP and FP capabilities, and learn ways to write concise, reactive, and concurrent applications in a type-safe manner. You will also learn how to use top-notch libraries, such as Akka and Play, and integrate Scala applications with Kafka, Spark, and Zeppelin; plus, you'll explore deploying applications on cloud platforms.
By the end of the book, you will be empowered by knowing the ins and outs of Scala, being able to apply Scala to solve a variety of real-world problems.
If you are an amateur programmer who wishes to learn how to use Scala, this book is for you. Knowledge of Java would be beneficial for, but is not necessary to, understanding the concepts covered in this book.
Chapter 1, Writing Your First Program, explains how to set up your environment to start programming in Scala, covering the basic tools that are required and what the simplest Scala application might look like.
Chapter 2, Developing a Retirement Calculator, puts into practice the features of the Scala language seen in the first chapter. We will also introduce other elements of the Scala language and SDK to develop the model and logic for a retirement calculator. This calculator will help people work out how long they'll need to save and how much they'll need to save to have a comfortable retirement.
Chapter 3, Handling Errors, has you continuing to work on the retirement calculator from the previous chapter, handling the errors that riddle it. For instance, the calculator works correctly as long as the right arguments are passed to it but fails badly with a horrible stack trace if any of the parameters are wrong.
Chapter 4, Advanced Features, explores the more advanced features of Scala. As with any programming language, some advanced constructs might be seldom used in practice or can obfuscate code. We will aim to only explain features that we have encountered in real projects that have been deployed to production.
Chapter 5, Type Classes, you will learn what a type class is, what are the most common type classes in the cats library, and how to use them in your projects.
Chapter 6, Online Shopping – Persistence, explains how to persist data in a relational database. The data will be the contents of a cart for a shopping website.
Chapter 7, Online Shopping – REST API, covers how to develop a REST API using Play Framework. API is an acronym for Application Programming Interface, and the acronym REST stands for Representational State Transfer. Basically, we will provide an interface for our application, so that other programs can interact with it.
Chapter 8, Online Shopping – User Interface, gets you using Scala.js to build the user interface for the online shopping application. In this interface, you will be able to select a product to add to your cart, update the number of products that you wish to buy, and remove them from the cart if needed.
Chapter 9, Interactive Browser, introduces the actor model by extending our shopping project. The extension will consist of a notification, provided to anyone connected to the website, about who is adding/removing a product to/from the cart.
Chapter 10, Fetching and Persisting Bitcoin Market Data, look at developing a data pipeline to fetch, store, and analyze Bitcoin transaction data. We will use Apache Spark in batch mode to do so.
Chapter 11, Batch and Streaming Analytics, focuses on how to use Zeppelin and Spark to query our historical data store. We will use Zeppelin's capability to plot interesting graphs and Apache Kafka with Spark streaming to analyze live transactions.
Basic knowledge of any programming language would be helpful to you during the course of this book. Additional knowledge of Java would also be useful for understanding some concepts covered in this book. There are several levels of involvement to make the most of this book, from the quickest to the most effective:
You can just read it and look at the code in your IDE.
While you read it, you can copy and paste the code samples in your IDE and run them.
Same as before, but this time you re-type all the code samples. Using the auto-completion will make you discover more functions of the API. Typing the code will also make you remember it more. You make use of your visual and kinesthetic memory.
The
Benjamin Franklin
method. You read a whole chapter or a section in one go, then close the book. After that, try to re-write the code samples from memory. If you are stuck you can reopen the book. This will force your brain to have a complete picture of a project. You will memorize and understand the concepts in much more depth.
You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packt.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Scala-Programming-Projects. In case there is an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/9781788397643_ColorImages.pdf.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
In 2001, Martin Odersky started to design the Scala language – it took him three years to release the first public version. The name comes from Scalable language. This was chosen because Scala is designed to grow with the requirements of its users – you can use Scala for small scripts or for large enterprise applications.
Scala has been constantly evolving ever since, with a growing popularity. As a general purpose language, it is used in many different industries such as finance, telecoms, retail, and media. It is particularly compelling in distributed scalable systems and big data processing. Many leading open source software projects have been developed in Scala, such as Apache Spark, Apache Kafka, Finagle (by Twitter), and Akka. A large number of companies use Scala in production, such as Morgan Stanley, Barclays, Twitter, LinkedIn, The Guardian, and Sony.
Scala is not an extension of Java but is fully interoperable with it. You can call Java code from Scala, and you can call Scala code from Java. There is also a compiler to JavaScript, which we will explore later on in this book. You can, therefore, run Scala code in your browser.
Scala is a blend of object-oriented and functional programming paradigms, and it is statically typed. As such, it can serve as a bridge for people from an object-oriented or imperative background to move gradually to functional programming.
In this chapter, we will cover the following topics:
Setting up your environment
Using the basic features
Running the Scala Console
Using the Scala Console and Worksheet
Creating my first project
First things first, we need to set up our work environment. In this section, we will get all the tools and libraries, and then install and configure them on your computer.
Scala programs are compiled to Java bytecode, which is a kind of assembly language that can be executed using a Java Virtual Machine (JVM). You will, therefore, need to have a Java compiler and a JVM installed on your computer. The Java Development Kit (JDK) provides both components, alongside other tools.
You could develop in Scala using a simple text editor and compile your programs using the Scala Simple Build Tool (SBT). However, this would not be a pleasant nor productive experience. The majority of professional Scala developers use an Integrated Development Environment (IDE), which provides many helpful features such as syntax highlighting, autocompletion, code navigation, integration with SBT, and many more. The most widely used IDE for Scala is IntelliJ Idea from JetBrains, and this is the one we are going to install and use in this book. The other options are Scala IDE for Eclipse and ENSIME. ENSIME is an open source project that brings IDE-like features to popular text editors such as Emacs, Vim, Atom, Sublime, and VSC.
We are going to install the Oracle JDK, which includes a JVM and a Java compiler. On many Linux distributions, the open source OpenJDK is preinstalled. OpenJDK is fully compatible with the Oracle JDK, so if you already have it you do not need to install anything else to follow this book.
You might already have a Java SDK installed on your computer. We are going to check if this is the case. If you are using Windows, open a DOS Command Prompt. If you are using macOS or Linux, open a Terminal. After the prompt, type the following:
javac -version
If you have a JDK installed, the version of the installed compiler will be printed:
javac
1.8.0_112
If the version installed is greater than or equal to 1.8.0_112, you can skip the JDK installation. The version of Scala that we are going to use is compatible with JDK version 1.8 or 1.9.
If not, open the following URL, download the SDK for your platform, and follow the installation instructions given: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.
Go to https://www.jetbrains.com/idea/download. Download the community edition for your platform. The ultimate edition offers more features, but we will not use them in this book.
The following are the steps to install IntelliJ IDEA:
Run IntelliJ Idea.
Select the
Do not import settings
option:
Choose a UI theme. I personally prefer Dracula, since a dark background saves battery on a laptop and is more gentle on the eyes:
Create a desktop entry by checking the options given:
In the
Create Launcher Script
dialog window, check the create a script... checkbox. It will let you open files in IntelliJ from the command line:
Customize the plugins. For each component, click on
Customize...
or
Disable All
. We will not need most of the plugins. You can only select the following:
Build Tools
:
Disable All.
Version Controls
: Only keep Git and GitHub.
Test Tools
:
Disable All.
Swing
:
Disable.
Android
:
Disable.
Other Tools
:
Disable All
and keep Bytecode viewer, Terminal, and YAML.
Plugin Development
:
Disable.
You can see the aforementioned plugins in the following screenshot:
Install the featured plugins—
s
ome additional plugins are proposed for you to install, such as the Scala plugin and a tool to learn the essential features of IntelliJ.
Click on the
Install
button for
Scala
and for the
IDE Features Trainer,
as shown in the following screenshot, and then proceed
by clicking on
Start using IntelliJ IDEA
:
Click on
Create New
Project
|
Scala
|
sbt
:
Fill in the following details, as shown in the following screenshot:
Name
:
scala_fundamentals
.
JDK
: Click on
New
and then select the installation directory of the Oracle JDK.
sbt
: Choose the version
1.0.4
, check
Sources.
Scala
: Choose the latest version 2.12.x, for instance
2.12.4
(IntelliJ lists all the possible versions and will download the one you choose), and check
Sources.
Click on
Finish
.
It is going to take some time depending on your internet connection's speed:
You should see the following project structure:
In this section, and in the rest of this book, we will highlight some key shortcuts in italics. I strongly encourage you to use and remember these shortcuts. They save a tremendous amount of time and will keep you focused on the task at hand. If you cannot remember a shortcut, you can use the mother of all shortcuts, Ctrl + Shift + A (Windows/Linux) or cmd + shift + A (macOS), and type the name of the action you are looking for.
If you are using IntelliJ for the first time, I find it useful to display all tool buttons. Go to the View menu, and check Toolbar and Tool buttons.
Now, let's have a look at our build configuration. SBT (short for Simple Build Tool) is the de facto build tool in the Scala community. Double-click on build.sbt:
name := 'scala_fundamentals"version := "0.1"scalaVersion := "2.12.4"
This file describes how SBT will compile, test, and deploy our project. For now, it is fairly simple.
One important thing to keep in mind is that IntelliJ manages its own set of files to define a project structure. They are located in the .idea directory of your project. Whenever you change build.sbt, IntelliJ has to interpret the changes and translate them.
For instance, If I change the Scala version to 2.12.3 and save (Ctrl + S or cmd + S), IntelliJ will propose to synchronize the changes or enable autoimport:
On a small project, it is ok to use autoimport, but on a large one, it can be a bit annoying. The synchronization can take time and it might kick off too often.
When you program in Scala using IntelliJ, you therefore have two ways of compiling your project:
SBT, in which case you would only use IntelliJ as an advanced text editor
IntelliJ
You could, in theory, mix and match: start building with SBT and continue with IntelliJ or the other way around. However, I strongly discourage you to do so, as you may get some unexpected compilation errors. When you want to switch to one tool or the other, it is best to clean all compiled files first.
We will further expand on SBT later in this book, but for now, we are only going to use IntelliJ's own build.
The project has been created and ready to be built. The build process does the following:
Compiles the source files present at the source path and the test path
Copies any resource files needed in the output path
Reports any errors/warnings in the
Message
tool window
There are two ways to build the project:
If you want to build your project incrementally, go to
Build
|
Build Project
(
Ctrl
+
F9
or
cmd + F9
)
If you want to delete all files and rebuild everything, go to
Build
|
Rebuild All
As we do not have a source yet, the build is fast and no errors should appear in the Message tool window.
In IntelliJ, you need to have a run configuration whenever you want to run something: a program, a unit test, an external tool. A run configuration sets up the classpath, arguments, and environment variables that you need to run your executable.
We need to create a run configuration the first time we want to run the Scala console:
Go to
Run
|
Edit Configurations
. Click on the green
+
button, and select
Scala Console
. You should see the following screen:
Make the following changes and click
OK
:
Name:
Scala Console
.
Check
Single instance only
box
– w
e rarely need to have two consoles running at the same time.
In,
Before launch
, click on
Build
and then click the
Remove
button. This way, you will always be able to quickly run a console, even if your code does not compile.
Following that, click on
OK.
On the top toolbar, you should see that IntelliJ created a new Scala Console run configuration:
Click on the green arrow to run the console. You should see the following at the bottom of the screen, in the Run window. We can now type our first Scala expression after the Scala prompt:
By now, all the necessary tools and libraries should be installed. Let's start to play with the basics of Scala by experimenting in different environments. The simplest way to try Scala is to use the Scala Console. Subsequently, we will introduce the Scala Worksheet, which allows you to keep all the instructions that are entered in a file.
IntelliJ offers another handy tool to experiment with the language: the Scala worksheet.
Go to File | New | Scala Worksheet. Name it worksheet.sc. You can then enter some code on the left-hand side of the screen. A red/green indicator in the top right corner shows you if the code you are typing is valid or not. As soon as it compiles, the results appear on the right-hand side:
You will notice that nothing gets evaluated until your whole worksheet compiles.
