1 00:00:00,620 --> 00:00:02,810 - [Narrator] In this video we're going to talk about 2 00:00:02,810 --> 00:00:06,150 functional-style programming in Python. 3 00:00:06,150 --> 00:00:10,450 Now, Python itself is not a functional programming language. 4 00:00:10,450 --> 00:00:13,240 There are languages out there that are ` 5 00:00:13,240 --> 00:00:14,470 what we would refer to as 6 00:00:14,470 --> 00:00:16,970 pure functional programming languages. 7 00:00:16,970 --> 00:00:18,900 Uh, Python, on the other hand, 8 00:00:18,900 --> 00:00:21,200 is a general purpose programming language 9 00:00:21,200 --> 00:00:23,670 that supports procedural programming. 10 00:00:23,670 --> 00:00:26,030 It supports object object oriented programming, 11 00:00:26,030 --> 00:00:29,270 and it supports a number of key aspects 12 00:00:29,270 --> 00:00:31,830 of functional programming, ah, 13 00:00:31,830 --> 00:00:33,630 but because it's not an actual 14 00:00:33,630 --> 00:00:37,200 functional programming language we chose to to refer to that 15 00:00:37,200 --> 00:00:40,910 in our book as functional-style programming. 16 00:00:40,910 --> 00:00:43,730 Now, in functional-style programming 17 00:00:43,730 --> 00:00:48,220 we tend to focus on what we want to accomplish 18 00:00:48,220 --> 00:00:51,150 and less on how to accomplish it. 19 00:00:51,150 --> 00:00:56,000 The how part is generally handled by the libraries. 20 00:00:56,000 --> 00:00:57,660 So, just as a for example, 21 00:00:57,660 --> 00:01:00,109 let's revisit that sum function that we've used 22 00:01:00,109 --> 00:01:04,130 a couple of times now that's built into Python. 23 00:01:04,130 --> 00:01:08,470 We can certainly write our own code that specifies 24 00:01:08,470 --> 00:01:12,290 how to calculate the total of a bunch of integer values 25 00:01:12,290 --> 00:01:14,670 and get that final result, 26 00:01:14,670 --> 00:01:16,210 but we don't have to do that 27 00:01:16,210 --> 00:01:19,580 because the sum function already knows how to do that. 28 00:01:19,580 --> 00:01:23,600 So when we want to calculate the sum of a bunch of integers, 29 00:01:23,600 --> 00:01:28,230 we don't have to say, "Go declare a variable called total, 30 00:01:28,230 --> 00:01:31,580 add each individual item to the total, 31 00:01:31,580 --> 00:01:33,810 figure out how many times to do that, 32 00:01:33,810 --> 00:01:35,330 and give me back the total." 33 00:01:35,330 --> 00:01:36,627 All we have to do is say, 34 00:01:36,627 --> 00:01:39,660 "Here is the numbers, calculate the sum." 35 00:01:39,660 --> 00:01:44,010 So it's a more declarative style of programming. 36 00:01:44,010 --> 00:01:47,670 The case where we are figuring out all the details 37 00:01:47,670 --> 00:01:49,370 of how to calculate the total 38 00:01:49,370 --> 00:01:52,470 would be known as external iteration. 39 00:01:52,470 --> 00:01:55,570 We specify not only what we want to accomplish 40 00:01:55,570 --> 00:01:58,350 but how we want to accomplish it. 41 00:01:58,350 --> 00:02:00,190 In functional-style programming, 42 00:02:00,190 --> 00:02:02,990 we focus on internal iteration, 43 00:02:02,990 --> 00:02:06,480 meaning there is still iteration going on, 44 00:02:06,480 --> 00:02:09,050 but it's hidden from us by the libraries, 45 00:02:09,050 --> 00:02:11,360 by things like the sum function 46 00:02:11,360 --> 00:02:14,770 or the methods that are part of the statistics module 47 00:02:14,770 --> 00:02:18,900 that we've demonstrated to you previously as well. 48 00:02:18,900 --> 00:02:20,930 So what you'll find is that 49 00:02:20,930 --> 00:02:24,815 as you get into functional-style programming in Python 50 00:02:24,815 --> 00:02:29,470 you're going to write code where you mostly declare 51 00:02:29,470 --> 00:02:31,490 what it is you want to do. 52 00:02:31,490 --> 00:02:35,610 You will have to specify which library function to use 53 00:02:35,610 --> 00:02:37,730 or which method of a class to use 54 00:02:37,730 --> 00:02:39,940 as we'll see in some later examples, 55 00:02:39,940 --> 00:02:42,077 but you won't have to specify 56 00:02:42,077 --> 00:02:45,000 how those libraries perform their tasks. 57 00:02:45,000 --> 00:02:48,100 They have already done that for you. 58 00:02:48,100 --> 00:02:51,740 Now a key aspect of functional-style programming 59 00:02:51,740 --> 00:02:54,748 is a concept called pure functions. 60 00:02:54,748 --> 00:02:59,430 Pure functions are functions that produce results results 61 00:02:59,430 --> 00:03:04,234 that are dependent only on what you pass into the function 62 00:03:04,234 --> 00:03:05,840 in the first place. 63 00:03:05,840 --> 00:03:09,550 So they don't have any side effects, uh, 64 00:03:09,550 --> 00:03:11,910 where they modify other variables 65 00:03:11,910 --> 00:03:14,730 that you have access to in your code, 66 00:03:14,730 --> 00:03:17,540 and that's what we want to take a look at 67 00:03:17,540 --> 00:03:21,830 here in the context of that sum function once again. 68 00:03:21,830 --> 00:03:25,250 So let's switch over to an IPython session, 69 00:03:25,250 --> 00:03:28,220 and let's create a list of values. 70 00:03:28,220 --> 00:03:32,100 I will just do one, two three for this example. 71 00:03:32,100 --> 00:03:36,996 Now, the sum function is one example 72 00:03:36,996 --> 00:03:41,640 of functional-style programming in the context of Python. 73 00:03:41,640 --> 00:03:45,700 It's a library function, it knows how to perform a task, 74 00:03:45,700 --> 00:03:49,840 the task that it performs is to add up some values, 75 00:03:49,840 --> 00:03:51,550 and give you back a result. 76 00:03:51,550 --> 00:03:54,990 And the result you get back is always going to depend 77 00:03:54,990 --> 00:03:59,090 exclusively on what you give it in the first place. 78 00:03:59,090 --> 00:04:03,270 So if I pass values into sum, 79 00:04:03,270 --> 00:04:05,400 I'm going to get the total six, 80 00:04:05,400 --> 00:04:09,850 and every time I do that I'm going to get the same total 81 00:04:09,850 --> 00:04:12,840 if I give it the same initial values. 82 00:04:12,840 --> 00:04:17,020 Of course, if I give, an and separately it's not going to, 83 00:04:17,020 --> 00:04:20,330 uh, modify the data that it receives. 84 00:04:20,330 --> 00:04:23,380 Therefore, I can see that the values list 85 00:04:23,380 --> 00:04:25,410 still contains one, two, and three, 86 00:04:25,410 --> 00:04:29,310 even after a couple of calls to the sum function. 87 00:04:29,310 --> 00:04:32,059 So a pure function depends, 88 00:04:32,059 --> 00:04:35,520 has outputs that depend only on the inputs, 89 00:04:35,520 --> 00:04:39,070 and there are no side effects as a result 90 00:04:39,070 --> 00:04:41,180 of the function being called, 91 00:04:41,180 --> 00:04:45,011 meaning that the data you give it is left unchanged, 92 00:04:45,011 --> 00:04:48,293 unmodified after the call completes.