Polyglot Programming: TypeScript, Go, & Rust Fissures TypeScript Solution
Transcript from the "Fissures TypeScript Solution" Lesson
>> All right, so let's start off with TypeScript. But before we do, everybody if you're joining along, grab the input. Obviously, quintessential for solving the problem is input. All right, so I'm gonna go back to TypeScript, go to project, and I'm gonna create a p2 in my TypeScript project.
[00:00:15] I'll let everyone get there for a moment, and we're gonna do the exact same thing, of course, which is function getInput. Is that a Pinterest input? A string, return, paste that all in. What is the thing called? The grave, that's called the grave, right? What do you guys call that symbol?
[00:00:34] Yeah, look at that, it's a grave. Cuz that just sounds super exciting. They call me the grave-digger. I'm just out here just putting graves in. All right, so hopefully everyone is about right here. At this moment, I'll give you guys just one more second to get here. And we're gonna try to do this again in a fun way, kind of taking advantage of some more of the TypeScript ways to do it.
[00:00:58] So, let's first create a struct that represents a point, right? Cuz that's really what we have, we have two points. So I'm just gonna go type point with an equal sign, that always just emotionally hurts me. Does that hurt anybody else? Why is interface no equal sign? Why is inter, then type just, it has Okay, we can get back into this.
[00:01:21] Here we go. All right, so let's have an x, it's just a number, y, it's just a number. So there we go, we have our point. So, let's create a function called isStraight right? Our function is up wrong language, Straight, right? And it's gonna take in simply a point, and a point, right?
[00:01:43] We could do that, right? We can say, hey, is this thing Straight? Well, let's not do that. Let's actually create another type, and we'll call it Line. And a Line consists of two points, right? Does that make sense? And then so our isStraight will literally just have a line.
[00:01:57] Is this line straight? All right, so we're kind of creating the types for what we're about to parse. I think that that's probably a pretty straightforward way to approach this. First think of your types. Then we're gonna just jump back in and solve this damn problem. All right, so some people are asking, [LAUGH] which I did see, which is actually kind of thing.
[00:02:15] When I say isStraight, What do I mean by that? Is it horizontal or vertical? So you actually are correct. I should probably say something more like, is Horizontal or Vertical. It's just there's so many letters there. So, if everyone just can be okay with this, we could do that as well.
[00:02:31] There you go. I'm just not gonna have a function that's 30 characters long, okay? Yes, anything between two points is Straight, technically. So it needs to be Horizontal or Vertical. All right, so let's just program that up really quickly, should be easy. A p1.x = = = line.
[00:02:48] [LAUGH] Almost hit that, that double equals there. That's pretty dangerous. So let's put a little o sign, copy that over. Of course do the old x/y, put that. Put it in all, can fit on one line, beautiful. So there we go. We have this nice, either, that actually wasn't beautiful.
[00:03:09] So either p1x equals p2x, or p1y equals p2y. There we go, wonderful, right? So now, let's do the next thing. Let's parse a point, right? So I want it like this, ParsePoint. And this thing is simply gonna take a line that is a string. Do we wanna call it line?
[00:03:31] I mean, we can technically call it a point. I think we're real with ourselves. But then I can't use that term. So I'm gonna call it p. So I can call this thing a point, right? And I can create it. Actually, I don't need to do that. But either way, so I'm getting russ mixed up, I'd have to create some things.
[00:03:45] All right, so, let's take this and let's split. And I believe it's just this. So what comes out of this? Well, two items. So we should be able to like this, const (x.y) = this. And then of course we have to do this whole fun thing, where we have to do x is +x, and y of course is +y.
[00:04:27] So let's copy this exact same thing. And let's parse the line, right? So, I'm gonna have a nice little line string in here. We're going to simply split it with that, right, cuz that's the thing that should be split. Now we're gonna do a p1 and a p2.
[00:05:08] Cuz now I just can perform all that sweet functional nature that we all love. It's just so good. So we can go getInput, and now I can go split on the new line map x as parse line(x). And so now we have the lines, awesome. So now we need to do a filter on this thing.
[00:05:33] Filter is, what is it, is screwing me up with this whole non-straight thing. There we go. So now we have this, and we've even filtered out the ones that are gonna be a diagonal lines. So there we go. And now we have everything, and that is all we need to do to be able to get those lines.
[00:05:51] Cuz that's all I wanted to do for this. So let's just take this, and let's console, log the lines, and npx ts.node src/p2.ts, and print that out. It's gonna run. And as you can see, we're gonna get a set of lines. And y is equal in this case, y is equal in this case, x is equal in this case, x, y and y.