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

The "Submarine 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 version of the solution to the submarine navigation problem.

Preview
Close

Transcript from the "Submarine TypeScript Solution" Lesson

[00:00:00]
>> So let's first write this in TypeScript it should be very easy I think everybody should be able to do this if you want to type along with type along I'll make a little bit of time for you at the end. So I am going to go back to projects I'm gonna make a direct call to TypeScript and I'm gonna go to Typescript and I am going to what do we do We yarn a knit why we get a knit.

[00:00:21]
We yarn add ts- node typeScript types node. Don't forget all those things, right? Start a new quick little typescript project to get everything in there and so I'll give you guys one second to get ready. You're probably also asking yourself how did he move between those folders so fast well it's this beautiful thing I called T mux sessioniser and it's fantastic.

[00:00:43]
Where could you find out information about that dev productivity on front end masters.com/courses/found I'm proud heavy. So we can create a directory of course called source that's pretty much how everything's always done in the TypeScript world and let's create a nice little file called P1. You can call it what you want it can be in the main file doesn't really matter, right?

[00:01:01]
We're not too concerned about all those things. So let's jump back to our input. By the way, if you aren't following along, it's the primary and God GitHub Pages. You may want to be copying this input. It will be a little bit difficult if you don't, so obviously the prime mission can have IOTS go rust it's under a section lessons cuz you can just click into it, so just go to this top part right up here and we're right here.

[00:01:22]
So everyone go here grab your input [SOUND] right, cuz that's what we're gonna need to do to do the basic testing of this. We're gonna go back here, I'm gonna zoom this out just a little bit and let's create a quick function get input that's going to of course return a string and return with those beautiful ticks.

[00:01:39]
All of that data right? Now, we're gonna just do this in a very procedural way just make it easy we'll do something a little bit more functional coming up here but let's just start procedural and we'll move from there. So let's create a function parse line it's gonna take in a line.

[00:01:58]
And it's gonna return out a number of effectively a tuple right, cuz it's what do I need to change up never forget that there you go. How do I need to change my direction? So pretty straightforward when it comes to JavaScript, JavaScript has lLovely string parsing little dangerous when it comes to numbers but overall very lovely.

[00:02:18]
So I can go like this const direction amount I'm gonna call this thing a and go line dot split The whitespace of course, right? That makes pretty much most sense then of course const amount equals plus a that just converted into a number. If you don't know that one that's always a great one it's just so people always get confused like what's the plus side mean well it's actually, All right, there we go.

[00:02:45]
Of course, if dirt equals forward, don't forget all three equal signs. Because in this language you need to be very equal, all right? If it's this, if it's that then of course we return amount and zero. Else if dirt equals up, then let's just return zero and negative amount remember and of course at the end there's only one more condition we return.

[00:03:13]
And we've pretty much solved the problem at this point, right? I mean this is not a very complex problem but I want to make sure you guys type this feel it you really understand the point of this problem. Because when we do it and go, it's just gonna enough different if you don't have your ID setup hopefully I mean easiest to use something like IntelliJ or VS code.

[00:03:37]
I hate I know I have to say this VS code, but Yu can also use VIm, but you have to have your LSP setup. If you use a COC, it's just COC install, I think go really easy to do if you use neovim LSP takes a little bit more effort.

[00:03:55]
Alright, there we go. And so we have this beautiful parse line, which means really, all we need to do at this point is we just need to have our initial starting point and then just add those values into it, right? So I can go get input. I can split on the new line right so every line is something I want read from there I can map x into parse line.

[00:04:20]
All right, and take out x. So there you go. This is looking pretty good. We're like mostly there. And if we were to just simply go like this items, right? We could see the it's just an array of tuples of numbers, right? Okay, good. So we are going the right direction.

[00:04:35]
We have done what we're supposed to be doing. Thank you TS server for being actually a part pretty great. TFS server for all of its things it actually has an amazing LSP on it the fact that it's doing this all nice I just really appreciate that and so then I guess we can use my least favorite.

[00:04:51]
First construct, which is a reduce I just met, I never made a reduce that I like, I always find that I end up getting upset at these things, but we'll use it anyways. And our initial position is 00 of course, because remember, we're trying to be cool like the cool TypeScript kids.

[00:05:10]
We got to use functional nature. Nobody's uses for loops anymore. So we're gonna use this and of course acc zero plus equals my goodness plus equals dir actually is just, where are you? You're really just amount at this point amount zero paste that in get those two and of course return x as you can see, x is just the number number.

[00:05:34]
We're just adding them all together walking through and if I've done this correctly, I believe my two values should be 15 and 10 multiplied together of course, would be 150. And so we gotta go const out equals this. You'll see that it's just a tuple awesome. We've maintained our correct type at the end, and now we should be able to console.log out and then we can go out to 0 out [1].

[00:06:00]
So long as I didn't mess anything up, which I'm really hoping I didn't, this should just work. Put a trademark on it at this point, normally what we would be doing is taking a twitch prediction and did I get it right first try. I believe I did. All right, so I'm gonna go a little npx ts node and go source p 1, right?

[00:06:21]
I'm just gonna execute ts node and run this thing and if we've done it all correctly we see it Yay 1510 150. Yes I knew the answer ahead of time it's almost as if I've done this before. Are you guys excited to do the rust side of things?

[00:06:36]
Yeah, I'm pretty excited Go we're doing go because go honestly just let just a side note if you came to me and said. All right Brian, I'm obviously paying you seven figure salary to come here and help us with our startup. What language do we choose? We need to get this out as soon as possible.

[00:06:54]
I'm gonna say go. Go is just The work and man's language. You can get through stuff so fast. It just is so simple and I find that I have the least complications with mess up in logic while maintaining the most speed.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now