1 00:00:00,750 --> 00:00:03,450 I said it at the beginning of the course, but it's worth repeating. 2 00:00:03,450 --> 00:00:06,000 Functional programming is just another tool in your shed, 3 00:00:06,000 --> 00:00:08,310 instead of an entire means to an end. 4 00:00:08,310 --> 00:00:11,830 In that spirit, we're going to use a bunch of JSON data and then solve different 5 00:00:11,830 --> 00:00:15,730 problems with it, instead of just creating a single unified piece of software. 6 00:00:15,730 --> 00:00:17,320 Let's go look at the data. 7 00:00:17,320 --> 00:00:19,890 So before we get into actually writing some code, 8 00:00:19,890 --> 00:00:25,630 I wanted to talk about the data that we are gonna be using, because we need data. 9 00:00:25,630 --> 00:00:28,700 So the idea is that we're running a bookseller, 10 00:00:28,700 --> 00:00:32,510 a bookstore of some sort, and we have a whole bunch of books and 11 00:00:32,510 --> 00:00:36,590 we are gonna write software to tell us information about our books. 12 00:00:36,590 --> 00:00:37,980 So that's handy. 13 00:00:37,980 --> 00:00:42,340 And we've got a number of pages, we have a price, a publishing date, 14 00:00:42,340 --> 00:00:48,190 it's just a year, a list of subjects, and a title for each book. 15 00:00:48,190 --> 00:00:51,950 So you can see here's the book, 11/22/63, here's the book Bag of Bones, 16 00:00:51,950 --> 00:00:56,250 here's the book Carrie, here's the book Christine. 17 00:00:56,250 --> 00:00:58,700 If you didn't guess, these are all Stephen King books, 18 00:00:58,700 --> 00:01:00,970 I've got a little bit of a soft spot for Stephen King. 19 00:01:00,970 --> 00:01:03,386 I don't know, I find him fun, he's interesting. 20 00:01:03,386 --> 00:01:07,590 Okay, so there's that. 21 00:01:07,590 --> 00:01:09,310 That's all of our data. 22 00:01:09,310 --> 00:01:14,880 And then you see the prices are these ridiculously long and precise floats. 23 00:01:14,880 --> 00:01:19,690 We probably wouldn't want to have that in actual real data coming from an API. 24 00:01:19,690 --> 00:01:23,040 But it's fine for this, we're not gonna let that bother us. 25 00:01:23,040 --> 00:01:28,610 We're gonna write software that doesn't care about those ridiculously long floats. 26 00:01:28,610 --> 00:01:30,950 And we're gonna handle rounding them all off. 27 00:01:30,950 --> 00:01:35,140 So we're good, we're fine, don't worry about those being kinda weird. 28 00:01:35,140 --> 00:01:37,680 The other thing to talk about is the software that's going to 29 00:01:37,680 --> 00:01:38,780 give us our books. 30 00:01:38,780 --> 00:01:41,950 So you can see here that we have this class called Book. 31 00:01:43,850 --> 00:01:45,960 It's a really short class. 32 00:01:45,960 --> 00:01:49,270 We just take whatever quarks come in, and assign them as attributes. 33 00:01:49,270 --> 00:01:52,010 So price will come in and we'll set that as the price. 34 00:01:52,010 --> 00:01:53,910 And title will come in and we'll set that as the title. 35 00:01:54,980 --> 00:01:58,840 And then we have this get_books function that takes a file name and 36 00:01:58,840 --> 00:02:03,350 either gives us raw data, which is just straight data out of the JSON file, 37 00:02:03,350 --> 00:02:05,620 just a bunch of dictionaries. 38 00:02:05,620 --> 00:02:07,960 Or it gives us back our classes. 39 00:02:07,960 --> 00:02:12,140 So we're gonna set that up as Books and we're gonna get all of our books. 40 00:02:12,140 --> 00:02:16,180 We're not gonna need the raw stuff for the most part. 41 00:02:16,180 --> 00:02:18,270 We might end up using it once or twice, but 42 00:02:18,270 --> 00:02:20,800 for the most part we're just gonna deal with books. 43 00:02:20,800 --> 00:02:23,100 But it's kinda nice to have something like this where if you're like, 44 00:02:23,100 --> 00:02:27,350 oh I need to be able to get to the Dictionary, then I can. 45 00:02:27,350 --> 00:02:31,910 So it's a good idea to keep something like this in mind. 46 00:02:31,910 --> 00:02:34,030 And then that's our data. 47 00:02:34,030 --> 00:02:39,180 We have our data and then we have our digital, our Python version of the data, 48 00:02:39,180 --> 00:02:40,330 which is this class here. 49 00:02:40,330 --> 00:02:44,330 The other thing I wanna talk about though is functions. 50 00:02:44,330 --> 00:02:48,150 So we've gone over this in the Decorator's Workshop, 51 00:02:48,150 --> 00:02:49,990 which you may or may not have taken. 52 00:02:51,240 --> 00:02:54,420 And if you didn't I suggest that you do, you should go check that out. 53 00:02:54,420 --> 00:02:57,890 But functions are just like any other variable in Python, 54 00:02:57,890 --> 00:02:59,760 any other bit of data period. 55 00:02:59,760 --> 00:03:04,630 A function is the same as an integer is the same as a dictionary, 56 00:03:04,630 --> 00:03:06,960 it's the same as a class. 57 00:03:06,960 --> 00:03:09,880 Which is all fun to mess with. 58 00:03:09,880 --> 00:03:14,280 And so what that means is that how we would normally accept and 59 00:03:14,280 --> 00:03:17,700 integer or a dictionary or whatever in a function. 60 00:03:17,700 --> 00:03:19,460 We can accept a function. 61 00:03:19,460 --> 00:03:22,696 So to illustrate that, I'm gonna do another function here, 62 00:03:22,696 --> 00:03:24,638 we're gonna call this say_hello. 63 00:03:26,518 --> 00:03:31,390 And it's just going to return, or that's not gonna return, 64 00:03:31,390 --> 00:03:34,330 sorry it's gonna print, Hello!. 65 00:03:34,330 --> 00:03:41,230 Okay, and so now, I'm gonna do log_and_run and I'm gonna pass it say_hello. 66 00:03:41,230 --> 00:03:42,770 And let's see. 67 00:03:42,770 --> 00:03:47,437 Let's do a little bit 68 00:03:47,437 --> 00:03:52,370 of a print log and run. 69 00:03:52,370 --> 00:03:53,775 And then, I'm not sure why that indented. 70 00:03:53,775 --> 00:03:59,550 And then we're gonna print log and return. 71 00:03:59,550 --> 00:04:04,150 And we'll do log_and_ return, say_hello. 72 00:04:05,500 --> 00:04:08,600 All right so we're just gonna use our functions here. 73 00:04:08,600 --> 00:04:12,551 Let's try this out, role this down, 74 00:04:12,551 --> 00:04:16,910 scroll that up, python functions.py. 75 00:04:16,910 --> 00:04:18,540 And we get log and run. 76 00:04:18,540 --> 00:04:19,660 I just got say_hello. 77 00:04:19,660 --> 00:04:20,340 Hello. 78 00:04:20,340 --> 00:04:21,390 Log and return. 79 00:04:21,390 --> 00:04:22,250 I just got say_hello. 80 00:04:22,250 --> 00:04:25,035 Now why didn't log and return print out anything? 81 00:04:25,035 --> 00:04:27,820 Well, it's because it just returned to the function. 82 00:04:28,860 --> 00:04:30,985 So test this out. 83 00:04:30,985 --> 00:04:37,047 Let's do hola = log_and_return and then we're gonna call hola. 84 00:04:37,047 --> 00:04:38,110 Okay? 85 00:04:41,410 --> 00:04:42,160 And look at that. 86 00:04:42,160 --> 00:04:45,660 We've got effectively the same thing twice here. 87 00:04:45,660 --> 00:04:47,350 Now why did that work? 88 00:04:47,350 --> 00:04:50,530 So the reason that works is because at the end of log_and_return, 89 00:04:50,530 --> 00:04:55,660 we returned a function, just like we would return any other bit of data in Python. 90 00:04:55,660 --> 00:04:59,410 And here we called the function, cuz we used these two parens, and 91 00:04:59,410 --> 00:05:01,420 we called it just like we would any other function. 92 00:05:01,420 --> 00:05:02,750 Even though now we don't know the name of it. 93 00:05:02,750 --> 00:05:04,630 We just know func. 94 00:05:04,630 --> 00:05:10,480 So it's really handy to be able to pass functions around and 95 00:05:10,480 --> 00:05:12,780 you just think of them as any other data type. 96 00:05:12,780 --> 00:05:15,160 They work just like all the others. 97 00:05:15,160 --> 00:05:18,270 We've worked with functions as arguments and return values before. 98 00:05:18,270 --> 00:05:20,860 But I figure it's always good to have a little refresher. 99 00:05:20,860 --> 00:05:22,570 If you want a bit more modifying and 100 00:05:22,570 --> 00:05:25,150 using functions, check out my Decorator's Workshop. 101 00:05:25,150 --> 00:05:27,360 I'll link to it in the teacher's notes. 102 00:05:27,360 --> 00:05:29,440 All right, now that we have usable data. 103 00:05:29,440 --> 00:05:32,450 Let's get to some of the pieces in our functional programming tool belt. 104 00:05:32,450 --> 00:05:34,340 We'll clean up the streets one function at a time.