Polyglot Programming: TypeScript, Go, & Rust

# Fissures TypeScript Solution

## ThePrimeagen

terminal

### Check out a free preview of the full Polyglot Programming: TypeScript, Go, & Rust course

The "Fissures TypeScript Solution" Lesson is part of the full, Polyglot Programming: TypeScript, Go, & Rust course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen walks through the TypeScript solution to the Fissures problem.

Preview
Close

### Transcript from the "Fissures TypeScript Solution" Lesson

[00:00:00]
>> 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:06]
So there we go. We've created ourselves a point. Again, JavaScript, very wonderful when it comes to string parsing. But, if this was like more of a production situation in which input may vary, this obviously fails and you get an answer out your system. So it can be more of a pain because you don't address the errors upfront, or at least the errors can be hidden from you.

[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:04:44]
And we'll of course return p1 as parsePoint. P1 and parsePoint p2. So there we go. How nice is that? Very simple operations, right? We kind of created this nice little thing that goes through and, does each step of the way. So we're pretty much done. So this would be like, or at least I would consider this a fairly canonical way to do this in JavaScript.

[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.

[00:06:15]
There you go. So you can see, we got them all out. Pretty straightforward I'd say, kind of a nice way to do this, right? We define our functions ahead of time, then we do this. I think this is pretty much how a lot of people would like to solve it in the JavaScripty way.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses