1 00:00:00,000 --> 00:00:04,599 [MUSIC] 2 00:00:04,599 --> 00:00:08,369 Welcome back, we went over a lot of new stuff in the last stage, so 3 00:00:08,369 --> 00:00:10,460 let's recap for a bit. 4 00:00:10,460 --> 00:00:13,710 We learned that functions are first class citizens. 5 00:00:13,710 --> 00:00:15,140 What does this mean? 6 00:00:15,140 --> 00:00:18,740 Well it means that functions can be treated much the same way as we treat 7 00:00:18,740 --> 00:00:22,630 integers, strings or any other first class type. 8 00:00:22,630 --> 00:00:25,650 We learned that we can assign a function to a constant and 9 00:00:25,650 --> 00:00:28,860 pass it around, you can then refer to the function 10 00:00:28,860 --> 00:00:32,660 using the constant name rather than the function itself. 11 00:00:32,660 --> 00:00:35,360 The next important thing was that a function can accept 12 00:00:35,360 --> 00:00:39,618 another function as input much like it can accept an integer or a string. 13 00:00:39,618 --> 00:00:45,310 Not only can functions accept strings as inputs, it can also return a function. 14 00:00:45,310 --> 00:00:49,120 We use these concepts to build higher order functions. 15 00:00:49,120 --> 00:00:53,440 Finally, we learned how to write a nested function that can capture or 16 00:00:53,440 --> 00:00:57,850 close over variables or constants from the surrounding context. 17 00:00:57,850 --> 00:01:01,310 Doing this forms what is known as a closure. 18 00:01:01,310 --> 00:01:06,730 So let's solidify our definition, a closure is a combination of a function and 19 00:01:06,730 --> 00:01:09,930 an environment of captured variables. 20 00:01:09,930 --> 00:01:13,590 Closures are equivalent to anonymous functions, lambdas, and 21 00:01:13,590 --> 00:01:15,710 blocks in other languages. 22 00:01:15,710 --> 00:01:20,361 Before we look at why closures are useful let's start with how we can write 23 00:01:20,361 --> 00:01:21,121 a closure. 24 00:01:21,121 --> 00:01:24,249 Now the first way, which happens to be the longer way, 25 00:01:24,249 --> 00:01:26,360 is what we covered in the last video. 26 00:01:27,380 --> 00:01:31,490 You start by writing a function that contains an environment of capture 27 00:01:31,490 --> 00:01:34,720 variables and then assign it to a constant. 28 00:01:34,720 --> 00:01:37,810 This you're familiar with, we just went through how to do it. 29 00:01:37,810 --> 00:01:40,575 The second way is to write a closure expression. 30 00:01:40,575 --> 00:01:45,870 While closure and closure expressions are actually different implementations, 31 00:01:45,870 --> 00:01:50,450 it is common to refer to both these styles with the term closure. 32 00:01:50,450 --> 00:01:55,170 I'll be specific for now, but in future courses when I or anyone else 33 00:01:55,170 --> 00:02:00,770 says closure, we're going to use the term interchangeably with a closure expression. 34 00:02:00,770 --> 00:02:04,810 A closure expression syntax has the following general form. 35 00:02:04,810 --> 00:02:08,720 We start with a set of braces to define the closure expression. 36 00:02:09,740 --> 00:02:14,370 Inside the braces, first we include the parameters in a set 37 00:02:14,370 --> 00:02:18,590 of parentheses followed by an arrow and the return type. 38 00:02:19,700 --> 00:02:22,710 This defines the type of the closure. 39 00:02:22,710 --> 00:02:26,010 To start the body of the closure, we write the keyword in and 40 00:02:26,010 --> 00:02:27,260 then write our statements. 41 00:02:27,260 --> 00:02:29,070 A lot easier right? 42 00:02:29,070 --> 00:02:31,260 Well let's look at an example. 43 00:02:31,260 --> 00:02:35,510 The Swift standard library contains a really useful function 44 00:02:35,510 --> 00:02:37,270 on a raise named map. 45 00:02:38,460 --> 00:02:41,470 The map function accepts a function and 46 00:02:41,470 --> 00:02:45,580 returns a new array after performing the methods of the function on 47 00:02:45,580 --> 00:02:50,510 each member of the array, as always this is easier with an example. 48 00:02:50,510 --> 00:02:56,197 Now we'll do this in a new playground page, 49 00:02:56,197 --> 00:02:58,972 we'll add a new one. 50 00:02:58,972 --> 00:03:03,789 We'll call this new one closure expressions and 51 00:03:03,789 --> 00:03:07,559 we'll name the first one functions. 52 00:03:15,985 --> 00:03:20,464 Okay so we're gonna start here by rewriting that double function we wrote 53 00:03:20,464 --> 00:03:21,990 earlier so func double. 54 00:03:26,080 --> 00:03:29,190 Double will accept an integer as its only argument and 55 00:03:29,190 --> 00:03:32,770 then return a value that equals that integer multiplied by two. 56 00:03:33,960 --> 00:03:38,408 So value of type Int, and returns an Int. 57 00:03:38,408 --> 00:03:45,048 And this is simply going to be value * 2. 58 00:03:45,048 --> 00:03:52,354 We'll also assign this to a constant, we'll say let doubleFunction = double. 59 00:03:52,354 --> 00:03:56,774 Now we can test that this works by passing in an argument to double function and 60 00:03:56,774 --> 00:03:58,550 in the result area will see 4. 61 00:03:58,550 --> 00:04:05,350 Okay now given an array of integers, we'll say let numbers equal 1,2,3,4, 62 00:04:05,350 --> 00:04:12,020 I want a new array containing each of those values doubled. 63 00:04:13,110 --> 00:04:15,480 I can do this using the map function and 64 00:04:15,480 --> 00:04:18,250 passing in the double function as an argument. 65 00:04:18,250 --> 00:04:23,030 For now all you need to know about map, is that it takes each value out of the array, 66 00:04:23,030 --> 00:04:26,770 out of the numbers array and allows us to work with each value. 67 00:04:26,770 --> 00:04:35,450 So we can say let doubledNumbers = numbers.map (doubleFunction). 68 00:04:35,450 --> 00:04:38,320 And if you look in the results area, you see that this works. 69 00:04:38,320 --> 00:04:42,200 And we now have a new array containing double the original values. 70 00:04:42,200 --> 00:04:44,260 If you're wondering how this is a closure, 71 00:04:44,260 --> 00:04:47,760 keep in mind that the map function forms the outer function. 72 00:04:47,760 --> 00:04:49,290 And the inner function, 73 00:04:49,290 --> 00:04:53,920 that is the double function, captures the variables defined in the map context. 74 00:04:54,970 --> 00:04:57,840 Other than what map does which we're going to ignore for 75 00:04:57,840 --> 00:05:02,270 just a small bit, there isn't anything really new going on here. 76 00:05:02,270 --> 00:05:06,060 Map takes a function while passing in the doubler function as argument. 77 00:05:07,070 --> 00:05:11,810 All of this was a bit cumbersome though we had to declare a function, assign it to 78 00:05:11,810 --> 00:05:16,700 a constant, pass that constant to another function as an argument. 79 00:05:16,700 --> 00:05:20,480 So in the next video, let's take a look at how closure expressions makes this 80 00:05:20,480 --> 00:05:21,630 much nicer to deal with.