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’ll learn some useful higher order functions and how to use them. We will use some of the collections we learned in the earlier stage to become comfortable with higher order functions.
New Terms:
Higher-order functions -- Functions which take other functions as parameters or whose result is a function.
Further Reading:
Higher-order functions
Map function
Drop and drop while functions
Hi.
0:00
In the next two videos,
we'll learn about higher order functions.
0:01
Higher order functions are functions
that take other functions as parameters.
0:05
In the first video, we'll focus on
using built In higher order functions.
0:10
Whereas in the second video,
we'll create our own.
0:14
You may have noticed in earlier
stages that in certain locations,
0:18
I used the foreach function to
print elements in a collection.
0:21
Foreach is a high order function which
takes another function as a parameter.
0:25
Let's take a look.
0:30
Let's recall our heroes array from
an earlier lesson, when we called for
0:32
each on our heroes array, it would
bring out every element in the array,
0:37
let's dive back into the code and
take a look.
0:41
As we've noticed earlier,
1:04
we call the foreach function on
the heros array then we pass in
1:06
the print line function and this in total
prints out every element in our array.
1:09
Let's recompile our app and
check out the results.
1:15
There are many other useful higher order
functions in Scala, let's dive into them.
1:19
One of the ones I find
myself using most is map.
1:25
Map allows us to apply a function
to each element in a collection.
1:29
For example, if we have an array of
elements, and we call them .mapfunction,
1:34
we iterate through the array and
can perform actions on each element.
1:39
For instance, we created a function
to convert each element in the array
1:44
to lowercase in order
to achieve our result
1:48
we had to loop throughout
the collection using a four loop.
1:52
However, now we can achieve the same
result in a much more concise approach
1:55
using map.
1:59
Let's take a look.
2:00
In this line, we take the heroes array and
call the map function on it to iterate
2:14
throughout all the elements
of the collection and for
2:18
each one of these elements,
we convert them to lower case.
2:21
Another way to write the above function
literal is to use an underscore.
2:25
Think of an underscore as a wildcard which
refers to the elements being iterated on.
2:30
Let's take a look at that.
2:35
Let's re-run our app and
check out the results.
3:00
As expected, every element in the array,
is lowercase, however,
3:06
this code is in a way more concise
than what we had previously.
3:10
Another useful higher
order function is filter.
3:14
Filter takes a function that returns a
Boolean and builds a new collection in for
3:17
which the predicate is true.
3:22
In this case, we filter for
3:38
all the superheros that contain
the string man in their name.
3:40
Let's take a look at the result.
3:44
Awesome.
3:51
As expected we got all
the elements except for Thor.
3:52
Learning about higher order
functions is great but
3:56
they become way more powerful if
4:04
we can learn how to use them together.
4:09
In this instance, we use the filter
to grab only sent elements we want.
4:25
And then map to iterate to reach one
nd return their value in upper case.
4:30
To dive in a little bit more detail,
filter takes a function called contains
4:34
which checks if the string man exists,
and then map takes a function
4:40
called two uppercase to convert the
characters of the elements to uppercase.
4:45
Let's recompile our app and
check out the results.
4:50
Awesome, as expected we
filtered every element and
4:59
made sure that we only got back
the ones containing the key word man.
5:02
And we're also able to change
them to all upper case.
5:07
There are many other useful higher order
functions such as count, drop while, drop
5:11
Index where which we can get familiar with
by implementing an array of integeres.
5:16
Let's create an array of integers to get
familiar with some of the higher order
5:22
functions we just mentioned.
5:26
Great, the first one we'll
use is dropWhile, which loops
5:39
through the collection and drops any
elements that do not satisfy a predicate.
5:43
In this case, any element less than three.
5:48
As expected, we drop any elements
that were less than the value three,
6:07
hence number one and
two are no longer in our array.
6:12
Another function, count, counts the number
of elements that match the predicate.
6:16
In our case we'll check how many
elements are less than or equal to four.
6:20
Let's recompile our app and
check out our results.
6:36
Awesome.
6:46
As we can see, the numbers.count function
returned seven elements and that's
6:47
because there's seven elements in the
array that are less than or equal to four.
6:51
Another useful function is index ware.
6:56
Index ware returns the index
of the element found or
6:59
a negative one if the element
is not in the collection.
7:02
Let's take a look.
7:05
Awesome.
7:30
As expected,
the value three is found in index two.
7:31
Remember, arrays are zero
based even in scalar.
7:35
Great, we learned about higher
order functions in Scala.
7:39
In the next video we'll create
our own higher order function and
7:42
use it in our code.
7:46
See you here.
7:47
You need to sign up for Treehouse in order to download course files.
Sign up