This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we will learn to build our first Scala app.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In this video,
we will build our first Scala app.
0:00
We'll learn how to create functions for
loops, guards, and k statements.
0:03
I'll be using IntelliJ as my IDEA.
0:08
And I've included a link in the teacher
notes if you'd like to download the IDEA
0:10
and follow along.
0:15
Let's fire up IntelliJ.
0:17
Click create new project.
0:19
Choose Scala from the list, click next,
and provide a project name.
0:21
Click finish.
0:27
In the project source directory, we'll
right click and create a use Scala class.
0:29
For now we'll create
an object called superheroes.
0:36
Within that, we will create a main
functions that prints out Hello World.
0:44
Let's compile and run our application.
1:04
On the upper right hand corner,
1:06
we'll click the down arrow,
click edit configurations,
1:08
click the plus sign to add a new
configuration to run as an application.
1:11
And for our main class,
we'll select our super heros object.
1:17
We can also provide a name for
this configuration.
1:23
Then we'll click Apply,
and then select OK.
1:27
To run our app we'll
click on the green arrow.
1:31
Great, we've created our first Scala app
which prints Hello World to the screen.
1:35
For the next few examples, we'll be
using what is known as a work sheet.
1:40
A Scala file which is evaluated on save.
1:43
Scala work sheets are very useful as they
can show the output of our expressions
1:46
on the right hand side.
1:51
To define a function in Scala, we use the
def keyword along with a functions name,
1:52
followed by a comma
separated list of parameters
1:57
we will like to pass in a function body.
2:01
For each parameter we will have to provide
the type annotation proceeded [SOUND] by
2:03
a colon.
2:08
As a Scala compiler cannot
infer function parameter types.
2:08
We'll right click the root of
our directory, select new.
2:13
Then click on Scala Worksheet, and then
we'll provide a name for our worksheet.
2:18
In our case, we'll call it functions.
2:23
The following code creates a function name
multiplied by two, which takes in a single
2:48
integer parameter followed by
an equal sign in the function body.
2:53
The function checks of x is equal to zero
in which case it'll return the value of x
2:57
or otherwise both applies
multiplies x by two.
3:02
The last expression evaluated in
a function is always returned.
3:04
So you don't have to explicitly call
return as you might do so in Java.
3:08
Also, it is almost always not necessary
to provide the result type of a function.
3:13
Look at a case where it is
required here in a bit.
3:18
To explicitly return the type would
have to add a colon followed by the type
3:21
annotation before the equal
sign in our function like so.
3:26
Scala also supports the fold arguments for
functions.
3:32
So if an argument is not passed, they'll
provide the default value to the function.
3:36
Additionally, if a function
body exceeds to multiple lines.
3:40
You can used a block implement your code.
3:45
Let's take a look.
3:47
In this case, X will default to ten and
we'll multiply the value passed in for
4:08
the variable Y.
4:12
This is exactly how we would invoke the
function with one parameter which would
4:14
return 50 as variable Y takes the value
five and variable X is defaulted to ten.
4:18
And as you can see on the right-hand
side the result of our function call
4:25
is equal to 50.
4:29
In Java, you may have come across
functions that don't return anything.
4:31
We try to find with
the return type of void.
4:35
Similarly in Scala, you can create
functions that don't return anything
4:37
by setting the result type to unit.
4:42
Let's take a look
4:44
Here we've created a function called
greeting which does not return a value but
4:55
simply prints out
Hello World to the screen.
4:59
Earlier we mentioned that there's a case
when we need to explicitly provide
5:05
the return type of a function.
5:08
In Scala, we need to do so
with the recursive function.
5:11
A recursive function is
a function which calls itself.
5:14
Finding the factorial of a number is
easily implemented as a recursive
5:17
function.
5:21
Great.
5:42
Unlike Java,
Scala does not directly support the for
5:44
statement you may be familiar with.
5:47
Let's dive into Scala loops and
see how they differ.
5:50
You can read the left arrow as, in.
6:03
So for i in one to ten, print i.
6:06
Therefore, we loop from one to ten and
print the element i.
6:10
In this case, one to ten creates an
immutable range, and it's also inclusive.
6:13
For an exclusive range,
we can use the keyword until.
6:18
The form variable left arrow
expression is known as a generator.
6:30
Each generator in Scala can have a guard,
which is a boolean condition preceded by
6:35
an if that allows for processing of
elements that match the condition.
6:39
In this example, we use a guard which
allows for processing of the range and
6:58
we print all the even
numbers from one to ten.
7:02
A for comprehension is a type of loop
which returns a collection of values.
7:05
These are great for
iterating over collections and
7:09
attaining a new collection
of the same type.
7:11
We can achieve this in Scala
with the yield keyword.
7:14
As we can see,
7:31
yield creates a new immutable
collection which is stored in numbers.
7:32
Scala also provides a very
useful case statement
7:37
which is similar to Java switch statement.
7:40
Let's take a look at how that works
7:43
The last line, the case_ is the catch-all
phrase which you always want to provide.
8:17
Otherwise if a match is not found
there will be a match error thrown.
8:23
This is similarly specified using
the default keyword in Java However
8:27
unlike Java switch statements,
there's no fall through problem.
8:33
So we do not need to have a break
statement after each case.
8:36
We can also save the result into value or
8:40
even create a function
based on pattern matching.
8:42
Calling findAge passing the number 20,
9:36
returns us the age as a string
with a value 20, great.
9:39
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up