This course has been updated! We now recommend you take the Hardcore Functional Programming in JavaScript, v2 course.
Transcript from the "The Silence: Review" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Joe Nelson: When the stuff is really pure, often loops are reduce, filter, or map. If things aren't pure, you have to worry about what it's doing. You can't just always use it so happily. In fact, there's another list thing, each. But that's kind of the impure one that shouldn't be in the triangle.
[00:00:24] Do something stateful to each thing that you see, rather than map which is like, change it, pass it through change it like you know purely. So when you work with pure functions, these are the probably most of the loop things that you're gonna see. And, in fact, we like to say if you write loops, you get an F.
[00:00:42] Because, no loops.
>> Joe Nelson: Okay, so kind of reviewing the separations. In general, phase one was to prepare you to see that it's kind of like the basics. The type of stuff that's gonna to feed into what Brian shows you to write really cool stuff. So, the basics are keep the function arguments explicit, separate input from environment so you know exactly what's going into the function.
[00:01:09] Also, the inputs can be provided over time. So, separate a function from its. Make it be whatever it needs to be so that we could just throw stuff together and compos or split it like you saw. Don't be modifying outside things. Or when you do, keep it very localized, so there's one place that does it in the teasers.
[00:01:31] The teaser function was like both trying to figure out how to shorten something and updating the DOM. It's better to have something it's like, yeah, all I do is update the DOM. And I'm composed, in fact, and a way to think about it is you do your pure stuff, and it's sandwiched in between the impure stuff.
[00:01:46] So your pure stuff in the teasers was calculating, okay, I've got a string of words. What's a way to shorten it so it's an appealing thing someone wants to click into and it's not overwhelming? And it's sandwiched between reading the things in the DOM, like seeing that they're there or finding them, and putting them in, and on the other side updating the things in the DOM.
[00:02:07] ,if most of your program is composing pure, pure, pure, and only at the end do you ever sandwich it, then the things that appear are free to combine a lot more easily and compose. So, between the buns of the sandwich, you could rearrange all the layers nicely and freely.
[00:02:24] You could throw a log statement in between in your compose chain and see how things are changing. Even though it's impure, it just has a the debugging technique.
>> Joe Nelson: Minus one, so that makes me feel sad-
>> Off Camera 1: No, that was for [CROSSTALK] [LAUGH]
>> Joe Nelson: Okay, and then try to compose without using glue variables like we saw with the error thing.
[00:02:54] And if you do those things, you've omitted a lot of needless words, and your code is ready to enter from the silence to the voyage that Brian's gonna to take you on. So I wonder if there's anything else we wanna cover? It's 11:45.
>> Off Camera 1: Yeah, do you wanna write some more compositions, or just take questions?
[00:03:17] Or, cuz people need to know this before we go into the second part, or you guys are going to be like we hate you. [LAUGH]
>> Joe Nelson: Yeah, yeah.
>> Off Camera 2: I did have a question on the challenge, too. I actually wrote it originally without the compose. I just did a return contains, and then I passed in, as a second argument, was the names.
[00:03:42] So, in other words, I didn't do the compose. So, what's good or bad? I mean-
>> Joe Nelson: It's probably better. You probably did a better imitation there.
>> Off Camera 1: Yeah, I think it's a good example to be like, well, compose works here, but if you have to put arguments in weird spaces, just write a function and just use the normal stuff.
[00:04:02] But, I mean, if we were, yeah, I think we should write an extra couple composes to show that, I mean, one of the benefits of compose is you don't have to name anything. So, there's no ties to this specific implementation. We're just saying glue this function with this function.
[00:04:19] And you're kind of, without thinking about it, being like writing this generic code that's building up a little functions into bigger functions. If you, the second you start to write function, and you start doing things in there, you're writing, you're telling JavaScript how to evaluate, do this first, then do this, then do this, and it can't be changed [INAUDIBLE].
[00:04:38] You also end up writing, making all these references to names and having to name things. And you might be doing things that aren't other functions that you're building up new functions from. You might just add one out of nowhere instead of using our add function. So, what it's doing is kind of forcing a higher level declarative discipline.
[00:04:57] And if you can't get it, you might write readable code. [LAUGH] Don't worry about it.
>> Off Camera 2: Well, do you worry about performance, then? Are you getting too many functions, and does that, as opposed to sometimes just doing it in a straightforward, JavaScript way?
>> Joe Nelson: It's funny to think about, to really think about performance and what affects it in a web application.
[00:05:20] It's so, probably not. [INAUDIBLE] on your JavaScript. Because huge network calls, it'll like an eternity compared to calling an extra function. Or like choices about caching went wrong. Or waiting for the server. It's just, I think it's good to think about it. I would, for a minor increase in efficiency, I would not wanna make a major increase in the complexity of my code.
[00:05:44] Assuming that I found a nice way to compose [INAUDIBLE] clean and better example, too.
>> Off Camera 1: Yeah, what if we-
>> Joe Nelson: Should we make some more compositions?
>> Off Camera 1: Yeah, if we go mess with those authors some.
>> Joe Nelson: Yeah, let's see.
>> Joe Nelson: Okay, what could we do with this thing?
[00:06:04] If it's a,
>> Joe Nelson: Aside from just plucking things out.
>> Off Camera 1: Yeah, look into Scott and Michael for some cool-
>> [INAUDIBLE]
>> Off Camera 1: Functions to play with. Maybe you could say, take one, right? To take one of the articles, and you cold compose that. Take one article and then get started.
[00:06:30]
>> Joe Nelson: I see. Take one article. So, get the first title?
>> Off Camera 1: Yeah, and composition working together.
>> Joe Nelson: Take, cool. Sure. I don't have to do a var. I can just do it, compose.
>> Joe Nelson: Actually, I'll do the other way around. So, we wanna get the
>> Joe Nelson: The title.
[00:06:58]
>> Joe Nelson: So this whole function is gonna get the first title of the thing.
>> Off Camera 1: I think so.
>> Joe Nelson: I think so, yeah.
>> Off Camera 1: [LAUGH]
>> Joe Nelson: Absolutely will, except it's undefined.
>> Off Camera 1: I mean, maybe they can tell us-
>> Joe Nelson: Maybe I did take wrong [INAUDIBLE].
>> Off Camera 1: Wait, yeah, check [INAUDIBLE].
[00:07:24]
>> Joe Nelson: All good.
>> Off Camera 1: _get, .get?
>> Joe Nelson: What is the, I should actually go to the articles.
>> Off Camera 1: Yeah. [LAUGH] I think it's the first one.
>> Joe Nelson: But it's actually still an array.
>> Off Camera 1: I see. So we get the head? Dropped out in the-
>> Joe Nelson: If you don't understand I could just do it
[00:07:56]
>> Off Camera 2: Was it like it's first instead of a take?
>> Off Camera 1: Yeah, yeah, that's true.
>> Multiple: [LAUGH]
>> Off Camera 1: Why will I take that? That is a same function.
>> Joe Nelson: So that's the same?
>> Multiple: Yeah.
>> Joe Nelson: That still seems to be like it's an array with one thing in it.
[00:08:09]
>> Off Camera 1: No, it's an object.
>> Joe Nelson: Is it? Object? Okay.
>> Off Camera 1: Yeah, let's do that ahead or-
>> Joe Nelson: Sure.
>> Joe Nelson: Cool, okay.
>> Multiple: [LAUGH]
>> Joe Nelson: I mean, this alternative, I don't know if this is such a cuz it'd be articles
[0] dash-
>> Off Camera 1: Well, so the, but the top one is, something else to consider is the top one is, if you're going through functions, we can start to, it, I'm gonna talk about in part two.
[00:08:42] We can to compose contexts like what if there was none? So, if you try to get the head, and there's nothing there, if we're doing, if we're composing functions, we're able to compose behaviors like null checks and stuff. So that'll-
>> Joe Nelson: That's true.
>> Off Camera 1: Get pretty wild. If you do this bottom one, you're locked into the, you're stuck, you're done.
[00:09:04] You have to wrap that in an if not null. If article is first, get a title.
>> Joe Nelson: Yeah, good point. I can't say how many time I've [CROSSTALK]
>> Multiple: [LAUGH]
>> Joe Nelson: Something's null. It's not in the thing and if you stick something in between, you'll be in a different context which will show you.
[00:09:16]
>> Off Camera 1: So, we're starting to do that, but one of the benefits right here is you're completely declarative. You don't say how anything happens. I guess the bottom one is kinda declarative, too.
>> Joe Nelson: But I think it's important, I think the biggest thing is what you said. This composes, and it composes in a way that pure code can be lifted into a container, like you're gonna explain with.
[00:09:33] A container can do null checks, so you're kind of doesn't have to care. It's just like I'm pretending everything's perfect, but I'm in this container that insulates me from nulls and stuff that can happen and does other things.
>> Off Camera 1: It's gonna be exciting.
>> Joe Nelson: Yeah.
>> Multiple: [LAUGH]
>> Joe Nelson: Yeah.
[00:09:49]
>> Off Camera 3: There's a couple questions from chat. One was whether currying is implemented in underscore or a low dash, or if they need to use Ramba.
>> Joe Nelson: I think curry is in both of them, that would be my gut instinct.
>> Off Camera 1: Yeah, I think it's in both.
>> Joe Nelson: It's a pretty standard thing.
[00:10:03] But the thing is, say that you choose to use underscore. It gives you curry, but the other functions it gives you don't have there arguments and a convenient order for you to be
>> Off Camera 1: Right.
>> Joe Nelson: So, but you get the curry.