This course will be retired on May 31, 2020.
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
In this video, let's add an extension to the Int type and define a function that accepts a math operation.

0:00
Let's take a look at another example.

0:02
Now keep in mind these examples aren't particularly useful bits of code, but

0:07
we're going to use them to reinforce our point.

0:10
What is the point of writing a function that accepts another function

0:13
as an argument?

0:15
Well, it allows us to write a general function, on a type,

0:18
where we customize and defer the implementation when we call the function.

0:23
This concept is also known as a higher order function.

0:27
So let's write an extension on the int type.

0:30
I want to define a method on int

0:33
that applies a math operation to the integer value.

0:37
What math operation are we applying?

0:39
Well I don't know right now, and that's exactly the point.

0:42
We'll define this method in such a way that we can apply any math operation we

0:47
can conceive of when we want, as long as it involves integers.

0:51
So I'll say extension Int, and

0:56
we're going to write a new function, we'll call this applyOperation.

1:00
And this function will have a single parameter operation

1:03
without an external argument label.

1:06
The type of operation will be a function type,

1:10
because we want to be able to pass in any math operation when using apply operation.

1:15
We'll define this parameter, this argument, as a function that takes

1:20
an integer, remember the parameters have to be enclosed in parentheses.

1:25
And then it returns an integer as well, okay.

1:27
So applyOperation here, takes an operation,

1:31
which is a function that takes an int and returns an int.

1:35
When we apply this operation to an integer value we're going to get an int back,

1:40
as we can see from the return type.

1:43
And we want to return this Int as well, so the final return type for

1:47
the entire function will be an Int inside the body of apply,

1:50
we're simply going to call the function that we provide as an argument.

1:54
Now operation, which is a constant holding on to that function,

1:58
expects an integer value as an argument.

2:01
And since we're doing this as an extension on Int itself,

2:05
we're going to pass in self as the argument.

2:08
Now the result of calling operation on self is an integer as well, so

2:11
we can go ahead and return it.

2:14
So pretty simple, we've defined a function that applies some math operation

2:18
on an integer, where we haven't actually defined any particular math operation.

2:23
Since we can now provide a function that matches the signature as an argument,

2:28
we've deferred defining the actual operation until we call this function.

2:32
What apply operation does is ensure that the function

2:36
we provide as an argument is called and a value is returned.

2:40
We can see this in action by defining a simple math operation that

2:44
doubles an integer value, so say func double.

2:49
This is going to take a value of type Int and

2:53
return an integer, inside the body we'll say return value * 2.

2:59
Notice that this signature Int to Int of double matches the parameter

3:04
type of the applyOperation function.

3:07
It is a function that takes an integer and returns an integer.

3:10
We can now use this with the applyOperation function.

3:14
Remember this is an extension on the Int type, so we first write out an integer,

3:18
and then we call applyOperation and we can pass in double

3:24
as the argument and notice in the results area we get 20.

3:28
The double method is really simple, of course, and

3:31
we don't even need a method for this, but

3:33
it highlights how we can pass in a function to apply and get a result.

3:37
The beauty though of this applyOperation function,

3:40
is that it can take any function or any operation that matches the signature.

3:45
Allowing us to define the operation we implement when we call the function.

3:49
So for example, we can define a new function closestMultipleOfSix,

3:55
this function takes an integer and returns an integer,

4:01
and in here we'll just do a bit of math.

4:04
We'll say loop over the first 100 multiples of 6.

4:13
We'll grab that multiple by doing x * 6,

4:17
and if the multiple is in range.

4:21
We'll just do a bit of math here, what I'm doing doesn't, isn't really important.

4:29
All that matters is that it is a different kind of operation.

4:33
Return multiple else if multiple == value returned value and

4:40
then finally down here I'll return 0.

4:46
You can just copy this as it is.

4:48
Okay, now that we have this new operation we can use it with the apply method, so

4:51
I can save 32.applyOperation closestMultipleOfSix and

4:55
it;ll find me the closest multiple of six, which is 36.

4:58
I can do that on 12, closestMultipleOfSix should be 12, there we go.

5:05
And we'll do it on 200, and we should get a correct value.

5:11
Look at that, now you know that you can use functions as

5:14
arguments to other functions, and more importantly this should give you

5:18
an example of why you would want to do that in the first place.
You need to sign up for Treehouse in order to download course files.
Sign up