<?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>DEV Community: Nimai</title>
    <description>The latest articles on DEV Community by Nimai (@nimai).</description>
    <link>https://dev.to/nimai</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%2F395436%2Ffa2e869f-2aea-4fc5-ad93-976c7488ed9e.jpg</url>
      <title>DEV Community: Nimai</title>
      <link>https://dev.to/nimai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nimai"/>
    <language>en</language>
    <item>
      <title>Rust Style Expressions in Other Programming Languages</title>
      <dc:creator>Nimai</dc:creator>
      <pubDate>Wed, 02 Jun 2021 17:10:10 +0000</pubDate>
      <link>https://dev.to/nimai/rust-style-expressions-in-other-programming-languages-1ka0</link>
      <guid>https://dev.to/nimai/rust-style-expressions-in-other-programming-languages-1ka0</guid>
      <description>&lt;p&gt;Lets say we have a variable &lt;code&gt;animal&lt;/code&gt; and we want to initiate the value of a variable &lt;code&gt;sound&lt;/code&gt; based on the value of &lt;code&gt;animal&lt;/code&gt;. In JavaScript, we could do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sound;
if (animal === 'dog') {
    sound = 'barks';
} else if (animal === 'cat') {
    sound = 'meows';
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this solution works as expected, if you're obsessed with functional programming like I am, the use of &lt;code&gt;let&lt;/code&gt; in a codebase that otherwise adheres to FP concepts as much as possible might bother you.&lt;/p&gt;

&lt;p&gt;The immutable solution in this case is actually pretty simple though. We can just use a ternary operator in the assignment for &lt;code&gt;sound&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sound = animal === 'dog' ? 'barks' : 'meows'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, this kinda works. The problem is that &lt;code&gt;sound&lt;/code&gt; is assigned &lt;code&gt;'meows'&lt;/code&gt; as long &lt;code&gt;animal&lt;/code&gt; isn't &lt;code&gt;'dog'&lt;/code&gt;. So even if &lt;code&gt;animal&lt;/code&gt; was &lt;code&gt;'pig'&lt;/code&gt;, &lt;code&gt;sound&lt;/code&gt; would be assigned &lt;code&gt;'meows'&lt;/code&gt;! Also, what do we do if we have more than two types of animals? We could start nesting ternary operators, but that would get ugly quickly!&lt;/p&gt;

&lt;p&gt;Now the Rust programming language has a syntactically clean solution to this problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* variables are immutable by default in Rust */
let sound = {
    if animal == "dog" {
        "barks"
    } else if animal == "cat" {
        "meows"
    } else if animal == "pig" {
        "oinks"
    } else {
        "not valid animal"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expressions inside braces in Rust can evaluate to values. This means you can use control flow key words like if-else statements and loops during the assignment of variables.&lt;/p&gt;

&lt;p&gt;Now how do we go about doing something similar in other programming languages like JavaScript? Well, we can use anonymous functions and immediately call them on assignment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sound = (() =&amp;gt; {
    if (animal === "dog") {
        return "barks"
    } else if (animal === "cat") {
        return "meows"
    } else if (animal === "pig") {
        return "oinks"
    }
})()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! And it doesn't look that bad either, no?&lt;/p&gt;

</description>
      <category>rust</category>
      <category>javascript</category>
      <category>functional</category>
    </item>
    <item>
      <title>Yet another guide on backing up dotfiles</title>
      <dc:creator>Nimai</dc:creator>
      <pubDate>Sun, 04 Oct 2020 10:45:54 +0000</pubDate>
      <link>https://dev.to/nimai/yet-another-guide-on-backing-up-dotfiles-3be6</link>
      <guid>https://dev.to/nimai/yet-another-guide-on-backing-up-dotfiles-3be6</guid>
      <description>&lt;p&gt;A few months ago I came across this &lt;a href="https://www.atlassian.com/git/tutorials/dotfiles"&gt; article &lt;/a&gt; which explained how a bare git repository can be used to backup dotfiles in the *NIX home directory. I messed around with it for a while and finally ended up with a no-nonsense system for managing system dotfiles.&lt;/p&gt;

&lt;p&gt;A bare git repository is almost like a regular git project that you might create with &lt;code&gt;git init&lt;/code&gt;. The only difference is that it doesn't have a specific working tree but &lt;em&gt;only&lt;/em&gt; the actual git repo (i.e. the .git folder).&lt;/p&gt;

&lt;h1&gt;
  
  
  Step One: Initializing the git repository
&lt;/h1&gt;

&lt;p&gt;This is as simple as running the commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir "${XDG_CONFIG_HOME}/cfg"
$ git init --bare "${XDG_CONFIG_HOME}/cfg/.git/"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you've used git for any period of time then you know that the &lt;code&gt;git init&lt;/code&gt; command initializes an empty repository in the folder that you're currently in.  Since we want to create a bare repository, we pass the &lt;code&gt;--bare&lt;/code&gt; flag and the location of the git folder. This should create a &lt;code&gt;.git&lt;/code&gt; folder in &lt;code&gt;~/.config/cfg&lt;/code&gt;. If you inspects its contents you'll find that they are similar to any other repository's &lt;code&gt;.git&lt;/code&gt; folder.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step Two: Accessing the repository via an alias/function
&lt;/h1&gt;

&lt;p&gt;Add the following line to your .zshrc or .bashrc along with the rest of your aliases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cfg() { git --git-dir="${XDG_CONFIG_HOME}/cfg/.git/" --work-tree="$HOME" "$@" ; }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function will allow us to stage and commit files to our backup repository.  The &lt;code&gt;--git-dir&lt;/code&gt; flag specifies the git repository we made in step one and &lt;code&gt;--work-tree&lt;/code&gt; will allow us to track any file in our &lt;code&gt;$HOME&lt;/code&gt; directory using the repo.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step Three: Ignoring files we don't want to backup
&lt;/h1&gt;

&lt;p&gt;Run &lt;code&gt;exec $SHELl&lt;/code&gt; or restart your terminal to make the alias we made in the previous step available. Then, run the following command, so that the repository doesn't display the hundreds of files in your home directory every time you check it's &lt;code&gt;status&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cfg config --local status.showUntrackedFiles no
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step Four: Setup a remote repository
&lt;/h1&gt;

&lt;p&gt;Create an empty remote repository (I will be using GitHub) and get its SSH or HTTP. On GitHub this is immediately available after the repo is created.  Finally add the remote with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cfg remote add origin &amp;lt;URL&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point you can simply do something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cfg add ~/.vimrc
$ cfg commit -m "Adding .vimrc"
$ cfg push origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will make the repository start tracking your .vimrc and you can commit and push the file to GitHub. Pretty much all git commands like &lt;code&gt;status&lt;/code&gt; and &lt;code&gt;diff&lt;/code&gt; should now be available to you with the &lt;code&gt;cfg&lt;/code&gt; function.&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting your dotfiles on a new system:
&lt;/h1&gt;

&lt;p&gt;Now all you have to do on a new system is run the following commands and you'll have all your settings back:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init --bare "${XDG_CONFIG_HOME}/cfg/.git/"
cfg() { git --git-dir="${XDG_CONFIG_HOME}/cfg/.git/" --work-tree="$HOME" "$@" ; }
cfg config --local status.showUntrackedFiles no
cfg remote add origin https://github.com/nimaipatel/dotfiles.git
cfg pull origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might get some errors if you already have, say, a .zshrc in your new system. In this case you probably would want to delete this before pulling your files from the remote.&lt;/p&gt;

&lt;p&gt;That's it we're done 🥳. Please do tell me if you face any issues with the script, in case you use it!&lt;/p&gt;

</description>
      <category>linux</category>
      <category>bash</category>
      <category>zsh</category>
      <category>unix</category>
    </item>
  </channel>
</rss>
