Pure Functions4:06 with Craig Dennis
A key tenet is making sure that your function is pure. Let's explore what that means more broadly.
[MUSIC] 0:00 All right, let's start unparking our terms. 0:04 I'm gonna pop up our parking lot document here. 0:07 All right, and the first one here is pure functions. 0:10 So we talk about the importance of pure functions in 0:13 the very beginning of this course. 0:16 And we talked about how the requirement for 0:17 them actually spawned the imperative movement. 0:19 Let's review what a pure function means now that we've got some experience writing 0:22 functions. 0:26 So the key feature to understand about a pure function is this. 0:27 Given the same input, a pure function will always produce the same output. 0:31 So, let's think about that a bit more concretely. 0:37 So you know the functional shape function, right? 0:40 It accepts a value, it processes it, and it returns a value. 0:42 As long as every value that I pass in returns the same result, 0:47 we can meet the first criteria of being pure. 0:51 So, let's say that we have a function that creates silly nicknames. 0:54 It takes a name and makes a new nickname for it. 0:57 So it takes a name, and 1:01 returns a new string that has gone through a specific transformation. 1:02 So if we were to actually call or 1:06 apply this function, we'd see that it returns Javay McJavaface. 1:08 And here's the thing with that code, 1:14 if I pass in Java it's always going to return Javay McJavaface. 1:16 That's what they use to call me in high school by the way. 1:21 No matter what I pass in to that function, I'll get back the same result for 1:24 that particular input. 1:28 In fact, if I could actually just replace all calls to the nicknamer function with 1:29 the parameter of Java, with the string Javay McJavaface, 1:34 it would run the exact same. 1:38 This is another way of saying that the function is referentially transparent. 1:41 Now because of this referential transparency, and 1:46 again, that means it returns the same result when called with the same argument. 1:49 Oftentimes, the replacing actually happens. 1:54 I mean, why run that code again, right? 1:56 Storing the results from a specific function called is called memoization. 1:59 The value that is stored is said to be memoized. 2:04 But here's the thing, if I end up wanting to make this nickname function a little 2:08 more fun, I'd probably end up wanting to add a bit of randomness, right? 2:12 Maybe I'd use a random number generator to pick from a list, so 2:17 that I could make a random nickname, like Javameister or Javatron. 2:21 But that would break the purity of the function, 2:25 because it wouldn't always return the same value, therefore it couldn't be memoized. 2:27 The function would no longer be pure. 2:32 Or maybe we would want to have it return a new nickname every hour. 2:35 If we use the current date to determine that inside of the function body, 2:39 we'd also make the function no longer pure. 2:44 What a bummer, right? 2:47 You wanna be able to perform this randomness, but 2:49 you also want the ability to take advantage of referential transparency and 2:51 all the speed enhancements that that can bring. 2:56 So can you work around this limitation? 2:59 Let's think about it for a second. 3:01 What was that rule again? 3:03 What if we create a randomness outside of the function and 3:05 provide it as input to the function. 3:09 And inside the function, we only use that value that was passed in. 3:12 That would keep things always the same in the output. 3:16 So the rule would hold true. 3:19 Although, outside the scope of this course, this is the basis for 3:22 understanding the important functional concept that is known as the state Monad. 3:26 Check the teacher's notes for more. 3:31 The more you explore the functional programming world, 3:33 the more you're going to hear talk about pure functions. 3:36 I'm glad we got the concept of referential transparency covered. 3:39 We've written a lot of functions in this course and 3:43 almost all of them have been pure. 3:46 Now, I say almost because we end up breaking another rule of pure functions, 3:48 quite a bit, and that is this one. 3:52 Produce no side effects. 3:54 So a side effect is a bit of a loaded word in English and 3:57 it often leads to unnecessary confusion. 4:00 Let's take a quick break and then come back to discuss. 4:03
You need to sign up for Treehouse in order to download course files.Sign up