<?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: Jeremy Marx</title>
    <description>The latest articles on DEV Community by Jeremy Marx (@jeremydmarx813).</description>
    <link>https://dev.to/jeremydmarx813</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%2F221850%2Fa58094ec-b135-4b30-be7a-fa0a0018632c.jpeg</url>
      <title>DEV Community: Jeremy Marx</title>
      <link>https://dev.to/jeremydmarx813</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jeremydmarx813"/>
    <language>en</language>
    <item>
      <title>PERSONAL ACCESS TOKENS ON GIT/GITHUB: A TALE OF A LESSON, MISTAKE, AND FUTURE QUEST</title>
      <dc:creator>Jeremy Marx</dc:creator>
      <pubDate>Fri, 20 Aug 2021 04:42:45 +0000</pubDate>
      <link>https://dev.to/jeremydmarx813/personal-access-tokens-on-git-github-a-tale-of-a-lesson-mistake-and-future-quest-a61</link>
      <guid>https://dev.to/jeremydmarx813/personal-access-tokens-on-git-github-a-tale-of-a-lesson-mistake-and-future-quest-a61</guid>
      <description>&lt;p&gt;Using Git/GitHub has always been a huge hole in my technical knowledge. Sure, I can add files to the staging area and make a basic commit, but anything beyond that has been beyond my comprehension.  I'd like to walk through a couple things I've learned recently, a big mistake I made, and highlight a concept that is still eluding me.  &lt;/p&gt;

&lt;h2&gt;
  
  
  REPLACING GITHUB'S DEPRECATED PASSWORD AUTHENTICATION WITH PERSONAL ACCESS TOKEN AUTHENTICATION
&lt;/h2&gt;

&lt;p&gt;A couple days ago, I sat down and did some work on a project.  When I got done making my changes, I made my commit.  When I went to push to my remote repository, I got a message in my command line that GitHub had removed password authentication and replaced it with the use of personal access tokens (read their statement about the change &lt;a href="https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/"&gt;here&lt;/a&gt;).  So, let's walk through how to set up a personal access token on GitHub, remove the original remote, and how to add a new one with your token.  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Go to your GitHub account, click on your profile picture icon in the top right corner, scroll down and click on &lt;em&gt;Settings&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On the left hand side of the screen, there is a menu bar.  Scroll down near the bottom of the menu, and click on &lt;em&gt;Developer Settings&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once again, on the left side of the screen, there is a menu bar.  Click on &lt;em&gt;Personal access tokens&lt;/em&gt;, and then click on &lt;em&gt;Generate new token&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the menu to create a token comes up, set your expiration date, set you permissions, and generate your token.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy your token down and &lt;strong&gt;KEEP IT HANDY&lt;/strong&gt;! As the prompt says, you will not be able to see this again (I deleted the one I made for this example, so don't go trying to use it to add emojis to my codebases!).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, go back to your terminal.  First thing we need to do is remove the remote so we can add it back using the token.  In the terminal, run &lt;code&gt;git remote remove origin&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git remote add origin https://&amp;lt;GITHUB_ACCESS_TOKEN&amp;gt;@github.com/&amp;lt;GITHUB_USERNAME&amp;gt;/&amp;lt;REPOSITORY_NAME&amp;gt;.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and replace the &lt;code&gt;&amp;lt;PLACEHOLDERS&amp;gt;&lt;/code&gt; with their respective value.  Then you should be free to run &lt;code&gt;git push origin main&lt;/code&gt; with no problems.  And if you go back to the screen we saw in &lt;strong&gt;Step 3&lt;/strong&gt;, you should see your new token paired with the repository, along with the expiration date and permissions.&lt;/p&gt;

&lt;h2&gt;
  
  
  MY EMBARRASSING MISTAKE
&lt;/h2&gt;

&lt;p&gt;So after I figure out how to do this, I go to repeat the process in other repositories.  Since I'm lazy, I scroll up to run the command with the access token I had just run.  I see some warning message in my terminal that I don't understand, so I brilliantly decide to use the &lt;code&gt;--force&lt;/code&gt; flag and override and push to that remote.  After doing this a few times, I go to check something on my GitHub repo homepage, and I noticed I wasn't seeing any of the commits I had just made.  After some backtracking, I realize what happened - when I scrolled up to the &lt;em&gt;remote add&lt;/em&gt; command with my token, I forgot to replace the &lt;code&gt;&amp;lt;REPOSITORY_NAME&amp;gt;&lt;/code&gt; placeholder with the current repository name that I actually wanted to push to.  I go to the previous repository homepage on GitHub, and sure enough, there were all my commits.  I had basically just overwritten the entire codebase.&lt;/p&gt;

&lt;p&gt;After a literal facepalm and a litany of swearing, I came up with a solution: go back to my local repo, and redo the push I had done earlier with the force flag.  I'm pretty sure this was not the proper way to do it, but it did fix the problem.  My GitHub remote repository reflected my local repo.  &lt;/p&gt;

&lt;p&gt;So the lesson I learned is:  be very careful when re-using commands to modify them properly!&lt;/p&gt;

&lt;h2&gt;
  
  
  NEXT GIT CONCEPT I NEED TO TACKLE
&lt;/h2&gt;

&lt;p&gt;Later that day, I was going through some old projects, and needed to make a GitHub repo to push to for a local repo.  When I made the repo, I initialized it with a README.md, since I know I need to get better at project readme's.  &lt;strong&gt;Note&lt;/strong&gt;: there is not a readme in my local repo.  When I go to set the GitHub repo as a remote, I can't push to it because of the file that is in the remote but not the local repo.  &lt;/p&gt;

&lt;p&gt;I wound up using my newly signature &lt;code&gt;--force&lt;/code&gt; flag trick and just making my README locally, but I know there is a more conventional way to do this.  Clone the repo? Use &lt;code&gt;git pull&lt;/code&gt; or &lt;code&gt;git fetch&lt;/code&gt;?&lt;br&gt;
Let me know in the comments what you think!&lt;/p&gt;

&lt;p&gt;If you liked this article, please give me a virtual like, a follow, share online, and if you &lt;strong&gt;REALLY&lt;/strong&gt; liked it, feel free to &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/jeremydmarx"&gt;Buy Me A Coffee!&lt;/a&gt;&lt;/p&gt;

</description>
      <category>github</category>
      <category>vscode</category>
      <category>git</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Create a memoized function in JavaScript</title>
      <dc:creator>Jeremy Marx</dc:creator>
      <pubDate>Fri, 07 May 2021 07:30:47 +0000</pubDate>
      <link>https://dev.to/jeremydmarx813/create-a-memoized-function-in-javascript-43pi</link>
      <guid>https://dev.to/jeremydmarx813/create-a-memoized-function-in-javascript-43pi</guid>
      <description>&lt;p&gt;One of the first software development courses I ever took involved recreating the well known JavaScript library &lt;a href="https://underscorejs.org/"&gt;Underscore.js&lt;/a&gt; from scratch. &lt;br&gt;
 Implementing more basic ones like &lt;em&gt;each&lt;/em&gt; or &lt;em&gt;map&lt;/em&gt; were manageable for me, but when we reached the more advanced ones, I was unable to keep up.  One of the functions that really gave me a lot of trouble was &lt;em&gt;memoize&lt;/em&gt;.  I figuratively banged my head up against the wall with this function for countless hours until one of my peers had to just show me how to do it.  I was definitely overthinking it, and even after my peer explained how it worked, I didn't fully understand it. &lt;br&gt;
 After encountering the memoization concept while learning React and researching more algorithmic functions, I revisited the &lt;em&gt;memoize&lt;/em&gt; function and feel like I understand the concept and implementation.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is memoize and when should you use it?
&lt;/h3&gt;

&lt;p&gt;According to the Underscore documentation, it&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Memoizes a given function by caching the computed result. Useful for speeding up slow-running computations. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Memoize&lt;/em&gt; takes a function as an argument, which is the function we are going to memoize.  &lt;em&gt;Memoize&lt;/em&gt; returns a function, which takes in an unspecified amount of arguments.  When the memoized function (the function originally passed into &lt;em&gt;memoize&lt;/em&gt;) is called, &lt;em&gt;memoize&lt;/em&gt; checks if the function has already been called with that particular set of arguments.  If so, &lt;em&gt;memoize&lt;/em&gt; will already have the result of that computation stored in its cache.  So it will look it up and return the already computed result.  If the memoized function has not yet been called with a particular set of arguments, then &lt;em&gt;memoize&lt;/em&gt; will perform the computation, store the result in its cache, and return the result.&lt;br&gt;
Why use it? Say you have a function that is really "expensive" that you will be using frequently in your program.  Instead of calling it over and over, with &lt;em&gt;memoize&lt;/em&gt;, you can save the result of a particular computation.  So if the function is called with the same set of arguments more than once, you won't have to repeat the computation.  &lt;/p&gt;
&lt;h3&gt;
  
  
  Caveats and Pre-requisites.
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;ES6 Syntax&lt;/strong&gt;.  I'm going to use all ES6 Syntax, so all functions will be arrow functions. This has implications about the execution context of the &lt;em&gt;this&lt;/em&gt; keyword, in addition to the syntax. I'll also be using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters"&gt;rest parameter&lt;/a&gt; as opposed to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments"&gt;arguments object&lt;/a&gt;, which will allow us to use JavaScript's built in Array methods more efficiently.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Closures&lt;/strong&gt;.  My favorite definition of a &lt;em&gt;closure&lt;/em&gt; is an inner function that has access to an outer function's scoped variables, even after the outer function has returned.  This will be key in implementing our &lt;em&gt;memoize&lt;/em&gt; function.  For additional information, consult the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"&gt;MDN documentation&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Function Methods/Apply&lt;/strong&gt;.  Functions are first class objects in JavaScript.  Just like Arrays, they have prototype methods.  &lt;em&gt;Apply&lt;/em&gt; is used to change the execution context of a function.  This will be key for our implementation, since we will be dealing with functions as parameters, returned functions, and using functions in different scopes. For additional information, consult the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply"&gt;MDN documentation&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Primitive vs. Complex Data Types&lt;/strong&gt;.  Our example function will only be optimized for primitive data, such as strings or numbers.  Complex data is passed by reference and would require us to implement logic that would check for objects being "deeply equal" to each other.  For a review on data types in JavaScript, consult the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures"&gt;MDN documentation&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Our Memoized Function
&lt;/h3&gt;

&lt;p&gt;Ordinarily, we'd use the memoization technique for far more complex functions, but for this example we are going to use a simple adding function that takes in an unspecified amount of numbers and adds them all together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const add = (...args) =&amp;gt; {
  return args.reduce((s, e) =&amp;gt; {
    return s += e;
  }, 0);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function uses the &lt;em&gt;rest&lt;/em&gt; parameter to collect all the arguments into an array and then uses the Array method &lt;em&gt;reduce&lt;/em&gt; to add them all together.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing Memoize
&lt;/h3&gt;

&lt;p&gt;First, &lt;em&gt;memoize&lt;/em&gt; takes in the function we want to memoize as a parameter.  Then, we need a cache to store our previously computed results.  Since we need to look up values, we'll need something with key-value pairs.  So we'll go with an object literal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const memoize = func =&amp;gt; {
  const cache = {};
}  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Memoize&lt;/em&gt; returns a function that takes in an unspecified amount of arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const memoize = func =&amp;gt; {
  const cache = {};
  return (...args) =&amp;gt; {
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are going to want to look up if the memoized function has been called with a particular set of arguments or have a way to create a key with which we can store the computation in the cache.  So let's turn the arguments into a string and store it in a function scoped variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const memoize = func =&amp;gt; {
  const cache = {};
  return (...args) =&amp;gt; {
     let strKey = args.join(',');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the &lt;em&gt;join&lt;/em&gt; method to turn all the numbers into a string we can use for lookup or storage, which is our next step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const memoize = func =&amp;gt; {
  const cache = {};
  return (...args) =&amp;gt; {
     let strKey = args.join(',');
     if(!cache[strKey]){
        cache[strKey] = func.apply(this, args);
      } 
       return cache[strKey];
   }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our &lt;em&gt;if&lt;/em&gt; statement, we check if the memoized function has &lt;strong&gt;not&lt;/strong&gt; been called/isn't present in the cache.  If that is the case, we store it in the cache using the Function prototype method &lt;em&gt;apply&lt;/em&gt; to call the memoized function in its new scope.  Remember, even though we will already be working within the global scope after the outer function is returned, we still have access to the cache because of &lt;em&gt;closures&lt;/em&gt;.&lt;br&gt;
After we perform the computation and store it, the inner function returns the result from the cache.  If the the computation is already stored in the cache, the &lt;em&gt;if&lt;/em&gt; block is skipped and the value returned.&lt;/p&gt;
&lt;h3&gt;
  
  
  Using Memoize
&lt;/h3&gt;

&lt;p&gt;Let's put this all to use and memoize our add function from earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const memoize = func =&amp;gt; {
  const cache = {};
  return (...args) =&amp;gt; {
  console.log(cache)
     let strKey = args.join(',');
      if(!cache[strKey]){
        console.log('adding to cache!');
        cache[strKey] = func.apply(this, args);
      } 
       console.log('fetching from cache!');
       return cache[strKey];
   }
}

const add = (...args) =&amp;gt; {
  return args.reduce((s, e) =&amp;gt; {
    return s += e;
  }, 0);
}

const memoizedAddFunction = memoize(add);

memoizedAddFunction(1, 2, 3);
memoizedAddFunction(1, 2, 3);
memoizedAddFunction(4, 2, 3);
memoizedAddFunction(4, 2, 3);
memoizedAddFunction(8, 2, 3);
memoizedAddFunction(1, 2, 3);
memoizedAddFunction(4, 2, 3);
memoizedAddFunction(8, 2, 3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there we have it!&lt;br&gt;
I encourage you to run this function in the JavaScript environment of your choice and add some more calls of the &lt;em&gt;memoizedAddFunction&lt;/em&gt; with some more/different numbers.  I've included some console logs in various places in &lt;em&gt;memoize&lt;/em&gt;, so you can see the computations being added or fetched from the cache.&lt;br&gt;
I hope this helps clarify a concept that gave me a lot trouble a few months ago in bootcamp. If you liked the article, please give me a like, share, or comment.  If you &lt;strong&gt;REALLY&lt;/strong&gt; liked it, help me out by &lt;a href="https://www.buymeacoffee.com/jeremydmarx"&gt;buying me a cup of coffee!&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
