1 00:00:00,620 --> 00:00:05,350 In order to start our journey on thinking functionally, one really important concept 2 00:00:05,350 --> 00:00:09,620 to dial in on is the difference between imperative and declarative coding styles. 3 00:00:11,110 --> 00:00:15,920 The imperative style focuses specifically on how to perform the operations. 4 00:00:15,920 --> 00:00:19,575 Typically at a low level, taking the system it is running on into account. 5 00:00:19,575 --> 00:00:22,760 Declarative style focuses more on 6 00:00:22,760 --> 00:00:25,340 what specifically you are attempting to accomplish. 7 00:00:26,710 --> 00:00:30,520 The imperative style is definitely more difficult to read as there are more lines 8 00:00:30,520 --> 00:00:35,140 of code and declarative is usually very clear as to what is happening. 9 00:00:35,140 --> 00:00:38,140 Let's walk through an example one that you've probably already seen and 10 00:00:38,140 --> 00:00:39,590 it will help demonstrate the differences. 11 00:00:41,210 --> 00:00:44,350 For those of you with people in your lives that have food allergies, 12 00:00:44,350 --> 00:00:48,160 you know how difficult it is to find something that they can eat. 13 00:00:48,160 --> 00:00:50,010 My daughter has an egg allergy. 14 00:00:50,010 --> 00:00:50,810 It's a bummer. 15 00:00:50,810 --> 00:00:51,900 It's pretty horrible. 16 00:00:51,900 --> 00:00:54,800 It seems like everything has eggs in it. 17 00:00:54,800 --> 00:00:56,600 Cupcakes, taffy, ice cream. 18 00:00:56,600 --> 00:00:59,070 You know, everything a kid likes to eat. 19 00:00:59,070 --> 00:01:01,780 Now as a parent, I am constantly needing to check 20 00:01:01,780 --> 00:01:05,860 a list of ingredients to see if it indeed contains eggs. 21 00:01:05,860 --> 00:01:08,970 Let's write a quick simulation of this in code, in both styles. 22 00:01:10,040 --> 00:01:13,650 So here I have the current version of IntelliJ IDEA open and 23 00:01:13,650 --> 00:01:15,740 I'm just going to make a new project. 24 00:01:15,740 --> 00:01:18,410 I'm gonna click create new project. 25 00:01:18,410 --> 00:01:19,400 Cool. And I'm gonna just leave 26 00:01:19,400 --> 00:01:20,520 the defaults here. 27 00:01:20,520 --> 00:01:24,460 I'm gonna click next and I do want to just make a quick command line. 28 00:01:26,750 --> 00:01:29,070 Let's go ahead and call this exploration. 29 00:01:31,480 --> 00:01:33,720 Okay so that's set everything up for us. 30 00:01:33,720 --> 00:01:37,250 Let's go ahead and let's start write our code here and suggest it. 31 00:01:39,430 --> 00:01:42,870 Oops I indent with two space I'm using the Google style. 32 00:01:42,870 --> 00:01:48,890 So we'll say list the string and those are ingredients, right? 33 00:01:50,190 --> 00:01:53,665 And we'll make a new Arrays.asList. 34 00:01:53,665 --> 00:01:59,220 And we'll just go ahead and we'll enter those guys, that's what we want. 35 00:01:59,220 --> 00:02:03,140 So let's see, we have flour, what else do we have? 36 00:02:03,140 --> 00:02:04,345 We've got some salt. 37 00:02:06,360 --> 00:02:11,216 Delicious cupcakes here, some baking powder, butter, 38 00:02:11,216 --> 00:02:16,480 I don't know how to cook really, eggs and here comes the milk. 39 00:02:16,480 --> 00:02:20,760 All right, and we will close off with a semicolon here. 40 00:02:22,130 --> 00:02:25,650 Okay, so let's solve our issue of checking whether or 41 00:02:25,650 --> 00:02:28,640 not our ingredients has eggs, okay. 42 00:02:28,640 --> 00:02:30,630 I'm gonna go ahead and give us more space. 43 00:02:30,630 --> 00:02:33,280 Get rid of all the windows that are open, here we go. 44 00:02:33,280 --> 00:02:35,095 Okay, so first things first. 45 00:02:35,095 --> 00:02:38,010 We'll need a variable to store our state, right? 46 00:02:38,010 --> 00:02:42,040 So we'll wanna say boolean hasEggs, and we'll just go ahead, and 47 00:02:42,040 --> 00:02:42,820 we don't know yet. 48 00:02:42,820 --> 00:02:45,880 We don't know if it has, and so we said false. 49 00:02:45,880 --> 00:02:49,140 And now, what we wanna do is we wanna loop through each of those ingredients. 50 00:02:49,140 --> 00:02:51,910 So we can do that by starting to define a for loop, right? 51 00:02:51,910 --> 00:02:56,010 So we'll say for and now we need another one of those temporary 52 00:02:56,010 --> 00:02:58,930 variables to store in the loop where we're gonna currently process. 53 00:02:58,930 --> 00:03:05,800 So we'll say int i for index, kind of standard thing there, right? 54 00:03:05,800 --> 00:03:09,470 And we'll make another temporary variable, right? 55 00:03:09,470 --> 00:03:14,110 So now we've got 2, we've got hasEggs and i, two temporary variables. 56 00:03:14,110 --> 00:03:16,790 And now we wanna make sure that we go through all of the ingredients. 57 00:03:16,790 --> 00:03:20,970 So we wanna see if i is less than the ingredients. 58 00:03:20,970 --> 00:03:24,730 And we wanna see if it's as long as it is which was size. 59 00:03:24,730 --> 00:03:27,160 And we've gotta remember that it's zero-based, right? 60 00:03:27,160 --> 00:03:29,340 It starts at zero, so it's gotta be less, 61 00:03:29,340 --> 00:03:32,170 not less than or equal to, always have to think about that there. 62 00:03:32,170 --> 00:03:34,118 And then we're gonna go ahead and we're gonna increment. 63 00:03:35,980 --> 00:03:38,872 Then we need to check if that specific ingredient, so 64 00:03:38,872 --> 00:03:41,590 what we need to do is we need to pull it out, right? 65 00:03:41,590 --> 00:03:42,810 We need to pull that one out. 66 00:03:42,810 --> 00:03:48,690 So we'll say String ingredient equals ingredients.get, and 67 00:03:48,690 --> 00:03:52,068 we're gonna pass in that index that we've been running through the loop with, right? 68 00:03:52,068 --> 00:03:59,260 Okay so then we wanna see does this ingredient that we pulled out, 69 00:03:59,260 --> 00:04:04,370 does that equal eggs, right? 70 00:04:06,110 --> 00:04:09,750 Okay, and if it does we will for sure. 71 00:04:09,750 --> 00:04:12,530 We'll say hasEggs, we're gonna set that to true. 72 00:04:13,890 --> 00:04:16,720 You know what since we did know it hasEggs, 73 00:04:16,720 --> 00:04:18,300 we don't need to look at the other stuff right? 74 00:04:18,300 --> 00:04:20,360 We can finish here, so we can just go ahead and say break. 75 00:04:23,430 --> 00:04:25,490 Let's look at this code here for a second. 76 00:04:25,490 --> 00:04:30,910 That's a lot of code and brainpower needed just to understand what's happening. 77 00:04:30,910 --> 00:04:34,120 Now we've done this a lot and probably see code like this quite a bit. 78 00:04:34,120 --> 00:04:37,630 So it's probably feeling kind of familiar. 79 00:04:37,630 --> 00:04:40,130 But just imagine for a second if you hadn't. 80 00:04:40,130 --> 00:04:44,820 There's a whole lot of what is this doing going on, right. 81 00:04:44,820 --> 00:04:50,380 So now that we have our variable, we can go ahead and see if it has eggs. 82 00:04:50,380 --> 00:04:54,490 Gotta break the news to my daughter and say, sorry sweetheart. 83 00:04:56,950 --> 00:04:57,450 It has eggs. 84 00:04:58,910 --> 00:04:59,410 Sad face emoji. 85 00:05:00,930 --> 00:05:04,110 Now of course there's been some evolution in looping. 86 00:05:04,110 --> 00:05:07,480 So we can of course clean up this cognitive overload a little bit of all 87 00:05:07,480 --> 00:05:08,220 these things, right? 88 00:05:08,220 --> 00:05:11,851 So we can, we can just go ahead we can say for 89 00:05:11,851 --> 00:05:17,260 the ingredients in ingredients, right? 90 00:05:17,260 --> 00:05:19,230 This enhanced for loop. 91 00:05:19,230 --> 00:05:24,350 So we can lose this index cause each one is gonna have a specific time, right? 92 00:05:24,350 --> 00:05:26,110 We don't need to know how long the list is anymore. 93 00:05:27,540 --> 00:05:31,470 This reads nicer, but it's still an awful lot 94 00:05:31,470 --> 00:05:36,700 when all we really want to know is, does ingredients contain eggs. 95 00:05:36,700 --> 00:05:39,060 We're still saying give me each of the ingredients and 96 00:05:39,060 --> 00:05:40,740 I'll tell you when to stop. 97 00:05:40,740 --> 00:05:44,730 So the more declarative way to ask this question, 98 00:05:44,730 --> 00:05:47,530 is just to ask the question, right? 99 00:05:47,530 --> 00:05:49,070 So, let's do this. 100 00:05:49,070 --> 00:05:53,023 If ingredients.contains, 101 00:05:55,252 --> 00:05:59,911 Eggs, Right? 102 00:05:59,911 --> 00:06:00,810 We don't need this. 103 00:06:01,930 --> 00:06:03,530 And there's nothing new here. 104 00:06:03,530 --> 00:06:07,320 Contains has been around since the collections framework was introduced. 105 00:06:07,320 --> 00:06:09,190 But it's declarative. 106 00:06:09,190 --> 00:06:12,160 Look at how it explains what it is that we're trying to do. 107 00:06:12,160 --> 00:06:14,000 It's not specifying how. 108 00:06:14,000 --> 00:06:17,660 Now this can be optimized efficiently behind the scenes. 109 00:06:17,660 --> 00:06:20,160 And this is how it works in the real world, right. 110 00:06:20,160 --> 00:06:23,760 If I'm at a restaurant, and I wanna know about a possible cupcake for 111 00:06:23,760 --> 00:06:28,600 my daughter, I don't go, can you please tell me every ingredient in this cupcake. 112 00:06:28,600 --> 00:06:30,670 Now don't worry I tell you when to stop listing them and 113 00:06:30,670 --> 00:06:33,590 the case that I happen to hear what I'm concerned about, right. 114 00:06:33,590 --> 00:06:36,490 I simply say, does this contain eggs? 115 00:06:36,490 --> 00:06:39,940 And the waiter or waitress has their own method of knowing. 116 00:06:39,940 --> 00:06:43,490 How they get the answer isn't really something I need to concern myself with. 117 00:06:43,490 --> 00:06:47,150 I'm really just concerned with the answer to my question. 118 00:06:47,150 --> 00:06:48,560 So ready for it? 119 00:06:48,560 --> 00:06:51,390 This is the start of the shift of your thinking. 120 00:06:51,390 --> 00:06:53,660 Functional programming is declarative. 121 00:06:53,660 --> 00:06:56,580 It'll take a little bit of shifting in you programming mind, but 122 00:06:56,580 --> 00:06:57,820 we do it in real life. 123 00:06:57,820 --> 00:07:02,570 So it should feel more normal than how we already force our imperative mind to work. 124 00:07:03,820 --> 00:07:07,220 One more example of how this looks in something you've probably seen. 125 00:07:07,220 --> 00:07:10,070 In SQL or Structured Query Language. 126 00:07:10,070 --> 00:07:11,560 It's all declarative. 127 00:07:11,560 --> 00:07:15,680 Even if you haven't seen SQL before, this is how you write the statements. 128 00:07:15,680 --> 00:07:18,190 The language allows you to declare 129 00:07:18,190 --> 00:07:21,040 very specifically what you would like to get back. 130 00:07:21,040 --> 00:07:25,090 In this case, it's some specific fields like first name, last name, and email. 131 00:07:25,090 --> 00:07:28,510 For a student with the id of 123. 132 00:07:28,510 --> 00:07:32,010 Notice how it doesn't specify how. 133 00:07:32,010 --> 00:07:35,940 This allows the database to do whatever indexing or 134 00:07:35,940 --> 00:07:40,800 caching it needs on whatever platform that implements the language specification. 135 00:07:42,490 --> 00:07:46,010 Let's stay in the declarative mindset and introduce our first function.