Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that we know how to use closure expressions, let’s write some code so we can see it in use.
Teacher's Notes

0:00
When using closure syntax, we don't need to write a separate doubler function.

0:06
Let's use map again,

0:07
this time to return an array with the numbers tripled from the original array.

0:13
Let's add a comment here, using closure expressions with the map function.

0:22
This time, we don't need to define a triple function.

0:25
Let's do let tripledNumbers, and we'll call the map function on numbers again.

0:34
And as I mentioned, to write a closure expression, we start with a set of braces.

0:40
So inside these parentheses, we'll add an opening and a closing brace.

0:46
Now, inside the braces we'll start first

0:49
with the parameters of the inner function in parentheses.

0:53
So we'll do i cuz we want an integer from the outer scope.

0:56
We'll give it a type of int.

0:58
Next is the return type.

1:01
Since we're returning the number triple, the return type is also int.

1:06
To start the statements of the closure, we add the key word in and

1:11
then the body of our expression, so return i * 3.

1:16
That's it.

1:17
Far simpler, right?

1:19
I mean, it's a lot simpler than declaring a separate function,

1:22
assigning that to a constant, and then passing that constant in.

1:26
Now, the assistant editor on the right says that the expression

1:29
was evaluated six times.

1:32
That's once for each value, since there are five members in this array,

1:35
and then once to return that array.

1:38
And you can see this if you click on the pin button.

1:41
So we'll switch to values and you'll see that the numbers are tripled.

1:45
Closures can also be used in functions that contain multiple parameters.

1:51
Let's look at another example using yet another standard library function, sorted.

1:56
The sorted function takes two arguments.

1:59
The first is an array of values of known type.

2:03
So let me get rid of this.

2:05
I'll add another comment, using closure expressions with the sorted function.

2:12
I'm going to create an array of type string.

2:24
Now, we said that the sorted function takes two arguments.

2:28
So first, we have an array of values of known type, and that's our array names.

2:33
The second argument is a function for sorting the array.

2:37
Now, the function should take two arguments of the same type as the array's

2:41
contents, and returns a boolean value.

2:44
That's a requirement.

2:45
If the first value should appear before the second, we'll return true, and

2:50
false otherwise.

2:51
So let's implement a function that does this.

2:54
Let me make some more room.

2:57
Okay.

2:59
We'll call this function backwards.

3:02
Since our array is of type string, we need to have two arguments of the same type.

3:07
So we'll call it s1 and s2.

3:10
The function also needs to return a boolean value.

3:16
So now if we want the first value to appear before the second,

3:20
we were gonna return true.

3:22
So we'll do return S1 greater than s2.

3:26
This functions accepts two strings, and

3:28
returns true if the first string is greater than the second string.

3:33
But what does greater mean in the context of a string?

3:35
Well, it's pretty simple, actually.

3:37
B is considered greater than A, C is greater than B, and so on.

3:42
And you can verify that by just doing A greater than B and it should say false.

3:48
The other way around is true.

3:50
Now, if the first characters are equal,

3:53
then the second character in each string is evaluated.

3:57
Now we can call the sorted function.

3:59
The sorted function takes an array as its first parameter, so

4:02
let's pass in our names array.

4:05
So we'll call sorted and we'll pass in names.

4:10
And for the second parameter,

4:12
we pass in a function that determines how to sort the array.

4:16
So we'll pass in backwards.

4:18
Now, you should see in the sidebar, I'm gonna expand this out so

4:21
you can see it more, that the array is sorted in backwards order,

4:25
that is, alphabetically, we're going up to A.

4:29
Okay, let's try this using a closure expression instead.

4:33
First, we'll get rid of this code so that our array isn't sorted.

4:36
I'm just going to comment it out.

4:38
Now, to use a closure expression,

4:41
we use the same syntax and list it as the second parameter.

4:46
So we'll call sorted again.

4:49
We'll pass in names.

4:51
And this time, we're gonna pass in a closure instead of the backwards function.

4:56
So first we started with the curly brace.

4:58
Then inside, we start with the function type.

5:01
So we'll specify the parameters in parentheses.

5:05
Then, we specify the return type, which is Bool,

5:10
followed by the keyword in, and finally, the body.

5:16
Pretty cool, huh?

5:17
You'll see that in the sidebar,

5:19
it says that the expression was evaluated ten times.

5:22
Now, if we print this or pin this, we're not gonna see the actual values and

5:26
that's because we're returning booleans here.

5:29
To get a sorted array, we can assign this to a constant, like sortedNames = and

5:36
then if I print out sortedNames, you should see that the array is sorted.

5:41
Awesome.

5:42
Now that we know how to write a basic closure expression,

5:46
let's take a look at some of the more shorthand cases

5:48
of closure expression syntax that we can use to make our code easier.
You need to sign up for Treehouse in order to download course files.
Sign up