Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Scala Programming

You're reading from   Learning Scala Programming Object-oriented programming meets functional reactive to create Scalable and Concurrent programs

Arrow left icon
Product type Paperback
Published in Jan 2018
Publisher Packt
ISBN-13 9781788392822
Length 426 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Vikash Sharma Vikash Sharma
Author Profile Icon Vikash Sharma
Vikash Sharma
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

1. Getting Started with Scala Programming FREE CHAPTER 2. Building Blocks of Scala 3. Shaping our Scala Program 4. Giving Meaning to Programs with Functions 5. Getting Familiar with Scala Collections 6. Object-Oriented Scala Basics 7. Next Steps in Object-Oriented Scala 8. More on Functions 9. Using Powerful Functional Constructs 10. Advanced Functional Programming 11. Working with Implicits and Exceptions 12. Introduction to Akka 13. Concurrent Programming in Scala 14. Programming with Reactive Extensions 15. Testing in Scala 16. Other Books You May Enjoy

Running our first program

Time to do some real work. The recommended way of getting started with a Scala project is to use an activator/gitor8 seed template. For gitor8, you require SBT version 0.13.13 and above. Using SBT, give the command sbt new providing the name of the template. A list of templates can be found at https://github.com/foundweekends/giter8/wiki/giter8-templates/30ac1007438f6f7727ea98c19db1f82ea8f00ac8.

For learning purposes, you may directly create a project in IntelliJ. For that, you may first start the IDE and start with a new project:

  1. Click on the Create New Project function:
  1. Select the Scala | IDEA option and click Next:
  1. Give Project name, Project location, select/locate Scala SDK, and Finish:

You're ready to write your first program.

Let's write some code:

package lsp

object First {
def main(args: Array[String]): Unit = {
val double: (Int => Int) = _ * 2
(1 to 10) foreach double .andThen(println)
}
}

The preceding program does nothing but print doubles of numbers ranging from 1 to 10. Let's go through the code. First, we gave the package declaration with a name lsp. In the next line, we created an object named First. An object in Scala is a singleton container of code which cannot take any parameters. You are not allowed to create instances of an object. Next, we used the def keyword to define the main method that works as an entry point to our application. The main method takes an array of String as parameters and returns Unit. In Scala terminology, Unit is the same as the void, it does not represent any type.

In the definition of this method, we defined a function literal and used it. A value named double is a function literal (also called anonymous function) of type Int => Int pronounced Integer to Integer. It means this anonymous function will take an integer parameter and return an integer response. An anonymous function is defined as _ * 2. Here _ (that is an underscore) is sort of syntactic sugar that infers any expected value, in our case, it's going to be an integer. This is inferred as an integer value because of the signature (Int => Int) Int to Int. This function literal applied on a range of integer values 1 to 10, represented by (1 to 10), gives back doubled values for each integer:

(1 to 10) foreach double .andThen(println)

This line contains a few tokens. Let's take them one by one. First is (1 to 10), which in Scala is a way to represent a range. It's immutable, so once produced it can't be changed. Next, foreach is used to traverse through the range. Subsequently, double is applied on each element from the range. After application of the anonymous function andThen, it composes the result of double and prints it. With this example, you successfully wrote and understood your first Scala program. Even though the code was concise, there's a bit of overhead that can be avoided. For example, the main method declaration. The code can be written as follows:

package lsp

object FirstApp extends App {
val double: (Int => Int) = _ * 2
(1 to 10) foreach double .andThen(print)
}

Here, the same code is written in an object that extends the App trait. By extending the App trait available, you don't have to explicitly write the main method.

You have been reading a chapter from
Learning Scala Programming
Published in: Jan 2018
Publisher: Packt
ISBN-13: 9781788392822
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime