Now that Kyle has finished demonstrating the solution to exercise 1, he spends a few minutes answering some audience questions.
(Asian fusion music) - Saw a couple of hands, yeah. - [Student] Wouldn't you have 175? - Sorry, I typed it wrong. The question was when I have 175. I just completely mistyped. Yes, the end result would be 175. Was that your question as well? - [Student] Yeah, I just want to make sure I got the right answer. - I have not had enough coffee, apparently, I can't do my math this morning, okay? I said 175 and typed 125. Yes. - There was a question.
- [Student] Is it possible for an app to have all of their functions pure? - Well, we could quibble about that. I would say in general, the takeaway would be if your program was entirely pure with no side-effects that would mean your programer would have no input or output and if no input or output, than your program would be completely pointless. If it had no effect on anything, if there was no observation of it, there was no state change as a result of your program, then I know what you can do with that program.
You can simply delete it, 'cause that's about how useful it is. That's the glib way of saying, you're going to have to have balance where you need purity and where you don't. And you'll notice, again, I didn't start this workshop to say, functional program, functional, do all functional. Functional is cool, but it's not the hammer that you use for everything. The perspective that we're taking here is, there is some usefulness to it and there are some places in our program where it will make it easier to reason about. So, I don't encourage you to write your own entire program in a functional style.
I've never once done that. But, I have used some of these functional concepts in my regular programs and in the places that I've done it's improved the understandability, the reasonability, of that code. Good question online, thank you. Okay, I see a question in the chatroom. "Where would you use all these pure functions if you were using frameworks like Angular and such?" My answer to this is not going to be terribly satisfying, but I'm not goinna to answer any questions about frameworks because that's way outside of the scope of what we want to talk about here.
I'm not an Angular expert. There's plenty of people that are very smart about Angular and I would recommend if you're doing Angular programming, embrace the Angular way, okay? Because Angular has a way of doing it. If you're doing React, embrace the React way. My goal is to really sit at a lower level than frameworks and to talk about the mechanisms of the language itself and how we can use those mechanisms. So, I think that it's entirely possible that you might be able to improve some of your Angular code with some functional-like programming concepts.
But, the chances are you're not going to be able to do a full functional programming pass and still use Angular, 'cause that's just not the mindset that they use to design that framework. The same would be true of React and Ember and Backbone and all of that. That's one of the reasons why I'm teaching this workshop by the way, is I'm trying to make some of this stuff more palatable even in mixed environments where you know you're not going to be able to go entirely functional programming. These are little tiny tools that we can use in very tactical specific ways. Yes? - [Student] We're kind of getting a few here so-- - Okay great.
- [Student] So bear with me. Is it best to return an array? What else could we return? What other way could return Y and Z to keep the function pure? Sorry. - You're going to have wrap it in some container the two fundamental containers that we have in Java script are going to be the array and the object. So, we could've gone to the trouble of assigning several different values into properties in an object. Usually, functional programmers like arrays. They like lists because lists are very convenient to do operations on.
So, my instinct in a functional programming mindset, or something that's even remotely close to functional programing, my instinct is to reach for the array before I reach for the object. But, any kind of container would do. I mean, even a more sophisticated container like a map or a set or something if you wanted to. You're going to have to have some container because syntactically the language only allows you to return a single value. If you need to return multiple values. Another way of answering that, I guess, is to say refactor your program, so you don't have to return multiple values. But, that may not be something you can use as general advice.
So, when you do have to have multiple side-effects, you're going to need some container with a return queuer. - [Student] Can you repeat what a pure function is? - Yes. A pure function is a function that has no side-effects. It operates entirely on it's own variables, it's own state, or any of the things passed into it. So, the arguments that are passed in and any of it's own, it operates entirely on that and does not change anything. A pure function does not mean it doesn't access outside state, it means it doesn't change the outside state.
So, there's no side-effects. So, I could write a function that acts as a variable outside of itself, and that's totally okay. And that's not going to be considered, at least from our perspective for our intensive purposes, that's not going to be considered an impure function. Except for the fact that, that variable now can change from out underneath this function. So, the overall reasonability of the program is less pure. The function itself we would still called pure 'cause it does not have a side effect. So, if you take precautions to make sure that you do not do side-effects in a particular piece of your program, then that piece of your program is pure.
- [ Student] On this program, I get "ZS undefined" or something? - Well, here we're not-- So, the question was: "Z is undefined." The way I rewrote this, I'm not using Z anymore externally. Remember, Z is only being used internally, that was part of the point. Z, was a set of state that we didn't want to change on the outside, so we encapsulated it as a local variable inside of bar. We do use it for the brief period of time between when we declare it and while foo is running, it's changing, and then we return it. So, we do have it there, but it's not exposed in the outside world.
That's intentional. - [ Student] This might have been, this is out of order but, they were asking, "Why do we return Z, if it's not being passed in and changed?" But, I don't think we are anymore, right? - Okay, so, that's actually a very good question. We are returning Z, we're not passing Z in. Z is not an input, Z is an output. Z is how I observe the output of this particular program.
If I didn't give you Z, it wouldn't be a terribly useful program, would it? So this, again, we're not going to get way deep into the weeds with functional programming but this is one of the ways that you can think about as a functional programmer. Even doing side-effects without side-effects, is to simply pass in everything that's needed and return everything back that's needed to be observed. Pass in the inputs, receive the outputs. So here, one of the direct outputs is the Z variable and one of the side-effect outputs, one of the state that we may or may not choose to track and use later, is that Y variable.
So, I'm returning both of those and that's why I need some container around it. We good? Alright, excellent, those were some fantastic questions. Thanks very much for that. That's exactly the kind of thing, that we want to pay our attention to.
This course was created by Frontend Masters. It was originally released on 03/08/2016. We're pleased to host this training in our library.
- Pure functions
- Manual composition
- Composition utility
- List operations