A quine is a program that writes its own source code. Let's create our own.
Twitter: https://twitter.com/BarelyDaniel Github: https://github.com/danba340
0:00 Intro 0:33 Creating Quines 5:00 The circle of quines 6:00 Watch "The Art of Code"
PUBLICATION PERMISSIONS: Daniel provided Coding Tech with the permission to republish his videos.
CREDITS: Daniel's YouTube channel: https://www.youtube.com/channel/UCgUCptbp4T5saC5WXePe1sw
Today we're going to take a look at coins. A coin is a piece of code that prints its own source code. You might think that should be pretty simple. For example, in JavaScript you just write a console log, you type out the source source code of the program, and you're done. But with that method, there's no way of getting the aftermost console log. Wines don't really have a real world utility, but they are a beautiful expression of programming joy. So I would like to create one today. So first let's try something really stupid. So we tried to add another console log inside, and as we can see, we have two console logs in the source code, but we can only print inside of that, so there's no way to get to the outer one. So we need to do something smart here. So if we create a function, call it F and inside that let's do the console log and then we print the function and then we call function. Okay, so now we got the function printed, but it's not in a stringified form. So I could just add an empty string to it and it will string if I function for us. So now we are pretty close, but what we're missing is the call to the function. So let's see if we can add that. Okay, so let's check. There's a semicolon. I'm just double checking. Yeah, I think we got it. This is one way of doing it. I don't really like this call immediately after, so I'm going to change that to an immediately invoked function. So now we're declaring the function in parentheses and then we call the output of those parentheses with these two parentheses. So now we can see that now we're missing the parenthesis that is in front. So that is a little bit nicer to me. Let's see if we can do this with a template literal instead to clean it up more. So here we do F and then the parentheses after and the parenthesis before. So there we have it. That looks pretty good and I think it's quite understandable as well. Okay, let's take this coin and see how small we can make it, converting the function to an arrow function, and so on. So we'll do F equals. So we have the start and now we can probably remove okay, let's check if it's still valid now. So if equals. So F is the variable and then this is the input to the arrow function and then we return immediately console log. And here we create the entire program so everything looks correct. Pack take parenthesis yeah, so this is my favorite one actually, because this one is neat and small. It's not as readable as the other one, I would say, but this is the nicest one. So people have actually gone super crazy with these coins. There's something called the circle of coins where instead of just printing your own source code, you output code that is executable in another programming language and that code will output code that is executable in the next programming language and you keep doing that going from language to language until you eventually output the original code that you started with. So you go through all of these languages just to come back to the original source code that you started running so you could start anywhere on the circle and if you have all the programming languages installed, you could execute program after program after program until you arrive at the same starting point. So that's just absolutely crazy and it's only the joy of programming that creates these things. So it's very cool. There's this talk called the art of code that I will link in the description. That's where I first heard about clients and I really recommend you watch it. Also in the talk he has created his own language based on rock lyrics and in the end he writes the fistbuss example in his programming language and then he actually plays a song because the programming language is lyrics, the output or the fist buzz program is actually just looks like a rock ballad song. So he plays that song in the end and I really recommend you watch that so hopefully you got inspired. Maybe you learned something. I really appreciate if you like the video, if you subscribe to the channel and if you share it with a friend. So thank you so much for watching. I'll see you in the next video.