<?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: Robby Ronk</title>
    <description>The latest articles on DEV Community by Robby Ronk (@robbyronk).</description>
    <link>https://dev.to/robbyronk</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%2F115142%2F4b4fb05a-c9b5-4dc2-b252-87e2f76b7912.jpeg</url>
      <title>DEV Community: Robby Ronk</title>
      <link>https://dev.to/robbyronk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/robbyronk"/>
    <language>en</language>
    <item>
      <title>Tidy Redux Reducers with Lodash FP</title>
      <dc:creator>Robby Ronk</dc:creator>
      <pubDate>Wed, 24 Jul 2019 10:49:46 +0000</pubDate>
      <link>https://dev.to/robbyronk/tidy-redux-reducers-with-lodash-fp-3fke</link>
      <guid>https://dev.to/robbyronk/tidy-redux-reducers-with-lodash-fp-3fke</guid>
      <description>&lt;p&gt;Lodash is the library I reach for most when writing JavaScript. In this post I'll show you how the functional programming (FP) build of Lodash can really tidy up your reducers.&lt;/p&gt;

&lt;p&gt;To get started, import the functions that we'll use: &lt;code&gt;import {set, update, flow} from 'lodash/fp';&lt;/code&gt;. Notice these are being imported from &lt;code&gt;'lodash/fp'&lt;/code&gt;, not &lt;code&gt;'lodash'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then have a look at this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SET&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;some.deep.key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;INCREMENT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;some.deep.key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;i&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="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;FOO&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;flow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;some.deep.key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;another.deep.key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;i&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="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;state&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;Keen users of Lodash will see that the arguments for &lt;code&gt;set&lt;/code&gt; and &lt;code&gt;update&lt;/code&gt; are jumbled around! In the branch for &lt;code&gt;FOO&lt;/code&gt; we only pass in two arguments, which is odd since we passed in three arguments earlier! What's going on?&lt;/p&gt;

&lt;p&gt;The arguments are in a different order due to the FP build of Lodash. This build is not as well documented as the standard one, which is a bummer, but there are some docs available &lt;a href="https://github.com/lodash/lodash/wiki/FP-Guide"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The lodash/fp module promotes a more functional programming (FP) friendly style by exporting an instance of lodash with its methods wrapped to produce immutable auto-curried iteratee-first data-last methods.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hold on! It's not as scary as it sounds.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;immutable&lt;/em&gt;: Reducers must not modify the state, instead they must return a new state. Lodash FP and Redux go together like PB&amp;amp;J!&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;auto-curried&lt;/em&gt;: A curried function takes a strict number of arguments. If it's given any less, it returns another function that takes the rest of the arguments before returning a value.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;iteratee-first&lt;/em&gt;: the "iteratee" is one of Lodash's superpowers. In our example, it's the dotted path inside of an object (our slice of state).&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;data-last&lt;/em&gt;: the "data" in our examples is &lt;code&gt;state&lt;/code&gt;. In normal Lodash, the data is usually the first argument.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If &lt;code&gt;set&lt;/code&gt; is given a path and a value but not the state, it will return a function. That returned function takes one argument, the state, and then returns the new state. That's currying!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;setNameToRobby&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Robby&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;setNameToRobby&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Robert&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wellington&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; { name: 'Robby', location: 'Wellington' }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Often I find that a given action will do a few things in a single reducer. This is where &lt;code&gt;flow&lt;/code&gt; and curried functions comes in handy. &lt;code&gt;flow&lt;/code&gt; takes a number of functions and returns a single function that composes them all together. A number of &lt;code&gt;set&lt;/code&gt;s and &lt;code&gt;update&lt;/code&gt;s (and others you might find useful in Lodash FP) can be given to &lt;code&gt;flow&lt;/code&gt; as arguments and flow will produce a single function. That single takes in &lt;code&gt;state&lt;/code&gt; and gives it to the first of the original functions, and the value out of the first feeds into the second and so on.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;flow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;some.deep.key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;another.deep.key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;i&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="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// is equal to:&lt;/span&gt;
&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;another.deep.key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;i&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="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;some.deep.key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
   &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
   &lt;span class="nx"&gt;state&lt;/span&gt;
  &lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// and quite a bit more readable!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>functional</category>
      <category>redux</category>
      <category>javascript</category>
    </item>
    <item>
      <title>There's Such A Thing As Too Many Open Files</title>
      <dc:creator>Robby Ronk</dc:creator>
      <pubDate>Tue, 23 Jul 2019 08:33:42 +0000</pubDate>
      <link>https://dev.to/robbyronk/there-s-such-a-thing-as-too-many-open-files-ien</link>
      <guid>https://dev.to/robbyronk/there-s-such-a-thing-as-too-many-open-files-ien</guid>
      <description>&lt;p&gt;I learned something new about Linux today! I ran into a problem when an OS process opened too many files. It turns out that also prevents opening a new socket while making network connections. The limit for how many files a process can open includes sockets because they are both &lt;em&gt;&lt;a href="https://unix.stackexchange.com/a/157353"&gt;kernel handles&lt;/a&gt;&lt;/em&gt;. The default limits were a little low for me so I wanted to raise the limits, not only per process but across the whole system.&lt;/p&gt;

&lt;p&gt;To check the limit per process, run &lt;code&gt;ulimit -n&lt;/code&gt;. 1024 in my case.&lt;/p&gt;

&lt;p&gt;To change it, add these two lines to &lt;code&gt;/etc/security/limits.conf&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* soft nofile 4096
* hard nofile 10240
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The soft limit is the number files a process can open normally. The hard limit is the maximum amount a user can configure, in case they temporarily need more resources. The first parameter is the user the limit will apply to and a &lt;code&gt;*&lt;/code&gt; can be used to change the limit for all users.&lt;/p&gt;

&lt;p&gt;To change the limits for the entire system, add &lt;code&gt;fs.file-max = 1048576&lt;/code&gt; to &lt;code&gt;/etc/sysctl.conf&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That was a fun one to track down!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
