<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rochel Levi on Medium]]></title>
        <description><![CDATA[Stories by Rochel Levi on Medium]]></description>
        <link>https://medium.com/@rochlevi?source=rss-5ecc3b059ae0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*flKxc629d924r_s7aICMBA.jpeg</url>
            <title>Stories by Rochel Levi on Medium</title>
            <link>https://medium.com/@rochlevi?source=rss-5ecc3b059ae0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 00:13:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rochlevi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[JavaScript Closures]]></title>
            <link>https://medium.com/@rochlevi/javascript-closures-5afdb5fe375f?source=rss-5ecc3b059ae0------2</link>
            <guid isPermaLink="false">https://medium.com/p/5afdb5fe375f</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Rochel Levi]]></dc:creator>
            <pubDate>Tue, 23 Jan 2018 16:43:42 GMT</pubDate>
            <atom:updated>2018-01-23T16:43:42.769Z</atom:updated>
            <content:encoded><![CDATA[<p>After learning some JavaScript at the Flatiron School, I set out to write my very own stock trading simulator in JavaScript. While the program itself is still a work in progress, I did learn an invaluable lesson while working on it. Namely, closures can’t be ignored. Now, I’m talking from experience here, because I tried really hard to ignore them, but too often, when I ran into an error or bug, the suggested fix on StackOverflow was “just use a closure!”</p><h3>So What is a Closure?</h3><p>To quote mozilla.org, “a <em>closure</em> is the combination of a function and the lexical environment within which that function was declared.” But that’s less than helpful, so let’s go straight into an example.</p><pre>let closure = (function(){<br>   let counter = 0 <br>   return function(){<br>      if(counter &lt; 2){<br>          console.log(&quot;you&#39;re within the limit&quot;) <br>          counter++<br>      } else{<br>          console.log(&quot;you&#39;ve reached the limit&quot;)<br>      }<br>   }<br>})()</pre><p>The code snippet above looks challenging, but it’s actually fairly easy to break down. The variable ‘closure’ is being set to an immediately invoked function — in other words, as soon as closure is set, the outermost function is invoked and it is never used again. So what was the point of it? The variable ‘closure’ has now been set to the innermost function and can be invoked by calling closure(). And now, the counter variable is hidden to your entire program — but accessible to ‘closure’ for its entire lifetime, as demonstrated below.</p><pre>closure()<br>=&gt; you&#39;re within the limit</pre><pre>closure()<br>=&gt; you&#39;re within the limit</pre><pre>closure()<br>=&gt; you&#39;ve reached the limit</pre><h3>So What’s the Point?</h3><p>You’ll probably never need a function like the one above, but tweak it a little bit, and it may be useful. Don’t you ever need a counter to check how many times a certain function was invoked? You don’t want the counter to be a global variable, but if you put it inside the function, it will be reset to zero each time the function is invoked. In cases like this, a closure is great to keep your code clean and free of bugs.</p><p>Here is a pattern using closures that you may have already seen:</p><pre>let Portfolio = (function createPortfolioClass(){<br>  const all = []<br>  return class Portfolio{<br>    constructor(id, name, cashBalance){<br>      this.id = id<br>      this.name = name<br>      this.cashBalance = cashBalance<br>      all.push(this)<br>    }<br>  static all() {<br>      return [...all]<br>    }<br>  }<br>})()</pre><pre>portfolio = new Portfolio(1, &quot;portfolio1&quot;, 10,000)<br>Portfolio.all()<br>=&gt; [ Portfolio { id: 1, name: &#39;portfolio1&#39;, cashBalance: 10000 } ]</pre><p>Above, a closure is used to keep the ‘all’ variable private, but accessible to the Portfolio class. This is especially handy if you have a few classes in your program and want to make an ‘all’ variable for each.</p><p>There are many more ways you’ll want to use closures — StackOverflow users will have plenty of solutions to common issues that use closures. The first step, though is to make sure you thoroughly understand how they work on a basic level.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5afdb5fe375f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Memory Management and Javascript]]></title>
            <link>https://medium.com/@rochlevi/memory-management-and-javascript-e5b2ae7006ca?source=rss-5ecc3b059ae0------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5b2ae7006ca</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Rochel Levi]]></dc:creator>
            <pubDate>Sat, 30 Dec 2017 23:34:25 GMT</pubDate>
            <atom:updated>2017-12-30T23:34:25.604Z</atom:updated>
            <content:encoded><![CDATA[<h3>Garbage and Garbage Collection</h3><p>“Garbage” in computer science refers to memory allocated for data that is no longer needed by a program. In small amounts, garbage can be harmless, but if too much garbage is created, it can slow down or even stall a program once the computer is out of memory. Thus, memory allocated to unwanted data must be returned to the “heap” (available memory pool). There are two main approaches to this — automatic and manual. Lower level languages such as C typically opt for manual garbage collection — where developers must explicitly release previously allocated memory, while higher level languages have automatic garbage collection.</p><h3>Memory Management in Javascript</h3><p>Javascript is one of those higher level languages with automatic garbage collection. But this does <em>not </em>mean that Javascript programmers can stop reading here. Garbage collectors aren’t perfect, and so it is important that programmers understand how they work and write code that works with them.</p><p>Garbage Collectors only free up memory when there are no references in the program to that area of memory (i.e. no access to the data stored in that area of memory). For example, take this snippet of code:</p><pre>new Date()<br>=&gt; Fri Dec 29 2017 12:37:58 GMT-0500 (EST)</pre><p>memory was allocated for the new date object above, but no variable was set equal to it, and therefore it can’t be accessed again by the program. The garbage collector recognizes this, deletes the object and returns the memory to the heap.</p><h3>Memory Leak in Javascript</h3><p>While Javascript can recognize and get rid of inaccessible data, accessible data that is no longer needed sticks around. Therefore programmers must be careful to get rid of unnecessary references. This will typically happen on its own, but there are times that developers inadvertently leave references around for too long. One very common mistake is using global variable unnecessarily.</p><h3>Unnecessary Global Variables</h3><p>Consider the code below:</p><pre>function sayHello(){<br>  let myName = &quot;Rochel&quot;<br>  console.log(`Hello ${myName}`)<br>}</pre><pre>sayHello()<br>=&gt; Hello Rochel</pre><pre>myName<br>=&gt; Uncaught ReferenceError: myName is not defined</pre><p>and contrast it with the following:</p><pre>function sayHello(){<br>  myName = &quot;Rochel&quot;<br>  console.log(`Hello ${myName}`)<br>}</pre><pre>sayHello()<br>=&gt; Hello Rochel</pre><pre>myName<br>=&gt; &quot;Rochel&quot;</pre><p>In the first snippet, the variable myName was scoped to the sayHello function, and is inaccessible outside of it. Therefore, the memory it uses is freed immediately upon exiting the function. In the second snippet, myName is a global variable, available everywhere in the program once the function is run. This is typically a mistake — if the programmer wanted access to myName outside of sayHello, they would define it outside of the function. In other words, myName is probably never accessed outside of sayHello, and it is thus garbage, but unrecognizable as such to the garbage collector as it is still accessible.</p><p>There are many other ways a program can leak memory, but the fundamental concept is always the same — memory is leaked when unnecessary data sticks around. As you code, consider the scope and lifetime of objects you create, and try to minimize them as much as possible.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5b2ae7006ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RSA Public Key Cryptography: An Overview]]></title>
            <link>https://medium.com/@rochlevi/rsa-public-key-cryptography-an-overview-46464ec04bf9?source=rss-5ecc3b059ae0------2</link>
            <guid isPermaLink="false">https://medium.com/p/46464ec04bf9</guid>
            <category><![CDATA[internet-security]]></category>
            <category><![CDATA[cryptography]]></category>
            <category><![CDATA[number-theory]]></category>
            <dc:creator><![CDATA[Rochel Levi]]></dc:creator>
            <pubDate>Sun, 17 Dec 2017 21:59:05 GMT</pubDate>
            <atom:updated>2017-12-18T16:28:41.960Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Motivation</strong></h3><p>Beginners at coding often try to write a program that encrypts and decrypts messages using the <a href="http://practicalcryptography.com/ciphers/caesar-cipher/">Caesar Cipher</a>. But let’s be honest; if the Caesar Cipher was the cryptography standard, we probably wouldn’t be able use credit cards. There are, of course, more sophisticated codes — think <a href="https://en.wikipedia.org/wiki/Code_talker">Navajo Code Talkers of WWII</a>. The problem with pre-modern codes, though, is that they required both the message sender and receiver to have access to the same code-book or language. In the modern era, that would not work. Imagine if Amazon had to set up millions of distinct codes for use with each of its customers so that credit card information could be safely transmitted. And how would the code book be transmitted to the customer originally? Would the customer have to travel to Seattle to set that up?</p><p>Stated simply, in the age of the internet, we need a method of encrypting data where the encryption can’t be easily inverted and the data decrypted.</p><h3>The Solution</h3><p>It’s hard to wrap your head around a mathematical encryption tool that can’t be easily rolled back — we’re used to mathematical functions like addition that can be undone with subtraction (think Caesar’s Cipher), but in fact, these tools do exist.</p><p>Imagine that you wanted to receive secure messages from many people. You could accomplish this by sending all those people unlocked padlocks and asking them to lock their message in a box with your padlock and then send them your way. After a padlock is locked, no one but you can unlock the box — even the one who originally locked the box! RSA Public Key Cryptography works in a similar fashion, thus providing a solution to our problem.</p><h3>The Math</h3><p>One of my favorite things about RSA Public Key Cryptography is that I understand it — it’s built using number theory, which is of the more accessible fields in math (calculus not required!) There are plenty of wonderful Number Theory books out there that can teach you from the ground up, and plenty of <a href="https://en.wikipedia.org/wiki/RSA_(cryptosystem)">webpages</a> that can give you a higher level understanding of the math behind RSA Public Key Cryptography, so check them out for a deeper understanding. For an overview of the encryption/decryption procedure, keep reading.</p><p>The first step in the encryption procedure is to choose 2 very large prime numbers.</p><pre>let p be a large prime;<br>let q be a large prime;<br>let n = p·q;<br>let k = (p - 1)(k - 1);<br>let d be some integer greater than 0 which is relatively prime to k;</pre><p>If all the variables above were set to spec, then it’s been proven that there exist integers e and f such that:</p><pre>de + kf = 1;<br>equivalently, de ≡ 1 (mod k)</pre><p>The setup is now complete, and we can get on to encrypting…</p><p>First, n and e should be made public — they are the “public keys.” d must be kept private, and all record of p and q can be destroyed. Additionally, we’ll need a way to represent characters as numbers — for that, we can use the ASCII table.</p><p>Alright, time to encode your message! Replace each character in your message with its ASCII index, and then:</p><pre>let x be the ASCII representation of your message;<br>let y = x^e mod(n)</pre><p>If the ASCII representation is numerically smaller than n, all you have to do is raise it to the power of e and find y. If it is larger than n, it will have to be broken down into pieces smaller than n, and each will have to be dealt with individually.</p><p>Now, it’s been proven that given any integer b:</p><pre>b^ed ≡ b (mod n)</pre><p>and, since</p><pre>b^ed = (b^e)^d</pre><p>the message is easily decoded by the intended recipient with their secret key, d. Y must only be raised to the power of d and calculated modulo n to return the original message.</p><h3>Why Does it Work?</h3><p>As explained, we needed a method of encryption in which messages can be easily encoded, but difficult to decode. There are a few factors working in our favor here:</p><ol><li>Computers can quickly generate really large prime numbers — this is important so the code can be set up — p and q are easy to find.</li><li>Computers can do modular arithmetic quickly — necessary for quick encoding and decoding.</li><li>Computers cannot factor really large numbers quickly — this is vital, because if computers could quickly factor numbers, the code would be easily broken — n has only 2 factor — p and q. from p and q, k can be found, and with the help of e, which is public, d can be found, and the code broken.</li></ol><p>Of course, computers only implement algorithms fed into them, so what I really mean when I say that computers can or cannot do things quickly, I mean that mathematicians either have or have not discovered efficient algorithms to do those tasks. It’s important to note, however, that just because an algorithm has not yet been discovered, that doesn’t mean it cannot be discovered in the future. If an algorithm is found to quickly factor large numbers, RSA Public Key Cryptography will no longer be a secure encryption method.</p><h3>Sources:</h3><p><a href="http://www.math.clemson.edu/~jimlb/CourseNotes/intronumbertheory2007.pdf">http://www.math.clemson.edu/~jimlb/CourseNotes/intronumbertheory2007.pdf</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46464ec04bf9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Use Subqueries in SQL (and When You Shouldn’t)]]></title>
            <link>https://medium.com/@rochlevi/how-to-use-subqueries-in-sql-and-when-you-shouldnt-1431285ea456?source=rss-5ecc3b059ae0------2</link>
            <guid isPermaLink="false">https://medium.com/p/1431285ea456</guid>
            <category><![CDATA[sqlite]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Rochel Levi]]></dc:creator>
            <pubDate>Tue, 28 Nov 2017 14:52:44 GMT</pubDate>
            <atom:updated>2017-11-28T14:52:44.315Z</atom:updated>
            <content:encoded><![CDATA[<p>I’m a student at the Flatiron School, and though I came in without much of a CS background, I was expecting to coast through the SQL chapter. Before coming to Flatiron, I was a Pension Actuary for NYC, and my job involved correlating lots of really large sets of data. We used Microsoft Access to manipulate our data, and Access is built on SQL, so conceptually, I had it all down. I knew all about inner joins, right joins, left joins, grouping, ordering…</p><p>But then, I had a homework problem in which I was given a dataset of visitors to the Jon Stewart Show. The schema of the table looked like this:</p><pre>CREATE TABLE guests (<br>  id INTEGER PRIMARY KEY,<br>  year INTEGER,<br>  occupation TEXT,<br>  show TEXT,<br>  show_group TEXT,<br>  name TEXT<br>  );</pre><p>and then I was asked this:</p><pre>What was the most popular occupation of guest for each year Jon Stewart hosted the Daily Show?</pre><p>At first glance, the question looked pretty straightforward. But when I tried translating it into SQL, I got stuck. I started with this query:</p><pre>SELECT year, MAX(COUNT(occupation))<br>FROM guests<br>GROUP BY year, occupation</pre><p>…and got this error:</p><pre>misuse of aggregate function COUNT()</pre><p>Apparently, SQLite can’t get both the count and the max of that count all at once, and I had to break down my query a bit. After some Googling, I came up with an alternate (working!) solution, shown below:</p><pre>SELECT year, occupation<br>FROM (SELECT year, occupation, COUNT(occupation) AS num_occupation<br>      FROM guests<br>      GROUP BY year, occupation<br>     )<br>GROUP BY year<br>HAVING MAX(num_occupation)</pre><p>At first, I didn’t quite understand what this type of query was doing, but once I was sure I couldn’t find anything simpler, I sat down and took it apart until I understood it and was able to tweak it for my purposes.</p><p>The query is using a “subquery,” which means that instead of querying a table in the database, the outer query is querying the return value of a different query (the subquery).</p><p>In this example, the subquery returns a table with columns year, occupation, and num_occupation. Because the query is grouped by year and occupation, the return table has only one row per occupation in each year, with the number of times that occupation appeared that year recorded in the num_occupation field.</p><p>Now that we have the count stored in num_occupation, the outer query can easily pull the profession that has the max of num_occupation in each year in our outer query.</p><p>I found that breaking out the query this way was helpful not just in returning my answer, but also in helping me see how clear the query’s workings become if you’re only willing to break it down and examine it in smaller chunks.</p><p>Just a disclaimer, though — at my old job I would occasionally use subqueries and sometimes even multiple subqueries, and with some of the larger data sets, the computer became really sluggish really quickly. While there may be better solutions to this problem, my biggest takeaway from the assignment was being able to break down the code to more intuitive and manageable pieces.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1431285ea456" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Beginners Guide to Regular Expressions in Ruby]]></title>
            <link>https://medium.com/@rochlevi/a-beginners-guide-to-regular-expressions-in-ruby-fa5e56104792?source=rss-5ecc3b059ae0------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa5e56104792</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[regexp]]></category>
            <category><![CDATA[regular-expressions]]></category>
            <category><![CDATA[regex]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Rochel Levi]]></dc:creator>
            <pubDate>Sat, 11 Nov 2017 23:33:54 GMT</pubDate>
            <atom:updated>2017-11-14T02:09:14.769Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>A Beginners Guide to Regular Expressions in Ruby</strong></p><p>I’m a fairly new coder but last week, when I ran into an example where I had to parse a string of emails separated by whitespaces and/or commas, I knew I had a problem. In my few months coding, I’d already run into similar tasks a number of times and had not found an easy and direct solution.</p><p>I really wanted to solve this problem once and for all, so I started some serious Googling. I found similar problems on StackExchange, and started copying and modifying snippets I didn’t really understand. I came up with two solutions that worked, and one that did not.</p><p>These worked:</p><pre>string = &quot;john@doe.com, doe@john.com,jane@smith.com smith@jane.com&quot;</pre><pre>string.split(/[\s,]+/)<br>=&gt; [&quot;john@doe.com&quot;, &quot;doe@john.com&quot;, &quot;jane@smith.com&quot;, &quot;smith@jane.com&quot;]</pre><pre>string.split(/ |, |,/)<br>=&gt; [&quot;john@doe.com&quot;, &quot;doe@john.com&quot;, &quot;jane@smith.com&quot;, &quot;smith@jane.com&quot;]</pre><p>This did not</p><pre>string.split(/,|, | /)<br>=&gt;[&quot;john@doe.com&quot;, &quot;&quot;, &quot;doe@john.com&quot;, &quot;jane@smith.com&quot;, &quot;smith@jane.com&quot;]</pre><p>The snippets above use Regular Expressions, or “regexp” inside the split method, and in this post I’ll teach you some basic regexp syntax so that you can both build your own and understand some more complex ones.</p><p>But first, what is is a regexp?</p><blockquote>A <strong>regular expression</strong>, <strong>regex</strong> or <strong>regexp</strong> (sometimes called a <strong>rational expression</strong>) is, in theoretical computer science and formal language theory, a sequence of characters that define a <em>search pattern</em>. Usually this pattern is then used by string searching algorithms for “find” or “find and replace” operations on strings.</blockquote><blockquote>-Wikipedia</blockquote><p>The first thing you need to know in order to build your own regexp is to define it between two forward slashes. Though there are other ways to define a regexp, you will most commonly see regexp defined this way.</p><p>The next important piece of syntax is the pipe(|), which denotes “or”.</p><p>With just these two pieces of information, we can understand two of the expressions above.</p><pre>string = &quot;john@doe.com, doe@john.com,jane@smith.com smith@jane.com&quot;<br>string.split(/ |, |,/)<br>=&gt; [&quot;john@doe.com&quot;, &quot;doe@john.com&quot;, &quot;jane@smith.com&quot;, &quot;smith@jane.com&quot;]</pre><p>The expression above tells the method into which it is passed to operate on a space, or a comma followed by a space, or a comma.</p><p>The regexp below, however, looks very similar, and yet does not work as expected. Why is that?</p><pre>string = &quot;john@doe.com, doe@john.com&quot;<br>string.split(/,|, | /)<br>=&gt; [&quot;john@doe.com&quot;, &quot;&quot;, &quot;doe@john.com&quot;, &quot;jane@smith.com&quot;, &quot;smith@jane.com&quot;]</pre><p>When executing the statement above, the interpreter begins at the leftmost part of the string - in this case the letter “j.” It then searches the regexp, beginning at the left side again, to search for a match. Neither comma, nor comma-space, nor space match “j”, so the interpreter moves on to the next letter of the string, “o”. Of course, no matches are found in the string before we hit the comma. But when we hit the comma, the interpreter again begins searching the regexp left to right. It immediately finds that comma matches comma, splits on the comma, and moves on to the next character of string. Next up is a space. The space does not match the comma or comma-space, but it does match the space, and the string is split on the comma and then on the space. The second element of the return array, “”, is the result of the string being split on adjacent characters.</p><p>Swapping the order of the elements in the regexp so that the interpreter searches first for comma-space, and only searches for a comma on it’s own afterwards corrects the issue.</p><p>Utilizing the pipe is the easiest and most explicit way to build a regexp — as long as you are mindful of the order. But if your data is a bit messier and you don’t want to list all the combinations of delimiters you may need to split on, there is an easier way.</p><p>Brackets (which form “Character Classes”) can be used around the characters in your regexp instead of using pipes to separate between characters. Additionally, adding a plus sign after the brackets denotes any combination of the characters inside (including multiple of the same) the brackets. For example, imagine trying to split a string into sentences, where there may be multiple punctuation marks at the end of a sentence:</p><pre>string = &quot;Hello!!! How are you??!!&quot;<br>string.split(/[?!.]+/)<br>=&gt;[&quot;Hello&quot;, &quot; How are you&quot;]</pre><p>The brackets combined with the + neatly take care of that problem. Also note that order is not an issue here.</p><p>We do still have an extra space at the beginning of the sentence though. To fix this, we can’t add a space inside the brackets — that would tell the program to split on every space. What we <em>can</em> do, though, is chain a space after the plus sign, like this, using “\s,” which represents white space in regexp:</p><pre>string.split(/[?!.]+\s/)<br>=&gt;[&quot;Hello&quot;, &quot;How are you&quot;]</pre><p>And there we have it!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa5e56104792" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>