<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: jD91mZM2</title>
    <description>The latest articles on Forem by jD91mZM2 (@legolord208).</description>
    <link>https://forem.com/legolord208</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F813%2FNns43E4J.jpg</url>
      <title>Forem: jD91mZM2</title>
      <link>https://forem.com/legolord208</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/legolord208"/>
    <language>en</language>
    <item>
      <title>Absorbing Markov Chains, how do they work?</title>
      <dc:creator>jD91mZM2</dc:creator>
      <pubDate>Fri, 21 Dec 2018 05:44:23 +0000</pubDate>
      <link>https://forem.com/legolord208/absorbing-markov-chains-how-do-they-work-46</link>
      <guid>https://forem.com/legolord208/absorbing-markov-chains-how-do-they-work-46</guid>
      <description>&lt;p&gt;I'm sure a lot of people have heard of Absorbing Markov Chains mainly because of Google Foobar. One of the levels there was to, given an input of a bunch of states and chances to go between the different states, find the percentages of going from one state to a "terminating" state in an unlimited number of steps. When I got to that question, I did not have time to learn this technology. I am very embarrassed to admit that because I can't let things go... I cheated that challenge by hard-coding the answers from somewhere online. I hope there's a button somewhere to redo it - I can't check right now because it's down for maintenance.&lt;/p&gt;

&lt;p&gt;I think the least I can do to make up for my mistake is to make the guide I wish I had, so even somebody like me can understand them. Here we go:&lt;/p&gt;

&lt;h1&gt;
  
  
  Why use Absorbing Markov Chains?
&lt;/h1&gt;

&lt;p&gt;Absorbing Markov Chains can calculate the percentage to go from one state to another, even if such a calculation loops forever.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc8qc8h9tqpxyv9js5y5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc8qc8h9tqpxyv9js5y5.png" alt="Visual example" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;They can be as simple or complicated as you like.&lt;/p&gt;

&lt;h1&gt;
  
  
  Understanding
&lt;/h1&gt;

&lt;p&gt;Before I go on, I would like to point out that some of the following images have been remade as SVGs so you can infinitely zoom them without losing quality. Because dev.to's CDN just converts them to PNGs, you can find them all &lt;a href="https://gist.github.com/jD91mZM2/152598bc7cdd25281e38ed0b98358ef7" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Convergence
&lt;/h2&gt;

&lt;p&gt;Initially the biggest question to me was: How can there be a finite answer? Won't there be an infinite number of combinations?&lt;br&gt;
Imagine a box being built by materials that are half the size of the existing material. Since you can divide infinitely, is there really a finite answer?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffbsliebkveg2mgwjoeqn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffbsliebkveg2mgwjoeqn.png" alt="The box I spoke about" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;... Well! Since the box is slowly working towards 1 infinitely, we can just count 1 as the finite answer.&lt;br&gt;
Similarly,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1/2 + 1/4 + 1/8 + 1/16 + ... = 0.9999999... ≈ 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice I used the "almost equal to" sign there. But technically, I didn't even need to. Because in math, there's proof that 0.99999999... IS 1! The secret lies in the fact that infinity has some quirky rules. Here, let me show you something a friend showed me recently:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// As you know:
9x = 10x - x
// Now do this on 0.99999...
x = 0.99999...
0.99999... * 10 = 9.99999...
9.99999... - 0.99999... = 9
// Therefore...
0.99999... * 9 = 9
0.99999... = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works because normally when you multiply by ten, you add one more number before the decimal point and remove one after it. But since removing one from infinity is still infinity, there's still the same amount of numbers after the decimal point, which means they cancel out.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Crash course on matrices
&lt;/h2&gt;

&lt;p&gt;Before we continue, I need to give a few crash courses. If you already feel comfortable with any of these subjects you can skip over them if you like. I'll try my best to keep it short and to the point while still being clear!&lt;/p&gt;

&lt;p&gt;Matrices are just like 2 dimensional arrays in programming. Doing addition and subtraction on two matrices creates a new matrix where each value in the first matrix is added/subtracted with the corresponding value in the other matrix.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpr7aawr6uy4c3a76s9xl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpr7aawr6uy4c3a76s9xl.png" alt="Visual example" width="800" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although, multiplying two matrices is more difficult: Each row/column combination creates a new number in the result matrix that is the sum of each element in the row multiplied with the corresponding element in the column. This means the number of columns in the first matrix must be the same as the number of rows in the second matrix. The result will have the &lt;strong&gt;same number of rows as the first matrix&lt;/strong&gt; and &lt;strong&gt;same number of columns as the second&lt;/strong&gt;.&lt;br&gt;
Yep, this is confusing. Here, let me show you:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjwcv5333d7klznw6asye.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjwcv5333d7klznw6asye.png" alt="Visual example" width="800" height="536"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, in the above image, it'll do the same thing for every row.&lt;br&gt;
This means that unlike normal numbers multiplying two matrices might give different results depending on the order.&lt;/p&gt;

&lt;p&gt;Multiplying a normal number by &lt;code&gt;1&lt;/code&gt; gives itself. The matrix alternative to this is something called the "identity matrix", which is very simply a bunch of ones on the diagonal.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Crash course on matrix inversion
&lt;/h2&gt;

&lt;p&gt;Now, as you might know, inverting something in math is written as taking something to the power of minus one, &lt;code&gt;^-1&lt;/code&gt;. For "normal" numbers, this is often &lt;code&gt;1/n&lt;/code&gt;. But inversion also exists for matrices, and is another matrix that meets the following requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the original matrix multiplied by the inverse matrix must be the identity matrix&lt;/li&gt;
&lt;li&gt;the inverse matrix multiplied by the original matrix must be the identity matrix&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not all matrices have an inverse, and finding it is a bit tricky! There are special rules for 2x2 and 3x3 matrices, but since we're programmers we need a method for all sizes!&lt;/p&gt;

&lt;p&gt;One method of finding an inverse matrix is Gauss-Jordan elimination. It's a cool mathematical technique that's not specific to inverting matrices. It works by applying various "row operations" to each row in order to turn it into the identity matrix, but while doing so also applying the same operation to the result. The result in this case, is the identity matrix.&lt;/p&gt;

&lt;p&gt;The row operations are relative operations such as multiplying the row, adding/subtracting any multiple of another row, or swapping two rows.&lt;/p&gt;

&lt;p&gt;It's easiest to imagine both the work matrix and result matrix to be both put into one big matrix, but nothing is technically keeping you from having them separate which might be more performant.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdqx50r4re1elxqxxr1nw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdqx50r4re1elxqxxr1nw.png" alt="Visual example" width="800" height="928"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating an input matrix for Absorbing Markov Chains
&lt;/h2&gt;

&lt;p&gt;Let's create a very &lt;em&gt;very&lt;/em&gt; basic example, so we can not only learn how to use this to solve a problem, but also try to see exactly what's going on as we do.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb9npof3f432msvdvejy0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb9npof3f432msvdvejy0.png" alt="Visual example of what I'll describe below" width="734" height="846"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we have an Absorbing Markov Chain that has 90% chance of going nowhere, and 10% of going to an absorbing state. We also draw the absorbing state as having 100% chance of going to itself but that honestly doesn't matter in any of the formulas I've seen yet.&lt;/p&gt;

&lt;p&gt;We talked about convergence before and because it's an infinitely increasing percentage chance to reach a terminal (absorbing) state, we should expect a 100% chance of reaching state 2. I'll also go as specific as to say we should get an estimated number of 10 steps to reach state 2, as 1/0.1 is 10, how many times 0.1 (10%) is repeated before reaching 1 (100%).&lt;/p&gt;

&lt;p&gt;To calculate the result of this, we need to write this as a matrix. Each row will be a state, and each column in that row will be the chance of going to the state with the same row number as that column. I recommend specifying the chance as a fraction, &lt;em&gt;especially&lt;/em&gt; if you're making something that calculates this, as fractions never lose precision unlike floats.&lt;br&gt;
Like always, it's easier if I show you:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa21sceyzou3wsa0bjh6s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa21sceyzou3wsa0bjh6s.png" alt="Visual example of an input matrix" width="800" height="737"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a fundamental matrix
&lt;/h3&gt;

&lt;p&gt;We have a few things we need to do that applies to all operations you might want to do.&lt;/p&gt;

&lt;p&gt;First of all, we need to separate non-absorbing and absorbing states in the matrix. Sort all states so that the absorbing states are at the end (or beginning, if you really really want to. But for this article, I'll assume end). This should allow you to split the input matrix into 4 parts (2 for each dimension):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2duejwxvr61k730t8j7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2duejwxvr61k730t8j7.png" alt="Sliced matrix" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Secondly, we need a "fundamental matrix". This will allow us to get properties from the chain. Each entry in the fundamental matrix will contain the average number of visits to the state with the same index as the column starting from the state with the same index as the row number. In other words, each entry at position &lt;code&gt;(x, y)&lt;/code&gt;, must contain the average visits to state &lt;em&gt;x&lt;/em&gt; starting from state &lt;em&gt;y&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A cool thing about Markov chains is that you can get a matrix where each entry at position &lt;code&gt;(x, y)&lt;/code&gt; contains the probability of transitioning from state &lt;em&gt;y&lt;/em&gt; to state &lt;em&gt;x&lt;/em&gt; in exactly &lt;em&gt;k&lt;/em&gt; steps, simply by taking &lt;code&gt;Q^k&lt;/code&gt; (yes, that's an exponent, those work on matrices too, Q*Q*Q*Q etc).&lt;/p&gt;

&lt;p&gt;So to get the fundamental matrix, we want to sum together the probability of each number of steps ranging from 0 to infinity. Luckily though, it's been proven that you can get the exact same result using the formula &lt;code&gt;N = (I - Q)^-1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwikimedia.org%2Fapi%2Frest_v1%2Fmedia%2Fmath%2Frender%2Fsvg%2F703d6eddb5e051bfa37772992a2e19e8dbaab5e6" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwikimedia.org%2Fapi%2Frest_v1%2Fmedia%2Fmath%2Frender%2Fsvg%2F703d6eddb5e051bfa37772992a2e19e8dbaab5e6" alt="Wikipedia image of the math I just described" width="26" height="7"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;^Image ^stolen ^from ^&lt;a href="https://en.wikipedia.org/wiki/Absorbing_Markov_chain#Fundamental_matrix" rel="noopener noreferrer"&gt;Wikipedia&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So following the equation &lt;code&gt;N = (I - Q)^-1&lt;/code&gt;, where N is the fundamental matrix, Q is the sub-matrix we just established, and I is an identity matrix with the same size as Q (will always be square), we can calculate the fundamental matrix.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsus3hwsq4x2op258i6ik.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsus3hwsq4x2op258i6ik.png" alt="Calculating the fundamental matrix" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our fundamental matrix is now &lt;code&gt;[10]&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting the average number of steps to reach an absorbing state
&lt;/h3&gt;

&lt;p&gt;Since each row in the fundamental matrix has columns that each give the average number of times to visit the state with the same index as column, starting from the state with the row's index, we can get the average number of steps to complete by summing together each row, resulting in the average number of visits in total starting from there.&lt;/p&gt;

&lt;p&gt;In math, a trick to sum each row in a NxN matrix is to multiply it by a 1xN (1 column, N rows) matrix where each row is a one.&lt;br&gt;
The formula is &lt;code&gt;t = N1&lt;/code&gt;, where &lt;code&gt;t&lt;/code&gt; is the matrix of average number of steps from each starting state to an absorbing state, and &lt;code&gt;1&lt;/code&gt; is the 1xN matrix mentioned.&lt;/p&gt;

&lt;p&gt;Obviously, since our example matrix is 1x1, multiplying it is really simple...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft9yxzviuy3qxcatfwpsi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft9yxzviuy3qxcatfwpsi.png" alt="Multiplying 10 by 1" width="500" height="124"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And, as you can see, the average number of steps to reach the absorbing state is 10, just as we guessed!&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting the percentage chance to reach each absorbing state
&lt;/h3&gt;

&lt;p&gt;So, this is probably the step you've been wanting to read. After all, getting a percentage for each state sounds more interesting than the average number of steps it takes to reach any (absorbing) state.&lt;/p&gt;

&lt;p&gt;The formula for this is &lt;code&gt;B = NR&lt;/code&gt;. Remember &lt;code&gt;R&lt;/code&gt; from the sub-matrix we cut earlier? Multiplying the matrix containing the average number of visits to each non-absorbing state with the matrix containing the probabilities to transition into an absorbing state in one step, we get the probabilities of entering an absorbing state after any number of steps.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftmcutq2vam649p8d00ne.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftmcutq2vam649p8d00ne.png" alt="Multiplying 10 by 1/10" width="800" height="157"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final result is a matrix where each column is the chance for the state with that index has to reach the index of that row. In this case non-absorbing state 1 has 1 (100%) chance to reach absorbing state 1 (actually state 2). Seems correct!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5r6fnrcozdf5mlk9vxg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5r6fnrcozdf5mlk9vxg.png" alt="Another example" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;^(&lt;em&gt;Example output here is the output of &lt;a href="https://en.wikipedia.org/wiki/Absorbing_Markov_chain#String_generation" rel="noopener noreferrer"&gt;https://en.wikipedia.org/wiki/Absorbing_Markov_chain#String_generation&lt;/a&gt;&lt;/em&gt;)&lt;/p&gt;

&lt;h1&gt;
  
  
  Final notes
&lt;/h1&gt;

&lt;p&gt;There's a famous quote:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you can't explain it simply, you don't understand it well enough.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;^(&lt;em&gt;Sadly couldn't find who said it, but it's frequently miss-attributed to Einstein.&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;And I admit I certainly don't understand this witchery well enough. But I hope that my article may still have given you a little more insight. Wikipedia and other sources I've found just seem to state the facts, but don't try to explain them for 5 year olds. I might not be able to explain them well either, but the least I can do is try.&lt;/p&gt;

&lt;p&gt;I have implemented this here: &lt;a href="https://gitlab.com/jD91mZM2/math-types" rel="noopener noreferrer"&gt;math-types&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://gitlab.com/jD91mZM2/math-types/blob/b6cf5ac29d714532069cb2091cc5a4399ff7975e/src/matrix.rs#L265-283" rel="noopener noreferrer"&gt;Matrix Multiplication&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gitlab.com/jD91mZM2/math-types/blob/b6cf5ac29d714532069cb2091cc5a4399ff7975e/src/matrix.rs#L138-211" rel="noopener noreferrer"&gt;Matrix Inversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gitlab.com/jD91mZM2/math-types/blob/b6cf5ac29d714532069cb2091cc5a4399ff7975e/src/fraction.rs" rel="noopener noreferrer"&gt;Fraction data type (optional for precision)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gitlab.com/jD91mZM2/math-types/blob/b6cf5ac29d714532069cb2091cc5a4399ff7975e/examples/absorbing-markov-chains.rs" rel="noopener noreferrer"&gt;Program example&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>math</category>
      <category>chance</category>
      <category>matrix</category>
    </item>
    <item>
      <title>Programming for Redox OS</title>
      <dc:creator>jD91mZM2</dc:creator>
      <pubDate>Thu, 05 Jul 2018 17:52:55 +0000</pubDate>
      <link>https://forem.com/legolord208/programming-for-redox-os-4124</link>
      <guid>https://forem.com/legolord208/programming-for-redox-os-4124</guid>
      <description>&lt;p&gt;If you don't know what Redox OS is, it's an entire operating system written in Rust. This is because Rust has some amazing guarantees about memory and thread safety. To make it even safer, it's a microkernel as opposed to a monolithic kernel. That means that for example drivers and similar run in userspace and cannot crash the entire computer in the same way.&lt;/p&gt;

&lt;p&gt;To communicate between processes and the kernel, Redox OS has custom schemes. It follows the tradition of having &lt;a href="https://doc.redox-os.org/book/design/url_scheme_resource/everything_is_a_url.html"&gt;everything be a URL&lt;/a&gt;. Schemes are dead simple and super awesome. You open a file with the path &lt;code&gt;scheme:path&lt;/code&gt;. If no scheme is specified it defaults to the one of the current working directory - which is usually going to be &lt;code&gt;file:&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  TCP
&lt;/h2&gt;

&lt;p&gt;An example of the scheme is the TCP protocol. Even though networking is supposed to be hard, in redox it's dead simple. You just &lt;code&gt;open("tcp:address", O_RDWR)&lt;/code&gt;.&lt;br&gt;
Here's an example that curls example.com without any libraries what-so-ever (not an idiomatic use of the http protocol - prefer a library for doing http requests):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mut file = File::open("tcp:93.184.216.34:80")?; // example.com
file.write(b"GET / HTTP/1.1\nHost: example.com\nConnection: close\n\n")?;

io::copy(&amp;amp;mut file, &amp;amp;mut io::stdout())?;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Program IPC
&lt;/h2&gt;

&lt;p&gt;Another example of a scheme is one I wrote myself - the ipcd daemon. It's a simple scheme for inter-process communication. You call &lt;code&gt;open("chan:name", O_CREAT)&lt;/code&gt; to create a server, and &lt;code&gt;dup("listen")&lt;/code&gt; to accept a connection. The &lt;code&gt;dup&lt;/code&gt; syscall is for duplicating files, but can optionally take a path which can be used from the scheme.&lt;br&gt;
Here is a simple hello world server using ipcd - this time we need the syscall library for redox in order to duplicate the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"chan:hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;loop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;syscall&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;dup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="nf"&gt;.as_raw_fd&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="s"&gt;"listen"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_raw_fd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="nf"&gt;.write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="s"&gt;"Hello World!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And to read from this ipc channel you just have to &lt;code&gt;cat chan:hello&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Events
&lt;/h2&gt;

&lt;p&gt;This scheme is somewhat special, not because it has any special hardcoded exception to it, but because it's used everywhere, even in other schemes.&lt;br&gt;
The event scheme is kind of like epoll in linux. You &lt;code&gt;write&lt;/code&gt; an event to an instance and it registers to listen for events from file (or instance of a scheme), and then you &lt;code&gt;read&lt;/code&gt; and it blocks until the first event.&lt;br&gt;
There is an excellent example in the &lt;a href="https://gitlab.redox-os.org/redox-os/rfcs/blob/master/text/0002-event-overhaul.md#detailed-design"&gt;event overhaul RFC&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This scheme works very well with the &lt;code&gt;time:&lt;/code&gt; scheme, and can be used for setting a timeout to reading. The time scheme is simple, can read the current time and write a deadline which triggers an event when that time is elapsed.&lt;br&gt;
Here's an example that timeouts a read to 2 seconds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;TOKEN_TIME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;TOKEN_FILE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;selector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"event:"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;some_file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="cm"&gt;/* anything you want to read from here, like a tcp stream */&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c"&gt;// Deadline&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"time:"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;TimeSpec&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="nf"&gt;.read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="py"&gt;.tv_sec&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;// deadline to 2 seconds in the future&lt;/span&gt;
&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="nf"&gt;.write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;selector&lt;/span&gt;&lt;span class="nf"&gt;.write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Event&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="nf"&gt;.as_raw_fd&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;EVENT_READ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;TOKEN_TIME&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;selector&lt;/span&gt;&lt;span class="nf"&gt;.write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Event&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;some_file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;EVENT_READ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;TOKEN_FILE&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;selector&lt;/span&gt;&lt;span class="nf"&gt;.read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="py"&gt;.data&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;TOKEN_TIME&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c"&gt;// The timeout of 2 seconds was exceeded&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;TOKEN_FILE&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c"&gt;// The file can now be read!&lt;/span&gt;
        &lt;span class="c"&gt;// How much can be read is undefined and depends on the scheme.&lt;/span&gt;
        &lt;span class="c"&gt;// Most built-in schemes are nowadays edge triggered and you should&lt;/span&gt;
        &lt;span class="c"&gt;// have the file be non-blocking and read over and over again&lt;/span&gt;
        &lt;span class="c"&gt;// until you hit EAGAIN.&lt;/span&gt;
        &lt;span class="c"&gt;// This matches the behavior of linux.&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="k"&gt;loop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;some_file&lt;/span&gt;&lt;span class="nf"&gt;.read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c"&gt;// EOF, we read everything!&lt;/span&gt;
                &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="c"&gt;// Handle the read here.&lt;/span&gt;
                    &lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="nf"&gt;.kind&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nn"&gt;ErrorKind&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;WouldBlock&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c"&gt;// can't read any more this round.&lt;/span&gt;
                &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  How does it work?
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Note: Writing a custom scheme requires root&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Main Loop
&lt;/h2&gt;

&lt;p&gt;The scheme is registered using the root scheme - which has no name. &lt;code&gt;open(":test", O_CREAT)&lt;/code&gt; registers the scheme and returns a file you can read packets from.&lt;br&gt;
How do you handle the packets? That's where the Scheme family of traits come in! They have a function called &lt;code&gt;handle&lt;/code&gt; that is already implemented, that reads the packet and calls the correct trait function.&lt;br&gt;
A simple loop may look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Handler&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;// handler implements SchemeMut&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;scheme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;loop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;packet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Packet&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;scheme&lt;/span&gt;&lt;span class="nf"&gt;.read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="nf"&gt;.handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;scheme&lt;/span&gt;&lt;span class="nf"&gt;.write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sadly most schemes' loops look more difficult because they might need to read from multiple schemes at the same time, or reschedule blocking functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unique IDs
&lt;/h2&gt;

&lt;p&gt;The scheme has functions like these:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;_u&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;_&lt;/span&gt;&lt;span class="n"&gt;gid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where you return any integer you want!&lt;br&gt;
A cool thing about Redox schemes is that each instance of the scheme (file descriptor) gets mapped to an ID, and multiple file descriptors can have the same ID (so you don't have to implement &lt;code&gt;dup&lt;/code&gt;s without a path yourself).&lt;br&gt;
What almost all schemes do here is keep an integer that points to the next id, and then on &lt;code&gt;open&lt;/code&gt; it adds the id to the an internal map together with some struct with data about the handle, and finally increments the integer.&lt;br&gt;
Later functions are called with the id:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn read(&amp;amp;mut self, id: usize, buf: &amp;amp;mut [u8]) -&amp;gt; Result&amp;lt;usize&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Blocking
&lt;/h2&gt;

&lt;p&gt;A blocking function like read is usually rescheduled by the scheme when it fails. Usually they used to use the EWOULDBLOCK error internally to add the packet to a list of todos and then later (on either another scheme event or some timer) try to process that packet again. To ease with this, we now have a SchemeBlock trait that can return a None to indicate that it should be rescheduled.&lt;br&gt;
For an example of a really simple blocking loop, check &lt;a href="https://gitlab.redox-os.org/redox-os/ipcd/blob/19f8137eef18ea4cbcec119402741dae6feec68e/src/main.rs#L26-52"&gt;ipcd's main file&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Events
&lt;/h2&gt;

&lt;p&gt;To make your schemes support events, you just need to write a packet to the scheme file with the values of a=SYS_FEVENT,b=id,c=flag,d=count (I don't &lt;em&gt;think&lt;/em&gt; count is used anymore, you might just leave it at 1). There is also an &lt;code&gt;fevent&lt;/code&gt; scheme function that will let you know when somebody has registered your scheme to an event instance. This can be used to reset variables to keep track of notified state in schemes that send edge triggered events.&lt;/p&gt;

</description>
      <category>redox</category>
      <category>rust</category>
      <category>os</category>
    </item>
    <item>
      <title>Base64 is fun!</title>
      <dc:creator>jD91mZM2</dc:creator>
      <pubDate>Fri, 09 Feb 2018 13:56:11 +0000</pubDate>
      <link>https://forem.com/legolord208/base64-is-fun--420d</link>
      <guid>https://forem.com/legolord208/base64-is-fun--420d</guid>
      <description>&lt;p&gt;Hello! Today I decided to stop feeling like Base64 is black magic, and go &lt;em&gt;really&lt;/em&gt; learn it. You can see the result &lt;a href="https://github.com/jD91mZM2/base64test"&gt;here&lt;/a&gt;. I learned a lot of interesting things, so I'll share it with you here.&lt;br&gt;
Because of mixed backgrounds I'll make sections for everything so you can skip anything you already know.&lt;/p&gt;
&lt;h1&gt;
  
  
  Bitwise operators
&lt;/h1&gt;

&lt;p&gt;To properly learn Base64, you need to learn bitwise operators. I'm sure you've seen stuff like &lt;code&gt;50 &amp;amp; 1 == 0&lt;/code&gt; somewhere before. What are these weird half-boolean operators doing on the integers?!&lt;br&gt;
As you know, computers work with binary. But the relationship is closer than you think. What bitwise operators do is flip certain bits.&lt;br&gt;
&lt;em&gt;Each binary digit (0 or 1) is called a bit.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Bitwise And: &lt;code&gt;&amp;amp;&lt;/code&gt;. This calculates the integer result of two integers where each bit is the same. In other words, it sees which bits number one &lt;strong&gt;and&lt;/strong&gt; number two has. So it's called bitwise and. Examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  42 // &amp;lt;- Binary: 101010
&amp;amp; 15 // &amp;lt;- Binary: 001111
= 10 // &amp;lt;- Binary: 001010

  5 // &amp;lt;- Binary: 101
&amp;amp; 6 // &amp;lt;- Binary: 110
= 4 // &amp;lt;- Binary: 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bitwise Shift: &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt; and &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;. Basically "shifts" all numbers to left or right. These can be compared to multiplying by 10^n in decimal. In fact, it's &lt;em&gt;exactly&lt;/em&gt; the same as multiplying by 2^n. When you multiply something by 2, the compiler sometimes optimizes it to bit-shifting by one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   16 // &amp;lt;- Binary:  10000
&amp;lt;&amp;lt; 1
=  32 // &amp;lt;- Binary: 100000

   42 // &amp;lt;- Binary: 101010
&amp;gt;&amp;gt; 3
=  5  // &amp;lt;- Binary:    101
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Byte order
&lt;/h1&gt;

&lt;p&gt;As you may know, an &lt;code&gt;int&lt;/code&gt; is a 32-bit integer and a &lt;code&gt;byte&lt;/code&gt; is an 8-bit integer.&lt;br&gt;
Since 32 (and all other types of integers) is a multiple of 8, you could technically convert a bigger type of integer to multiple bytes and then back... right?&lt;br&gt;
Like say 666, &lt;code&gt;0000001010011010&lt;/code&gt;, could be stored as &lt;code&gt;00000010&lt;/code&gt; and &lt;code&gt;10011010&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Turns out, of course, I wasn't the first one to think of this. In fact, in memory they're represented like this too. In C you could simply cast a &lt;code&gt;int*&lt;/code&gt; to a &lt;code&gt;byte*&lt;/code&gt; and then access the specific bytes inside the integer.&lt;br&gt;
What you &lt;em&gt;may&lt;/em&gt; find (it depends), is that they are in the reverse order of what I just showed (&lt;code&gt;10011010&lt;/code&gt; first, not last). This is called &lt;em&gt;little endian&lt;/em&gt;, where as what I showed is &lt;em&gt;big endian&lt;/em&gt;. &lt;em&gt;Big endian&lt;/em&gt; means the "big end" (the byte that makes the largest impact, just like changing the one in 100 makes a larger impact than changing the one in 10) is first.&lt;/p&gt;
&lt;h1&gt;
  
  
  Putting it all together
&lt;/h1&gt;

&lt;p&gt;Normal binary data is stored in 8 bits, which means it can go to 256.&lt;br&gt;
64 happened to be a good amount of characters &lt;strong&gt;that is a power of 2&lt;/strong&gt;.&lt;br&gt;
The reason it's a power of two is quite brilliant: It can be converted to by simply reading 6 bits instead of 8.&lt;/p&gt;

&lt;p&gt;Say we have the following: "hello".&lt;br&gt;
In bytes, this is &lt;code&gt;104 101 108 108 111&lt;/code&gt;.&lt;br&gt;
In binary: &lt;code&gt;01101000 01100101 01101100 01101100 01101111&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we make this into one integer. Before we do that though, there's a problem. This quickly gets unbelievably large.&lt;br&gt;
We solve this using a convenient fact: 4 6-bit integers has the same bit length as 3 8-bit ones. That's also why it's padded to 4: 6*4 = 8*3. This is really convenient for the programmer when decoding, so he/she doesn't have to bounds check valid base64.&lt;/p&gt;

&lt;p&gt;Because of this, we only have to worry about 3 bytes at a time (4 when decoding).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="nf"&gt;.chunks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// Fun fact, you *could* do this, but it's more fun to use bit shifting.&lt;/span&gt;
    &lt;span class="c"&gt;// let buf = mem::transmute::&amp;lt;&amp;amp;[u8], u32&amp;gt;(bytes).to_be();&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.cloned&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.cloned&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt;  &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.cloned&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap_or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The reason for &lt;code&gt;.get(n).cloned().unwrap_or(0)&lt;/code&gt; is basically because indexing can crash. The input doesn't have to be dividable by 3.&lt;/em&gt;&lt;br&gt;
Now we have&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;011010000110010101101100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;011011000110111100000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;.&lt;br&gt;
&lt;em&gt;Note: The last number gets padded out with 0s.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Second step now sounds really simple: Re-split the numbers every 6 bytes and insert &lt;code&gt;=&lt;/code&gt; if it runs out before it's a multiple of 4.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mi"&gt;0x3F&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first part is actually rather simple but unecessarily verbose. It counts down from 24, 6 steps at a time. It could probably have been written like &lt;code&gt;for j in (0..=24).rev().step_by(6)&lt;/code&gt;, but I need &lt;code&gt;i&lt;/code&gt; later.&lt;/p&gt;

&lt;p&gt;But next comes the fun part:&lt;br&gt;
Let's imagine &lt;code&gt;j&lt;/code&gt; is 18.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   011010000110010101101100
&amp;gt;&amp;gt; 24
=  011010000110

   011010000110
&amp;amp;  000000111111 // &amp;lt;- 0x3F in binary.
=  000110
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See the pattern? In a way, we basically just took a substring... of the integer! The bitwise shift allowed us to fetch a value down, and the bitwise and allowed us to specify which values we cared about.&lt;/p&gt;

&lt;p&gt;Now we have&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;011010 000110 010101 101100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;011011 000110 111100 000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;Finally, let's insert the value, or &lt;code&gt;=&lt;/code&gt; if out of bounds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'='&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: I didn't include the definition of &lt;code&gt;char&lt;/code&gt; function. It basically just maps a value to a base64 character, like &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;A&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We now have&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a G V s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;b G 8 =
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aGVsbG8=&lt;/code&gt;&lt;/p&gt;

</description>
      <category>base64</category>
      <category>encoding</category>
      <category>rust</category>
    </item>
    <item>
      <title>There are STILL not enough messaging applications</title>
      <dc:creator>jD91mZM2</dc:creator>
      <pubDate>Mon, 08 Jan 2018 09:35:27 +0000</pubDate>
      <link>https://forem.com/legolord208/there-are-still-not-enough-messaging-applications-no</link>
      <guid>https://forem.com/legolord208/there-are-still-not-enough-messaging-applications-no</guid>
      <description>&lt;p&gt;Every single service nowadays has private messaging. Twitter, Facebook, Reddit, Steam, etc. Every single programmer just learning about Tcp wants to make a simple one. Yet there are still not enough.&lt;/p&gt;

&lt;p&gt;Alright, since there are so many services I am going to filter them by a simple requirement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One server can have multiple channels.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you don't get what I mean by that, I mean basically something like IRC.&lt;br&gt;
Who meets the requirement? IRC, Discord, Slack, Mattermost. Those are the only ones I know of. Now let's check how each individual one falls short:&lt;/p&gt;

&lt;p&gt;Discord:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Electron&lt;/li&gt;
&lt;li&gt;Client side validation&lt;/li&gt;
&lt;li&gt;Huge cache files&lt;/li&gt;
&lt;li&gt;Not following the XDG standard (cache files should be in ~/.cache)&lt;/li&gt;
&lt;li&gt;Using regex over linkify&lt;/li&gt;
&lt;li&gt;Closed source&lt;/li&gt;
&lt;li&gt;Stores currently written message in the JS local storage for every single keypress&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;The main reason Discord has so many points here is because it's what I've been using (and suffering from) the most.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Slack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Electron&lt;/li&gt;
&lt;li&gt;Closed source&lt;/li&gt;
&lt;li&gt;Only stores the last 10 000 messages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;I have never tried Slack. This is all I know.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Mattermost:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Electron&lt;/li&gt;
&lt;li&gt;Channel join/leave system is kind of useless&lt;/li&gt;
&lt;li&gt;Can only use one server at a time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;IRC:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Difficult&lt;/li&gt;
&lt;li&gt;No real message storage&lt;/li&gt;
&lt;li&gt;No real authentication system apart from unofficial NickServ&lt;/li&gt;
&lt;li&gt;No real bot API&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One thing that hurts me a lot here is how many of these are using Electron. Is programming GTK+ really so difficult that you have to steal all the client's CPU? Discord is using 7% CPU idle for me. Sure, Electron is easy and looks good, but it's also the biggest hack ever made. Launching an independent web browser copy because you want to look professional having an EXE file for your webapps... really?&lt;/p&gt;

&lt;p&gt;So what I'm trying to say, is that if any of you feel urged to make a chat application but aren't because of how many there already are... think again. We need more alternatives out there.&lt;br&gt;
Myself, I've been &lt;a href="https://github.com/synac-chat/"&gt;writing one&lt;/a&gt; that's currently in beta. Although it's been a useful experience for me, it's not very fun at times.&lt;/p&gt;

&lt;p&gt;And of course, if any of you know an alternative (even if you made it yourself) that seems to be what I'm looking for, please do link it in the comments!&lt;/p&gt;

</description>
      <category>messaging</category>
    </item>
    <item>
      <title>Arch Linux: Stop recommending people to use makepkg for the AUR</title>
      <dc:creator>jD91mZM2</dc:creator>
      <pubDate>Sat, 06 Jan 2018 11:22:13 +0000</pubDate>
      <link>https://forem.com/legolord208/arch-linux-stop-recommending-people-to-use-makepkg-for-the-aur-35nn</link>
      <guid>https://forem.com/legolord208/arch-linux-stop-recommending-people-to-use-makepkg-for-the-aur-35nn</guid>
      <description>

&lt;p&gt;&lt;em&gt;This is relevant to Arch Linux and the Arch User Repository. If you don't know what that is, no need to read this article.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Alright, there seems to be around 3 kinds of people:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;"Pacman should be like apt"&lt;/li&gt;
&lt;li&gt;"You probably want an AUR helper"&lt;/li&gt;
&lt;li&gt;"Real men use makepkg"&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2/3 of these people know that adding a custom repository to get a program is HORRIBLE security-wise. That's not worth mentioning.&lt;br&gt;
I will instead be focusing on eliminating the 3rd kind, leaving us with the second (which I'm a part of).&lt;/p&gt;

&lt;p&gt;Let me tell you a tale of how I got used to the AUR.&lt;br&gt;
Immediately when I started off, I wanted to be the 3rd kind. I wanted to use only the official tools. "Real men don't need no helper" or whatever. I scanned the wiki on my phone (Arch wasn't completely set up yet), and found out how to install packages. I failed to find out how to update them, so I assumed pacman did that for you. I started cloning all packages to ~/Downloads, building them, and deleting them.&lt;br&gt;
That was my first pitfall. Already, I had made a mistake. I needed to keep them updated. Alright, that's simple enough. I re-downloaded all my packages (TIP: pacman -Qm) to ~/AUR. Then I made a bash script to git pull all the things, and building them if there was anything to update (NOTE: I hadn't thought of -git packages).&lt;br&gt;
This is what I used for a while. And it worked, except it required a lot of interaction. I had to copy the URL, cd, git clone, cd, makepkg. I started avoiding the AUR as much as I could. Alright, simple fix: Just make a bash script to download it? No. This is where I stopped. And I'll tell you why in a second.&lt;br&gt;
But first I want to inject that I never checked any other files than the PKGBUILDs. I never read the wiki carefully enough, so I failed to realize that ALL files could contain viruses. If I had used a helper, this risk would have been avoided all together.&lt;/p&gt;

&lt;p&gt;I saved my most important point to last. If you make shell scripts around the manual way of doing it, are you really still using the manual way...&lt;br&gt;
&lt;em&gt;...or have you created a helper?&lt;/em&gt;&lt;/p&gt;


</description>
      <category>archlinux</category>
      <category>aur</category>
    </item>
    <item>
      <title>Are async frameworks really worth it?</title>
      <dc:creator>jD91mZM2</dc:creator>
      <pubDate>Sat, 30 Dec 2017 07:50:15 +0000</pubDate>
      <link>https://forem.com/legolord208/are-async-frameworks-really-worth-it-4de0</link>
      <guid>https://forem.com/legolord208/are-async-frameworks-really-worth-it-4de0</guid>
      <description>&lt;p&gt;&lt;em&gt;This "article" is going to be specifically about &lt;a href="https://www.rust-lang.org/"&gt;Rust&lt;/a&gt; and &lt;a href="https://tokio.rs/"&gt;Tokio&lt;/a&gt;, but it should apply to more frameworks&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is more of a question than an article. I've been working on &lt;a href="https://github.com/synac-chat/server"&gt;Synac&lt;/a&gt; for a while, and I've needed to handle multiple connections on one thread.&lt;br&gt;
For the server, I went with tokio. It allowed me to do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="nf"&gt;.incoming&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.for_each&lt;/span&gt;&lt;span class="p"&gt;(|(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="p"&gt;)|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ssl&lt;/span&gt;&lt;span class="nf"&gt;.accept_async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.map_err&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="mi"&gt;_&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="nf"&gt;.and_then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;move&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Client connected!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="nf"&gt;.run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem with that is that often you can't just make everything async unless you're programming in a language where blocking calls are barely possible (looking at you, JavaScript). Sure, Tokio is easy once you get the hang of it. But usually you want to combine syncs, asyncs, and all kinds of things. This is where Tokio becomes a problem.&lt;br&gt;
For the synac client, I already had experience with a library that only allowed blocking reads, so multithreading wasn't possible. I didn't want to force built-in mutexes upon users, and I didn't want to prevent multithreading, so I came up with my own solution. It gave me code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="nf"&gt;.try_read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Packet received: {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;(If you wonder how I did this magic sorcery, I simply did something similar to &lt;a href="https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact"&gt;read_exact&lt;/a&gt;, but in a struct. &lt;a href="https://github.com/jD91mZM2/synac-rs/blob/master/src/listener.rs"&gt;Code&lt;/a&gt;)&lt;/em&gt;&lt;br&gt;
This ended up being a good decision, because I could then do non-blocking reads inside &lt;code&gt;gtk::timeout_add&lt;/code&gt;! And it wasn't too difficult to code it either!&lt;/p&gt;

&lt;p&gt;Now I want to ask: When should you use an async framework with cores and stuff, and when should you just write a non-blocking listener? I feel like non-blocking listeners are way more flexible, but there has to be a reason tokio exists... Right?&lt;/p&gt;

</description>
      <category>rust</category>
      <category>tokio</category>
      <category>async</category>
      <category>discuss</category>
    </item>
    <item>
      <title>A letter to my past self</title>
      <dc:creator>jD91mZM2</dc:creator>
      <pubDate>Thu, 14 Dec 2017 08:46:47 +0000</pubDate>
      <link>https://forem.com/legolord208/a-letter-to-my-past-self-5b5</link>
      <guid>https://forem.com/legolord208/a-letter-to-my-past-self-5b5</guid>
      <description>&lt;p&gt;A little more than a year ago, I was just about ready to move on. I had my fill of Java, discontinued my shitty "programming language" (which was just a bunch of startsWith("&amp;lt;command&amp;gt; ") statements), and I was making a crappy little chat-bot thing with TCP support. In order to connect to it, I realized I should look into some other language which was easier with this task. So I turned my attention on &lt;a href="https://golang.org/"&gt;Go&lt;/a&gt;.&lt;br&gt;
So that I was. Finally exploring a new language after a very long time of being addicted to Java Swing. I never liked front-end development, but I thought it was needed. Oh boy I was wrong.&lt;br&gt;
Somewhere there my friend &lt;a href="https://twitter.com/tblodt"&gt;tbodt&lt;/a&gt; popped along. He started asking a little about one of my applications, which drove me interested in working more on that application. I don't know when it happened, but after some time it was clear to me this tbodt person was a better, smarter and more knowledgable person than I'll ever be. And I was right, a year later I am a completely changed person thanks to him. Now I want to see if I can be my own tbodt:&lt;/p&gt;

&lt;p&gt;Dear past self. In order to improve, here are a few tips.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Font-end is trash. You don't like font-end development. You do it because you know that every user wants a GUI. That is incorrect, you're simply looking at the wrong target users. Start making terminal applications, it's way more fun.&lt;/li&gt;
&lt;li&gt;Abandon Windows. What you used to call "Linux" (when you actually meant "GNU/Linux". Linux is just the core of the operating system, the kernel. You still need the GNU operating system to actually make a working system). It isn't what everybody says it is, it's not a pro 1337 h4x0r operating system for edgy people. It's for everyone. It's better than Windows will ever be. Also, don't worry about your C: and D: drive. On GNU/Linux they're for some reason one drive. I still haven't figured out why, but I guess Windows is just drunk. Or maybe they're different partitions (parts of the drive which pretty much act like separate drives). What distribution? Ubuntu. But don't stay there forever. Make a separate /home partition so you can keep all your files when you reinstall your system, and then when you've been on Ubuntu for a while and feel bored with it you can switch to Arch Linux. You won't get bored with Arch. Also look into the i3 window manager. It isn't as empty and scary as it looks when you first start it. You just need to configure it some. If absolutely needed, watch some YouTube video on i3 to really get motivation. By the way, don't bother with trying to get nvidia drivers. They're literally broken. You'll just ruin your computer trying to uninstall them and reinstall the open source ones. Stick with the open source.&lt;/li&gt;
&lt;li&gt;Finally learn what the |, &amp;amp;, ^ and ~ operators are!!! I know you've really wanted to know but never been able to learn. Don't worry, it's simple. Just take a look at the binary values. &lt;code&gt;2 | 1 = 3&lt;/code&gt; because &lt;code&gt;10 | 1 = 11&lt;/code&gt;. &lt;code&gt;5 | 3 = 7&lt;/code&gt; because &lt;code&gt;101 | 11 = 111&lt;/code&gt;. It basically enables all bits from the second value in the first. &lt;code&gt;~1&lt;/code&gt; is -2 because &lt;code&gt;00000001&lt;/code&gt; inverted is &lt;code&gt;11111110&lt;/code&gt;. The reason it's 8 in length is because a byte is 8 bits in memory. And in memory, if the 1st bit is 1 that means it's a negative number, and starts subtracting everything from the max byte, 256. The rest you should be able to experment and learn on your own.&lt;/li&gt;
&lt;li&gt;Abandon Java. This is the first time you'll &lt;em&gt;really&lt;/em&gt; feel like staying. New languages look new and scary. You think you're confident with Java, and you doubt you'll ever be that confident with another language. You don't wanna throw away all your old skills for nothing. It isn't for nothing. You can fight through this. Stick with &lt;a href="https://golang.org/"&gt;Go&lt;/a&gt; for a while, but &lt;em&gt;don't stay&lt;/em&gt;. Go is just good to know, but it's not the end. &lt;em&gt;Go&lt;/em&gt; forward. Get &lt;a href="https://www.rust-lang.org/"&gt;Rust&lt;/a&gt;. Rust is everything you've ever wanted. You can code with C-like performance and possibilities while still keeping built-in UTF-8 support, safe memory management. Also threading.&lt;/li&gt;
&lt;li&gt;Start with Git and GitHub. Sharing code isn't as bad as you think. Nobody will read and judge your code. Plus, people can already decompile it. Trying to hide your code does nothing. Trying to produce binaries for all systems is a pain (not so much with Java as any other language). You can just give them the source and they'll compile it for themselves. Also versioning is nice for example if you're implementing a new feature but decide you wanna roll the changes back. &lt;/li&gt;
&lt;li&gt;I know you think there isn't anything like "true multithreading". You think that computers are just shuffeling instructions. And that is partly correct. But most computers have multiple cores, so there is a chance that "data races" occur when accessing the same variable from different threads. That is where so called "Mutexes" come in. There are also "Atomic" variables for primitive types, which is what mutexes use under the hood. And yeah, you've been misusing Java's "AtomicX", thinking it's just a wrapper in order to edit a &lt;code&gt;final&lt;/code&gt; field.&lt;/li&gt;
&lt;li&gt;Learn what memory is. It's just a big pile of bytes. If you make the string "hi", that looks like &lt;a href="https://dev.tothe%20character%20codes%20of%20each%20character"&gt;104, 105&lt;/a&gt; plus a little extra information, depending on programming language. In C it would be [104, 105, 0] because 0 marks the end of a string. In most languages it would be &lt;em&gt;something like&lt;/em&gt; [2, 104, 105], where the 2 shows the string length. And that's why pointers have "addresses". Because a pointer is just a number to a memory cell. Also, there are actually 2 different kinds of memory. Heap and stack. The heap is dynamic. That's where you can actually allocate more. Somebody correct me if I'm wrong with the following: The stack is faster to use, because it can never be resized. Once your program is started, you get the heap you ask for and then you can't make more. But yeah, in order to make your program faster, you don't need to do whatever you think you need to do. You just need to make sure to allocate less. Which Rust is awesome at.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>How I want my command inputs, but nobody agrees</title>
      <dc:creator>jD91mZM2</dc:creator>
      <pubDate>Sun, 13 Aug 2017 04:38:55 +0000</pubDate>
      <link>https://forem.com/legolord208/how-i-want-my-command-inputs-but-nobody-agrees</link>
      <guid>https://forem.com/legolord208/how-i-want-my-command-inputs-but-nobody-agrees</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;cmd&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"help"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;search&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;printHelp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"exit"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;closing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"exec"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nargs&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;stdutil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PrintErr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"exec &amp;lt;command&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is some source code from my old version of my crappy application DiscordConsole, written in Go. (A rewrite is being made.)&lt;br&gt;
I chose it because it perfectly demonstrates how I want my command systems, and also how languages make this difficult.&lt;br&gt;
Come on, I know you like your OOP, but isn't it damn overkill to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;CommandHandler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;acceptCommands&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;minArg&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;maxArg&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;handleCommand&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;CommandArgs&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EchoCommand&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;CommandHandler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="nf"&gt;acceptCommands&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"echo"&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;minArg&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;maxArg&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MAX_VALUE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;handleCommand&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;CommandArgs&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;join&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To me that's just plain ugly. And oh god how inefficient.&lt;br&gt;
&lt;em&gt;Sorry for the mix of programming languages, I thought Java perfectly described OOP, and Go perfectly described how I want my command systems while still using the "switch" keyword and not "match" like Rust does.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But there is this one advantage with your system. It allows splitting commands in multiple files. And now you won't stop nagging on me and how horrible my command system is.&lt;br&gt;
Yes, it's true I can't split mine into multiple files. It's true that's really a problem once you get more than 3 commands. But it's also the OOP way makes unecessary allocations and function calls, and is a pain to set up.&lt;/p&gt;

&lt;p&gt;So let's think outside the rules of any programming language. What is a perfect command system to me?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;cmd&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"echo"&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;usage_min!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"No argument specified"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="nf"&gt;.join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="s"&gt;"some_long_command"&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;include!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"commands/some_long_command.rs"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example was written in Rust. The &lt;code&gt;match&lt;/code&gt; keyword is like &lt;code&gt;switch&lt;/code&gt; but cooler. It perfectly demonstrates how macros are THE way to make a cheap application look good in code, and yes, that is saying all languages that don't have them suck. I love macros.&lt;br&gt;
It also shows that it very nearly &lt;em&gt;is&lt;/em&gt; possible to make a non-OOP command system look good, AND split it into multiple files. The only thing stopping us is that the &lt;code&gt;include!&lt;/code&gt; keyword in Rust exists, but it doesn't see local variables and macros.&lt;br&gt;
Still then, a lot more languages should have macros, and a lot more languages should be able to include a file's contents without any hygiene and stuff.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This whole post is essentially me hoping &lt;a href="https://github.com/rust-lang/rust/issues/32379"&gt;#32379&lt;/a&gt; gets fixed...&lt;/em&gt;&lt;/p&gt;

</description>
      <category>command</category>
      <category>input</category>
    </item>
    <item>
      <title>Comparing error handling in Java, Go and Rust.</title>
      <dc:creator>jD91mZM2</dc:creator>
      <pubDate>Tue, 04 Jul 2017 08:13:41 +0000</pubDate>
      <link>https://forem.com/legolord208/comparing-error-handling-in-java-go-and-rust</link>
      <guid>https://forem.com/legolord208/comparing-error-handling-in-java-go-and-rust</guid>
      <description>&lt;p&gt;Error checking is everywhere. Sometimes it's more complex than other time. Take file managing for example. You have to remember to close the file every time you return... If it's not automatic.&lt;/p&gt;

&lt;p&gt;Let's see how Java does it. First, we'll look at Java 6-.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;BufferedReader&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BufferedReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FileInputStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test.txt"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="c1"&gt;// read file&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// closed by finally block&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// closed by finally block&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do proper error handling&lt;/span&gt;
    &lt;span class="c1"&gt;// closed by finally block&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// do proper error handling&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"File read"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh my God. OK, that is HORRIBLE. Let's see how Java 7+ does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BufferedReader&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BufferedReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FileInputStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test.txt"&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// read file&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// automatically closed&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// automatically closed&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do proper error handling&lt;/span&gt;
    &lt;span class="c1"&gt;// automatically closed&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"File read"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is... very good. Congrats, Java. Let's see how Go does it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// no semicolon :(&lt;/span&gt;
                                 &lt;span class="c"&gt;// i love semicolons :(&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// do proper error handling&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c"&gt;// read file&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// closed by defer&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"File read"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c"&gt;// File closed on return, so you probably wanna return ASAP&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One thing to note with Go: Independent error handling. You can't handle all errors clumped together. This is GOOD. This let's you specify where exactly it went wrong. Errors in Go are also pretty much strings. You make a custom error with errors.New("oh no"). Good stuff&lt;br&gt;
Now let's take a look at Rust.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c"&gt;// Start a new scope. You don't *have* to do this&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="nn"&gt;File&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c"&gt;// do proper error handling&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="c"&gt;// read file&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c"&gt;// out of scope - automatically closed&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// out of scope - automatically closed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"File read"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After writing this, I had to look at the code again, thinking "that's it".&lt;br&gt;
Like you can see, I clearly like Rust's the most. Java 7's is also good. Go's is good, but defer statements only execute on function end, not scope based. You can of course close it manually, but that sucks too.&lt;br&gt;
On the other hand, Go error types are amazing to work with. &lt;code&gt;errors.New("custom error")&lt;/code&gt; is amazing. Java's is not too bad, but Rust's is "worst" for us lazy people, but also the most powerful. Plus there are macros that fix this.&lt;/p&gt;

</description>
      <category>java</category>
      <category>go</category>
      <category>rust</category>
    </item>
    <item>
      <title>Using Travis CI with Discord</title>
      <dc:creator>jD91mZM2</dc:creator>
      <pubDate>Tue, 13 Jun 2017 09:42:06 +0000</pubDate>
      <link>https://forem.com/legolord208/using-travis-ci-with-discord</link>
      <guid>https://forem.com/legolord208/using-travis-ci-with-discord</guid>
      <description>&lt;p&gt;It's a little known fact that Travis CI &lt;a href="https://github.com/travis-ci/travis-tasks/pull/71" rel="noopener noreferrer"&gt;does not support Discord&lt;/a&gt;.&lt;br&gt;
A long time it has bothered me, but never enough to do anything about it... Until now.&lt;br&gt;
So I decided to take &lt;a href="https://github.com/Techtony96/TravisCI-Webhooks/" rel="noopener noreferrer"&gt;Techtony96's PHP script&lt;/a&gt;, convert it to my &lt;a href="https://legolord208.github.io/krakeone/" rel="noopener noreferrer"&gt;Go webserver&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It's simple to use!&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fi.imgur.com%2FQNSWghp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fi.imgur.com%2FQNSWghp.png" alt="Imgur"&gt;&lt;/a&gt;&lt;br&gt;
Example: &lt;a href="https://krake.one/travis-ci/123456789123456789/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" rel="noopener noreferrer"&gt;https://krake.one/travis-ci/123456789123456789/abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

</description>
      <category>travis</category>
      <category>ci</category>
      <category>discord</category>
    </item>
    <item>
      <title>Benefits of Rust</title>
      <dc:creator>jD91mZM2</dc:creator>
      <pubDate>Tue, 23 May 2017 15:49:18 +0000</pubDate>
      <link>https://forem.com/legolord208/benefits-of-rust</link>
      <guid>https://forem.com/legolord208/benefits-of-rust</guid>
      <description>&lt;p&gt;As you might have noticed, Go seems to be quite the star right now. Strangely enough, I just started with it right before I suddenly heard it everywhere.&lt;/p&gt;

&lt;p&gt;And I mean, it's a fantastic language. It was, in fact, my favorite language. Still is one of the top ones. I made a pretty huge application in it actually, which has gotten a lot of love for being from me (I'm pretty damn unknown to be honest).&lt;/p&gt;

&lt;p&gt;So Rust then, what is it? Why am I talking about it now? Right from the start actually, my buddy &lt;a href="https://dev.to/tbodt"&gt;@tbodt&lt;/a&gt; seemed fashinated about the language. None of us ever dugged very deep into it, as far as I know. I was busy with Go, pretty much.&lt;/p&gt;

&lt;p&gt;Anyways, since I'm not professionally schooled and only a little teen hobbyist, I only just heard about data races. Of course, I hurried to update most of my applications. Including my biggest one. (All goes fine until I suddenly seem to get a goroutine leak for no apparent reason thanks to the new mutexes I put in place.)&lt;/p&gt;

&lt;p&gt;Then I was a little bored, I looked into Rust.&lt;br&gt;
My other buddy &lt;a href="https://github.com/nstafie"&gt;@nstafie&lt;/a&gt;, who I knew from the &lt;a href="https://github.com/bwmarrin/discordgo"&gt;discordgo&lt;/a&gt; community, talked highly about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  That's when I got into it.
&lt;/h2&gt;

&lt;p&gt;Deciding to actually make a test project, I read the initial tutorial and learned as much as I could and made the tiniest test ideas I could think of.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go vs Rust????
&lt;/h2&gt;

&lt;p&gt;Remember my good ol' program that is a little buggy? I got the idea of rewriting it in Rust. Of course, I definitely wouldn't want to do that huge thing, but... yeah it was a nice thought.&lt;br&gt;
Until one day, I get convinced by the discordgo community to rewrite my a little crappy code. So would I do it in Go?&lt;br&gt;
Rust.&lt;br&gt;
I chose Rust because it has a few tricks up it's sleeve compared to Go.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Macros. They are probably the most underrated things I've ever seen in programming. Ever wished you were able to return a function from a function? Well, macros can do just that. Plus they trade space for speed. But let's be honest, people have infinite hard drives now-a-days. Still, be careful.&lt;/li&gt;
&lt;li&gt;Generics. This might not be something I use myself, but because of generics, it's possible for the standard library to add slightly higher functions like contains and amazing stuff.&lt;/li&gt;
&lt;li&gt;Control. Barely any type implements the &lt;code&gt;Copy&lt;/code&gt; "trait" (a.k.a. Rust's &lt;code&gt;interface&lt;/code&gt;). While this makes you pull your hair out, it also makes you aware of when copies are needed, and you can see if you can avoid them. And even if you can't, which is most of the time, you can just add &lt;code&gt;.clone()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But that wasn't the biggest reason&lt;/p&gt;

&lt;h2&gt;
  
  
  Compile time errors and warnings
&lt;/h2&gt;

&lt;p&gt;One of the really cool things I saw when using Rust, was when I did something pretty stupid and compared if user input stored in an unsigned int was &lt;code&gt;&amp;lt; 0&lt;/code&gt;. Guess what? Rust warned me about useless check!!&lt;/p&gt;

&lt;p&gt;One of the best advantages with compiled languages over just-in-time compiled or interpreted ones, just got a million times better. In fact, during my whole rewrite, I haven't gotten a runtime panic one single time until today (when I tried to use a function result in a macro directly... oops)&lt;br&gt;
This goes to show that you suffer for your users.&lt;/p&gt;

&lt;p&gt;Of course, I still barely understand the error messages. While they attempt to be helpful, they are still a little big and bulky, and not always really very accurate to what you want.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go and Rust????
&lt;/h2&gt;

&lt;p&gt;Don't tell me I've switched to Rust. While we both know that's true, I like to see myself as somebody who would not be afraid of switching languages per project.&lt;br&gt;
Go is definitely a cool language, and is still great for a lot of things.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go is definitely easier to understand.&lt;/li&gt;
&lt;li&gt;Go has a lot of things built in, which Rust doesn't. The biggest missing built-in is signal handling but other missing built-ins include lazy static, http support and parsing command line arguments and JSON.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But I loved Go. What could Rust possibly have done that could make me swi-&lt;br&gt;
I didn't.&lt;br&gt;
You might not see it, but Rust and Go are amazingly similar compared to other languages.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understandable type naming. Seriously, I didn't know what the difference between &lt;code&gt;float&lt;/code&gt; and &lt;code&gt;double&lt;/code&gt; was until Go and Rust came around and said it's &lt;code&gt;f32&lt;/code&gt;/&lt;code&gt;float32&lt;/code&gt; and &lt;code&gt;f64&lt;/code&gt;/&lt;code&gt;float64&lt;/code&gt; (where the latter is Go.)&lt;/li&gt;
&lt;li&gt;Type AFTER the name. Not the biggest deal, but it would make sense figuring out what type a variable is after figuring out which variable.&lt;/li&gt;
&lt;li&gt;Return type last. Same as above.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion.
&lt;/h1&gt;

&lt;p&gt;Sorry for making you read so much. Jeez!&lt;br&gt;
In short, Rust is &lt;code&gt;C + Go + convenience + (annoyance * 10)&lt;/code&gt;.&lt;br&gt;
Definitely useful for anything, but not always the right language of choice.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/discordconsole-team/DiscordConsole/tree/rewrite"&gt;The rewrite I were talking about can be see here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Have fun!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>go</category>
      <category>compiler</category>
      <category>errors</category>
    </item>
    <item>
      <title>Implementing translations</title>
      <dc:creator>jD91mZM2</dc:creator>
      <pubDate>Fri, 14 Apr 2017 06:18:58 +0000</pubDate>
      <link>https://forem.com/legolord208/implementing-translations</link>
      <guid>https://forem.com/legolord208/implementing-translations</guid>
      <description>&lt;p&gt;As a hobby programmer, everything is your responsibility. Front end, back end, all of that.&lt;br&gt;
So when a friend requested translations, I got a lot of work to do.&lt;/p&gt;

&lt;p&gt;Let me give you some background. I started working on &lt;a href="https://github.com/LEGOlord208/DiscordConsole/"&gt;DiscordConsole&lt;/a&gt; some time ago. It's a program to take control over Discord bots, but also works for users and webhooks. It can nearly do every function Discord can, so that's the biggest issue. Every function creates new strings that needs translation.&lt;br&gt;
Even with some heavy vim macros (@a to add current string to a file, @b to add current file entry back to the string), it became extremely tedious.&lt;/p&gt;

&lt;p&gt;Now, this post isn't about how to make a simple translation system, or anything.&lt;br&gt;
It's about &lt;em&gt;when&lt;/em&gt; to start thinking about translations. C'mmon, don't make my mistake. I sat hours just moving and translating many different files, one being 1500 lines long!&lt;br&gt;
Common signs you need to start thinking about translations are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your application is becoming unexpectedly big.&lt;/li&gt;
&lt;li&gt;You start to write multiple similar error messages.&lt;/li&gt;
&lt;li&gt;Similar messages become different (because you automatically vary them more and more)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Better start early than late...&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This was my first post. It was very nervous to write, and I feel it's kinda useless too... So umm... Keep that in mind I guess... in case it was awful...&lt;/em&gt; &lt;/p&gt;

</description>
      <category>translations</category>
    </item>
  </channel>
</rss>
