1 00:00:00,005 --> 00:00:04,583 [MUSIC] 2 00:00:04,583 --> 00:00:07,330 All right, let's start unparking our terms. 3 00:00:07,330 --> 00:00:10,030 I'm gonna pop up our parking lot document here. 4 00:00:10,030 --> 00:00:13,420 All right, and the first one here is pure functions. 5 00:00:13,420 --> 00:00:16,070 So we talk about the importance of pure functions in 6 00:00:16,070 --> 00:00:17,550 the very beginning of this course. 7 00:00:17,550 --> 00:00:19,962 And we talked about how the requirement for 8 00:00:19,962 --> 00:00:22,778 them actually spawned the imperative movement. 9 00:00:22,778 --> 00:00:26,504 Let's review what a pure function means now that we've got some experience writing 10 00:00:26,504 --> 00:00:27,810 functions. 11 00:00:27,810 --> 00:00:31,790 So the key feature to understand about a pure function is this. 12 00:00:31,790 --> 00:00:36,180 Given the same input, a pure function will always produce the same output. 13 00:00:37,180 --> 00:00:40,240 So, let's think about that a bit more concretely. 14 00:00:40,240 --> 00:00:42,930 So you know the functional shape function, right? 15 00:00:42,930 --> 00:00:47,500 It accepts a value, it processes it, and it returns a value. 16 00:00:47,500 --> 00:00:51,450 As long as every value that I pass in returns the same result, 17 00:00:51,450 --> 00:00:54,370 we can meet the first criteria of being pure. 18 00:00:54,370 --> 00:00:57,710 So, let's say that we have a function that creates silly nicknames. 19 00:00:57,710 --> 00:00:59,940 It takes a name and makes a new nickname for it. 20 00:01:01,030 --> 00:01:02,390 So it takes a name, and 21 00:01:02,390 --> 00:01:06,770 returns a new string that has gone through a specific transformation. 22 00:01:06,770 --> 00:01:08,900 So if we were to actually call or 23 00:01:08,900 --> 00:01:13,060 apply this function, we'd see that it returns Javay McJavaface. 24 00:01:14,310 --> 00:01:16,060 And here's the thing with that code, 25 00:01:16,060 --> 00:01:21,670 if I pass in Java it's always going to return Javay McJavaface. 26 00:01:21,670 --> 00:01:24,120 That's what they use to call me in high school by the way. 27 00:01:24,120 --> 00:01:28,447 No matter what I pass in to that function, I'll get back the same result for 28 00:01:28,447 --> 00:01:29,901 that particular input. 29 00:01:29,901 --> 00:01:34,831 In fact, if I could actually just replace all calls to the nicknamer function with 30 00:01:34,831 --> 00:01:38,404 the parameter of Java, with the string Javay McJavaface, 31 00:01:38,404 --> 00:01:40,130 it would run the exact same. 32 00:01:41,570 --> 00:01:46,610 This is another way of saying that the function is referentially transparent. 33 00:01:46,610 --> 00:01:49,560 Now because of this referential transparency, and 34 00:01:49,560 --> 00:01:54,645 again, that means it returns the same result when called with the same argument. 35 00:01:54,645 --> 00:01:56,770 Oftentimes, the replacing actually happens. 36 00:01:56,770 --> 00:01:59,600 I mean, why run that code again, right? 37 00:01:59,600 --> 00:02:04,490 Storing the results from a specific function called is called memoization. 38 00:02:04,490 --> 00:02:08,126 The value that is stored is said to be memoized. 39 00:02:08,126 --> 00:02:12,484 But here's the thing, if I end up wanting to make this nickname function a little 40 00:02:12,484 --> 00:02:17,390 more fun, I'd probably end up wanting to add a bit of randomness, right? 41 00:02:17,390 --> 00:02:21,030 Maybe I'd use a random number generator to pick from a list, so 42 00:02:21,030 --> 00:02:25,027 that I could make a random nickname, like Javameister or Javatron. 43 00:02:25,027 --> 00:02:27,500 But that would break the purity of the function, 44 00:02:27,500 --> 00:02:32,120 because it wouldn't always return the same value, therefore it couldn't be memoized. 45 00:02:32,120 --> 00:02:34,190 The function would no longer be pure. 46 00:02:35,810 --> 00:02:39,790 Or maybe we would want to have it return a new nickname every hour. 47 00:02:39,790 --> 00:02:44,200 If we use the current date to determine that inside of the function body, 48 00:02:44,200 --> 00:02:46,700 we'd also make the function no longer pure. 49 00:02:47,890 --> 00:02:49,452 What a bummer, right? 50 00:02:49,452 --> 00:02:51,800 You wanna be able to perform this randomness, but 51 00:02:51,800 --> 00:02:56,240 you also want the ability to take advantage of referential transparency and 52 00:02:56,240 --> 00:02:57,990 all the speed enhancements that that can bring. 53 00:02:59,010 --> 00:03:00,720 So can you work around this limitation? 54 00:03:01,720 --> 00:03:03,180 Let's think about it for a second. 55 00:03:03,180 --> 00:03:04,100 What was that rule again? 56 00:03:05,582 --> 00:03:09,510 What if we create a randomness outside of the function and 57 00:03:09,510 --> 00:03:12,210 provide it as input to the function. 58 00:03:12,210 --> 00:03:16,500 And inside the function, we only use that value that was passed in. 59 00:03:16,500 --> 00:03:19,870 That would keep things always the same in the output. 60 00:03:19,870 --> 00:03:21,090 So the rule would hold true. 61 00:03:22,370 --> 00:03:26,630 Although, outside the scope of this course, this is the basis for 62 00:03:26,630 --> 00:03:31,590 understanding the important functional concept that is known as the state Monad. 63 00:03:31,590 --> 00:03:33,570 Check the teacher's notes for more. 64 00:03:33,570 --> 00:03:36,000 The more you explore the functional programming world, 65 00:03:36,000 --> 00:03:39,030 the more you're going to hear talk about pure functions. 66 00:03:39,030 --> 00:03:42,195 I'm glad we got the concept of referential transparency covered. 67 00:03:43,260 --> 00:03:46,320 We've written a lot of functions in this course and 68 00:03:46,320 --> 00:03:48,360 almost all of them have been pure. 69 00:03:48,360 --> 00:03:52,600 Now, I say almost because we end up breaking another rule of pure functions, 70 00:03:52,600 --> 00:03:54,970 quite a bit, and that is this one. 71 00:03:54,970 --> 00:03:57,405 Produce no side effects. 72 00:03:57,405 --> 00:04:00,452 So a side effect is a bit of a loaded word in English and 73 00:04:00,452 --> 00:04:03,680 it often leads to unnecessary confusion. 74 00:04:03,680 --> 00:04:05,900 Let's take a quick break and then come back to discuss.