Check out a free preview of the full The Good Parts of JavaScript and the Web course
The "Function Challenge 6" Lesson is part of the full, The Good Parts of JavaScript and the Web course featured in this preview video. Here's what you'd learn in this lesson:
In this challenge, you will write the gensymf(), gensymff(), and fibonaccif() functions.
Transcript from the "Function Challenge 6" Lesson
[00:00:00]
>> [MUSIC]
[00:00:03]
>> Douglas: Make a function gensymf that makes a function that generates symbols. So we're taking the generator idea and we're gonna try to do something practical with it now. So gensymf is a symbol generator or gensymf is a factory that makes symbol generators, or things that make serial numbers.
[00:00:26]
So we designate a serial number with a prefix, and so you can send it any string and that becomes the prefix string, and then we will get a series of strings starting with that symbol. So we're gonna make two generators this time from the same factory. We're gonna make the G series and the H series and when we call them, we'll get G1, H1, G2, H2.
[00:00:55]
>> Douglas: Let's look at gensymf,
>> Douglas: Gensymf takes a prefix string, it creates a number which it's gonna use for keeping track of where it is in the sequence. It will return a function which will add one to that number, and return the result of concatenating the number to the prefix.
[00:01:14]
So who got something that works? Good. Anyone do anything substantially different? Yeah.
>> Speaker 2: And you cast in, then it's prefix to the string, can you pass it in digit. Is that first thing?
>> Douglas: That's a wise precaution, yeah. Anybody else?
>> Speaker 2: I used find function.
>> Douglas: Use a find function that's going to.
[00:01:37]
So this is an example, we've seen a number of these where we've got a factory function which then makes something which will do some work, to get usually a generator, but it could be there are lots of different kinds of functions. But they're both functions, it's just one is nested in the other.
[00:01:55]
And in fact if we nest further, if we put another function outside of this, we could make a factory, factory. And you can wrap that with a factory, factory, factory. So just for fun, let's look at what this would look like if it were a factory, factory.
>> Douglas: So gensymff is the factory, factory and we're gonna pass the increment function and the initial seed value to gensymff in a produce of produces a function that works exactly like gensymf.
[00:02:27]
And it'll make the sequences right, and we've done things like this a couple of times already. So I'm just gonna show you the thing cuz we've already done this one. So it looks very similar to patterns we've seen before, where the factory, factory is supplying values that go into the generator.
[00:02:45]
And now, so we can automate the making of factories. Now, the interesting thing about this one is that statement there where we're creating the number variable which is gonna hold the value that is being used to generate the sequence numbers. So if we were to move that up one line, so that it's not in the factory anymore, but it's in the factory, factory.
[00:03:11]
That would change the visibility of that variable so it would be seen by all of the generators. So instead of generating G1, H1, G2, H2, we would generate G1, H2, G3, H4. That's a really interesting change in behavior. It's just moving one variable declaration one place to another.
[00:03:32]
So we've been dealing with closure and we can we saw that we can have things that are global and things that are local, and things that are sort of in between, but there can be more of those in betweens. And if we get into nesting things in useful ways, we have tremendous control over the visibility and the lifetime of the variables, and can do interesting things to affect their behavior.
[00:04:03]
How about that, any questions about that? Ready for another one or do you want to take a break? Let's do another one, sure why not. So everybody remembers fibonacci, right? You did fibonacci numbers in school. So fibonacci was an important mathematician. He discovered a lot of good stuff, but the only thing we seem to remember is the fibonacci sequence.
[00:04:30]
In their infinite number of Fibonacci Sequences, but mostly we only remember the famous one, which started with 0 and 1, and that's what we're gonna be doing now. So we're gonna make a factory which will make a Fibonacci generator. And you'll seed the factory with the first two numbers in the sequence.
[00:04:50]
So everybody remember how Fibonacci numbers work? Well let's review. So the Fibonacci sequence will be a sequence of integers, you specify the first two integers in the sequence, the third number will be the sum of the first two. The fourth number will be the sum of the previous two and so on.
[00:05:14]
So the first numbers we get are 0 and 1, because those are the ones we provide. Then the next 1, will be 1 because that's the sum of 0 and 1, the next one will be 2, because it's the sum of 1 and 1. The next will be 3, cuz it's a sum of 1 and 2, next be 5 because it's the sum of 2 and 3, and the next in the series will be?
[00:05:34]
8, exactly. So this was a tricky one, wasn't it? The Fibonacci sequence itself is totally trivial. It's three, it's simple statements, but getting the first two numbers to come out but that was the trick. So first off you got something that works? Like graduations this one was hard, so let’s look at a number of approaches that we could take.
[00:06:02]
So here's one, there is the Fibonacci function there in the box. So that's it, and then we've got an if statement around it versus which statement which asks, where are we in the sequence? At the first at the beginning of sequence put out the first number, otherwise put out the second number, otherwise use the Fibonacci function and do that.
[00:06:28]
And this works, it absolutely works, who took this approach or something like it maybe used and if instead, but basically. Yeah, you've got a variable which is telling you where you are in the sequence. This is a completely, yeah?
>> Speaker 3: Except for case one, you're inputting a and b.
[00:06:47]
A and b don't necessarily equal 0 and 1, if a is something other than 0 for case one.
>> Douglas: Right.
>> Speaker 3: You need to return a+ b, don't you?
>> Douglas: No.
>> Speaker 3: You don't know the rule.
>> Speaker 2: You're switching on i.
>> Douglas: I got i which is telling me where I am in the sequence.
[00:07:07]
>> Speaker 3: I know.
>> Douglas: Which isn't the value I'm at the sequence just how many numbers have I looked at.
>> Speaker 3: I know.
>> Douglas: So if I'm looking at the first number, I output a, and if I look at the second number i is 1, I output b. Otherwise I output a + b.
[00:07:23]
>> Speaker 3: So if you want the Fibonacci, if your starting elements are 5 and 7, the first number's 5, second number's 7, third number's 12.
>> Douglas: Right.
>> Speaker 3: Okay.
>> Douglas: Yeah, that's how they work.
>> Speaker 3: Yeah, I was thinking that you added the first. First, you took the second then you added the first two.
[00:07:41]
Then you, no, it's not until you get to the-
>> Douglas: It's the first two numbers, then you add.
>> Speaker 3: Right, because they tell you, got it.
>> Douglas: Yeah.
>> Speaker 3: Is y incremented or not?
>> Douglas: Yeah, well instead of adding 1, I set it to 1 because I know it's 0.
[00:07:59]
And beyond that I don't need to increment it, once I get past the first two cases, I don't care what i is anymore.
>> Speaker 3: Okay.
>> Douglas: Okay, so completely acceptable this is a reasonable way to do it. I would argue that a reasonably intelligent person could figure out what this code is doing and that's most of what we want code to do so.
[00:08:24]
This is good, it's okay. My complaint with it is, it's a fairly big function and only that much of it is concerned with the Fibonacci things, so it just feels kind of loopsided to me. So here's another approach. In this one, I kind of permuted the statements of the Fibonacci sequence in order to delay the output of the first one, or to cause the first two numbers to get output.
[00:08:51]
So who did something like this? Yeah, so this is probably the most optimal solution. It's gonna be the smallest code, the fastest performance not that either of those matter in real life, but it does have that advantage. The disadvantage of this is I'd hate to be the guy that has to debug it, right?
[00:09:11]
>> Speaker 4: I said b = a + next instead.
>> Douglas: Yeah, there are variations on it, but it's basically the same idea. So another approach we could take is recognizing that we're making a generator and we already have some tools for constructing generators. So here's another approach, I've got my Fibonacci generator here, which will give me the next number, I just need to get the first two on top of it.
[00:09:43]
So I'm going to make a special generato, and I'm gonna start by taking identityf which was the first interesting function that we wrote this morning, that you thought had no practical application. It turns out what identifyf is is a constant generator, it will always produce the same value.
[00:10:02]
So I'm gonna use that to make generators and then I'm gonna use the limit function that we wrote earlier to cut off the sequence, so I only get one. So I got a pair of things and I can then concatenate those two together and then concatenate that on to the Fibonacci function.
[00:10:21]
So who did that? [LAUGH] Of course nobody would do that.
>> Speaker 4: [LAUGH]
>> Douglas: And if you're, yeah, so there's that. And if I were gonna be doing this a lot, I would take limit identityf, and encapsulate that into something which would make sequences of one more compactly or we could do this.
[00:10:48]
A similar thing, we take the element function that we wrote earlier make an array containing the first two things and concatenate that onto the Fibonacci generator. So who did that? No, no.
>> [INAUDIBLE]
>> Douglas: Very, very good, brilliant.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops