This course will be retired on February 23, 2018. We recommend "Closures in Swift" for uptodate content.
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 look at another example.

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

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

0:09
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 where we can customize and

0:19
defer the implementation when we actually call the function.

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

0:28
For example, with display string or

0:30
parameter has a type string to void and this means that we can pass in any

0:35
function that has a type string to avoid not just the print string function.

0:40
Why does this matter?

0:41
Let's see, let's write an extension on the Int type.

0:46
I want to define a method on Int that applies a math operation to any

0:50
integer value.

0:52
What math operation do I want to apply?

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

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

1:02
we can conceive of, when we actually call the function.

1:06
So, this is an extension on the Int types.

1:08
So, we'll start with extension, and

1:11
we're going to write a function that we'll call, apply.

1:15
And this function will have a single parameter named, operation.

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

1:24
because we want to be able to pass in any math operation when we actually use apply.

1:31
We'll define our operation as a function that takes an integer, and

1:36
returns an integer as well.

1:39
Okay, so apply takes an operation which is a function that takes an Int and

1:44
returns in it.

1:46
When we apply this operation function to an integer value,

1:51
we're going to get in integer back, right?

1:53
Because this function returns an integer.

1:56
We want to return this integer that we get back from the result of operation.

2:01
So, the final return type for apply will be an Int as well.

2:07
Inside the body of apply, we don't know what operation is just yet,

2:11
but we're simply going to call the function that's assigned to operation.

2:16
Now, because the type signature of operation

2:19
expects an integer value as an argument.

2:23
And since this function is called on an integer itself,

2:26
because this is an extension of Int, we will pass in self as the argument.

2:34
Okay, so now, when we call operation and

2:38
pass in self, we're passing in an integer value, and we get an integer value back.

2:43
And we want that integer value, so we'll just return it.

2:47
Pretty simple.

2:49
We've defined a function that applies a math operation on an integer.

2:53
We haven't actually defined any particular math operation.

2:58
Since we can provide a function that matches the signature as an argument.

3:03
We've deferred defining the actual operation until we call apply.

3:08
What apply does is ensure that the function we provide as an argument is

3:13
called and then a value is passed in as an argument and returned.

3:19
Let's see this an action.

3:20
Let's define a simple math operation that doubles an integer value.

3:26
So we'll say, func double, value Int, right?

3:32
So, it has a single parameter named value of type Int and it returns Int.

3:38
You should notice immediately that this type signature, Int to Int,

3:42
matches the type signature of the argument that operation expects.

3:49
Okay, so in here we'll just return value times 2.

3:54
We can now use this function with the apply function.

3:59
So, since it's an extension on Int, we write out an integer value, and

4:03
then call apply.

4:05
And we can pass in the double function.

4:08
And you see we get 20.

4:10
The double method is really simple, of course and we don't even need a method for

4:15
this, but it highlights how we can pass in a function to apply and get a result.

4:20
The beauty of the apply function though, is that it can take any function or

4:25
any operation,

4:26
allowing us to define the operation we implement when we call the function.

4:31
So, we just saw how to use double.

4:33
Let's define a second operation.

4:36
So, we'll call this closestMultipleOfSix.

4:43
Now, this has to be a function that accepts an integer and

4:46
returns an integer value.

4:48
So again, we'll have a single parameter of type Int and will return an Int.

4:55
This is another trivial function that returns the closest multiple of six

5:00
to the integer that we pass in.

5:03
So, the body of the method is going to be really simple.

5:06
We want the first hundred multiples of six first.

5:09
So, we'll say for x in 1 to 100.

5:16
Now, to get the multiples of six will just say it let multiple equal x times 6.

5:23
So, for this entire loop now we have the multiples 6 through 600.

5:28
And now, we'll say if the multiple is or

5:32
if the difference between the multiple and

5:37
the value is less than six and the multiple is greater than the value.

5:44
Then we want to return the multiple or

5:49
if the multiple is equal to the value, will return the value.

5:57
Now, we need one more returns statement if none of that works and will return zero.

6:02
This needs an if over here, okay?

6:05
So, what we're saying is if the difference between the multiple and the value is

6:10
less than six, this means it's the closest multiple in the six table.

6:15
We're adding a second logic check to say that the multiple has to be greater than

6:18
the value as well, just to make it easy.

6:21
On the bottom of our function, if our logic fails, we return zero.

6:25
This last return statement is needed because our logic is an exhaustive.

6:30
But this is a silly example, so we'll do this.

6:33
We only check the first hundred multiples, so

6:35
over here if we use an integer value that's greater than 100,

6:38
we won't be able to get the closest multiple, so we return zero.

6:42
Okay, now the beauty of this is we can do something like this.

6:47
So say 32.apply and will pass in the closest multiple of six.

6:53
And you see we have a value, 36.

6:55
We can also do 12.apply.

6:59
Pass it in and we should get 12.

7:02
Finally, one more, 200.apply.

7:09
Now you understand that you can use functions as arguments to other functions

7:13
and more importantly you should understand why we want to do that in the first place.

7:19
Over here, we can apply any math operation that

7:23
matches the signature type into Int and we've deferred

7:26
an implementation on the operation until we actually call the function.

7:30
We can pass in any function at this point.

7:33
And it is applied to an integer value.
You need to sign up for Treehouse in order to download course files.
Sign up