<?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: George Rodier</title>
    <description>The latest articles on DEV Community by George Rodier (@grodier).</description>
    <link>https://dev.to/grodier</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%2F115851%2F84564733-70c4-43dd-84b1-88c1e4055e7f.jpeg</url>
      <title>DEV Community: George Rodier</title>
      <link>https://dev.to/grodier</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/grodier"/>
    <language>en</language>
    <item>
      <title>Today I Learned: Why To Prefer Top Margins</title>
      <dc:creator>George Rodier</dc:creator>
      <pubDate>Sat, 24 Aug 2019 14:43:17 +0000</pubDate>
      <link>https://dev.to/grodier/today-i-learned-why-to-prefer-top-margins-12mb</link>
      <guid>https://dev.to/grodier/today-i-learned-why-to-prefer-top-margins-12mb</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted on my personal site, &lt;a href="https://georgerodier.com/2019-8-24-why-to-prefer-top-margins/"&gt;georgerodier.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Recently I've been looking into &lt;a href="https://tailwindcss.com"&gt;Tailwind CSS&lt;/a&gt; and how to work with the utility class CSS framework. I think it's a great fit for working with React components and hope to write about it soon. In doing research and learning more, I've been watching &lt;a href="https://twitter.com/adamwathan"&gt;Adam Wathan's&lt;/a&gt; &lt;a href="https://tailwindcss.com/screencasts/"&gt;excellent screencasts&lt;/a&gt; for designing with Tailwind CSS. In one of the screencasts, as an aside, Adam talked about why he prefers to use top margins to bottom margins when spacing elements with CSS.&lt;/p&gt;

&lt;p&gt;I've never given too much thought in the past when spacing an element. Element A needs to be spaced 10px above Element B, so I'd give Element A a bottom margin of 10px. OR maybe Element C needs to be 20px below Element D, so I'd give Element C a top margin of 20px. This is a haphazard approach that I don't recommend to anyone.&lt;/p&gt;

&lt;p&gt;If you have ever worked on styling a page, you have probably at one point or another run into issues where the page does not look as expected and discovered that &lt;a href="https://css-tricks.com/what-you-should-know-about-collapsing-margins/"&gt;collapsing margins&lt;/a&gt; were to blame. When a top margin and a bottom margin come into contact with each other, they will end up overlapping producing a single margin. As a result, many developers and frameworks end up applying a margin in one direction (either top or bottom) to avoid the potential of margins collapsing.&lt;/p&gt;

&lt;p&gt;While discussing &lt;a href="https://tailwindcss.com/course/making-text-content-feel-designed"&gt;designing a card with Tailwind CSS&lt;/a&gt;, Adam suggested that he uses top margins. The reason was intuitive and made sense to me:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When you add a top margin to the element making that element move feels better to me than when you add a bottom margin to an element and that actually pushes away other content. I like the idea that the element I'm adding a class to actually changes. - Adam Wathan&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I like the fact that the style feels like it controls the element that it's applied to versus modifying the positions of the other elements on the page. That logic resonates with me and is enough to convince me to follow a similar approach. But since I had never heard about preferring top to bottom before, I was curious if anyone else had any opinions on the matter, and in doing so, I discovered yet another reason to prefer top margins.&lt;/p&gt;

&lt;p&gt;In the blog post &lt;a href="https://matthewjamestaylor.com/css-margin-top-vs-bottom"&gt;CSS: margin top vs bottom&lt;/a&gt; by &lt;a href="https://twitter.com/mattjamestaylor"&gt;Matthew James Taylor&lt;/a&gt;, he argued that because CSS cascades only in a forward direction, only the next elements in succession can be considered in context.&lt;/p&gt;

&lt;p&gt;Matthew James Taylor demonstrated this by first showing lists of elements with top and bottom margins applied. Then he attempted to modify each list by inserting an element that required different styling. It's possible to target the next elements top from the inserted element by using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator"&gt;adjacent sibling combinator&lt;/a&gt;. You wouldn't be able to target a previous element's bottom because you can't target a proceeding element in CSS without adding additional logic elsewhere.&lt;/p&gt;

&lt;p&gt;I'd read the article for a better explanation, but the conclusion is that modifying a top margin allows us to better override elements to a general purpose CSS principle:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If a style can be applied to one of many elements, always use the last possible element in the HTML source to allow overriding by context. - Matthew James Taylor&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As a result of the arguments I've discovered, I think I'm a top margin convert and will be actively looking to use the top margin rather than the bottom margin when possible. I'm sure there are plenty of edge cases where a bottom margin may be needed, but this seems like a good starting point when needing to add vertical spacing.&lt;/p&gt;

</description>
      <category>css</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>A Quick Functional Refactoring Tip</title>
      <dc:creator>George Rodier</dc:creator>
      <pubDate>Thu, 23 May 2019 18:13:32 +0000</pubDate>
      <link>https://dev.to/grodier/a-quick-functional-refactoring-tip-4ifb</link>
      <guid>https://dev.to/grodier/a-quick-functional-refactoring-tip-4ifb</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted on my personal site, &lt;a href="https://georgerodier.com/2019-5-23-quick-functional-refactor/"&gt;georgerodier.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There's a ton of benefits to making your code more functional. However, there is a learning curve in getting started with functional programming. Luckily, with JavaScript, you can slowly adapt some functional practices and don't need to go full functional all at once.&lt;/p&gt;

&lt;p&gt;One of the best things you can do is slowly start to refactor certain parts of your application until you can gain a better understanding. In a &lt;a href="https://dev.to/grodier/array-methods-the-gateway-to-functional-programming-f29"&gt;previous post&lt;/a&gt;, I talked about how refactoring for loops into array methods is a great first step that can teach you a ton about functional programming. However, that's not the only easy thing you can do to improve your codebase. The following video from &lt;a href="https://twitter.com/ericnormand"&gt;Eric Normand&lt;/a&gt; provides a great tip for making your existing code more functional.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/3mbTa2wgmTU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The tip is great and easier than you would think to implement.&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;var&lt;/span&gt; &lt;span class="nx"&gt;myAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;George&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;nameAndDecadeString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myAge&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nameAndDecadeString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nx"&gt;myAge&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;decade&lt;/span&gt;
      &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;myAge&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old`&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is older than &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;decade&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and less than &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;decade&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;nameAndDecadeString&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nameAndDecadeString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// George is older than 30 and less than 40&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this example of a hypothetical existing codebase, myAge and myName are global mutable variables. They are both defined outside the scope of the &lt;em&gt;nameAndDecadeString&lt;/em&gt; function where they are referenced. The function doesn't mutate these variables and is only reading them. This is still problematic because the function will return a different result depending on when you read those values. They could be updated at some point by a user interaction thus changing what the function may return. To test this, we would need to set up mocks or globals that would make the tests brittle.&lt;/p&gt;

&lt;p&gt;All that is needed to make &lt;em&gt;nameAndDecadeString&lt;/em&gt; more functional is to take the global variables (myAge and myName) and make them arguments to the function.&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;var&lt;/span&gt; &lt;span class="nx"&gt;myAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;George&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;nameAndDecadeString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;decade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nameAndDecadeString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;decade&lt;/span&gt;
      &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old`&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is older than &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;decade&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and less than &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;decade&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;nameAndDecadeString&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nameAndDecadeString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;myAge&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// George is older than 30 and less than 40&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that the parameters have been changed, the &lt;em&gt;nameAndDecadeString&lt;/em&gt; function can read in the global variables as arguments. The global variables could still be modified unexpectedly, but the code within the scope of &lt;em&gt;nameAndDecadeString&lt;/em&gt; is no longer relying on them. The overall functionality hasn't changed, but the code is now more testable and more functional.&lt;/p&gt;

&lt;p&gt;One of the principles of functional programming is writing &lt;strong&gt;pure functions&lt;/strong&gt;. Pure functions are any functions that will always produce the same outputs for the same inputs. Previously, &lt;em&gt;nameAndDecadeString&lt;/em&gt; didn't take any inputs, but might produce different outputs every time its called depending on the values of myAge and myName at any given time. Now that they are passed in as arguments, the function will produce the same output every time the same age and name are passed into the function. It no longer relies on global state.&lt;/p&gt;

&lt;p&gt;The goal now is to slowly refactor your codebase following this approach. Your codebase won't be fully functional, but it is a good first step to having more reliable and testable code.&lt;/p&gt;

</description>
      <category>functional</category>
      <category>programming</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Array Methods: The Gateway to Functional Programming</title>
      <dc:creator>George Rodier</dc:creator>
      <pubDate>Fri, 10 May 2019 20:15:50 +0000</pubDate>
      <link>https://dev.to/grodier/array-methods-the-gateway-to-functional-programming-f29</link>
      <guid>https://dev.to/grodier/array-methods-the-gateway-to-functional-programming-f29</guid>
      <description>&lt;p&gt;Functional programming seems to be catching on within the JavaScript community thanks to frameworks such has React and Redux. Having more readable and testable code is a no brainer. But words like functors, currying, higher order functions, and monads can be intimidating to someone looking to jump in. Luckily, you don't need to know everything to get started!&lt;/p&gt;

&lt;p&gt;My favorite thing about JavaScript as a programming language is that it is multiparadigm. It supports working with an object oriented paradigm through prototypical inheritance and factory functions. This is what most JavaScript programmers are familiar with. However, functions are also first-class citizens in JavaScript. This means a function can act like any other object. They can be assigned to variables, passed in as an argument to a function, and even returned as a value from a function. This is important because it means functional programming is also supported in JavaScript.&lt;/p&gt;

&lt;p&gt;The best part about JavaScript supporting object oriented and functional programming paradigms is they are not mutually exclusive. You can mix and match depending on your goals. This will also allow you to dip your toes into the functional world without having to fully commit. By focusing on data manipulation and working with array methods, you can develop a solid functional foundation on which to build upon.&lt;/p&gt;

&lt;p&gt;Before jumping into data manipulation and array methods, I wanted to provide a brief definition of functional programming from &lt;a href="https://twitter.com/_ericelliott"&gt;Eric Elliott's&lt;/a&gt; &lt;a href="https://medium.com/javascript-scene/master-the-javascript-interview-what-is-functional-programming-7f218c68b3a0"&gt;blog series on composing software&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Functional programming is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. Functional programming is declarative rather than imperative, and application state flows through pure functions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The definition contains a couple of key topics to functional programming such as pure functions, immutability, declarative style, and composition that will come up when exploring data manipulation and array methods more in depth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Manipulating Data as a Starting Point
&lt;/h2&gt;

&lt;p&gt;This may be an oversimplification, but programming involves doing three different things: acquiring input, manipulating data, and posting output. The input could be the result of reading from a database, a user triggering an event, or an API call. Likewise, posting output could be to an API, a new file, or manipulating the DOM.&lt;/p&gt;

&lt;p&gt;Part of functional programming is eliminating side effects from your functions. A &lt;strong&gt;side effect&lt;/strong&gt; is anything that manipulates some sort of state or variable outside it's own scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addToX&lt;/span&gt;&lt;span class="p"&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;x&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the function addToX there is a clear side effect as x is modified outside the scope of the function. However, in addTwo, console.log is also a side effect, because there is an observable change (something being printed to the screen) happening outside the scope of what is returned from the function. In fact, any function that calls a function with a side effect is also said to have a side effect.&lt;/p&gt;

&lt;p&gt;Side effects can make posting data very difficult to reason about from a functional perspective. However, manipulating data shouldn't have any side effects. You get an input, you do something with that input, and you return an output. Given the same set of inputs, the same outputs should always be produced. If you can do that and not produce any side effects, your functions doing the data manipulation are said to be &lt;strong&gt;pure functions&lt;/strong&gt; and you'll be hitting on another primary pillar of functional programming!&lt;/p&gt;

&lt;h2&gt;
  
  
  Manipulating Data with Array Methods
&lt;/h2&gt;

&lt;p&gt;Manipulating data usually involves iterating over something, modifying data, filtering out unnecessary data, or transforming data to a different shape. Many times, this is achieved through for loops such as the ones seen below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// transform each item in an array&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&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;0&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;lt;&lt;/span&gt; &lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;newValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&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;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&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="c1"&gt;// filter out select values&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&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;0&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;lt;&lt;/span&gt; &lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&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;2&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="c1"&gt;// transform data from array to string&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myCatString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&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;0&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;lt;&lt;/span&gt; &lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;seperator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myCatString&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;,&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="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;myCatString&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s2"&gt;`Cat &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]}${&lt;/span&gt;&lt;span class="nx"&gt;seperator&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;The for loops above are just a couple small examples of what can be done when iterating through a set of data. However, there's a problem. If I didn't leave the comment before each code block, you'd have to read through each line of the for loop to understand what it's doing. The above examples may be easy enough to understand, but you'll often find different approaches are combined. There may even be for loops within for loops filled with conditionals. If that sounds confusing, that's because it is, and trying to go back and decipher what that block of code is doing can be difficult.&lt;/p&gt;

&lt;p&gt;And that's not the only problem. The above example was careful not to modify the original array. However, there is no promise that will be true in a given for loop. I've read many for loops where they modify data outside their scope. This can lead to another set of bugs that is difficult to track down.&lt;/p&gt;

&lt;p&gt;Luckily you can solve these issues by using array methods!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&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="p"&gt;...&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myCatString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;newStr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;seperator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newStr&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;,&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="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;newStr&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;seperator&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each of the following examples is the same as the for loops above. However, by using map, filter, and reduce, I am being clear about the intention of the iterations. I can quickly see what a loop is trying to achieve without having to read through each line. They are mapping over some value to transform it, filtering to a smaller list, or reducing to another object shape. These array methods are said to be &lt;strong&gt;declarative&lt;/strong&gt; as they describe &lt;em&gt;what&lt;/em&gt; they are doing (without the need for a flow of control). This contrasts with an imperative style that is more procedural and describes &lt;em&gt;how&lt;/em&gt; things are done.&lt;/p&gt;

&lt;p&gt;Another benefit is that these array methods will return a new object (often a new array). In none of the examples am I modifying any pre-existing data. As a result, I am honoring the functional concept of &lt;strong&gt;immutability&lt;/strong&gt;. Immutability means that once an object is created, it can't be modified in any way. By keeping your structures immutable, you help to ensure that your functions stay pure and you don't introduce any side effects.&lt;/p&gt;

&lt;p&gt;Map, filter, and reduce aren't the only array methods you can use. There are tons others that you can apply as well. Be sure to check out the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Methods"&gt;documentation&lt;/a&gt; to learn more and see the browser support for the various methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Point Free Style, Closures, and Currying with Array Methods
&lt;/h2&gt;

&lt;p&gt;An important thing to take note of is that each array method takes in a function as an argument. This is a clear demonstration as a function as a first-class citizen. So, let's rewrite the functionality of our map iteration to use a reusable function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&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;n&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;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;addTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One thing you'll find often in functional programming is something called &lt;strong&gt;point free&lt;/strong&gt; style. Point free doesn't actually refer to the dot operator when accessing a property on an object, but rather the arguments to a function and not writing them where possible. If a function is an argument to another function and the parameters match up (in number and type), you don't need to pass in the arguments. In the previous example, addTwo and the anonymous function passed into map have the same parameters. In this case, you would only need to pass in addTwo without the arguments. The anonymous function isn't needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&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;n&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;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addTwo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By taking advantage of a point free style you can have code that is even more terse and declarative. However, what can you do in the case where the parameters don't match up? Assume you still wanted to add two, but you only had an add function that took in two arguments. Since the parameter list does not match up you can't use a point free style.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&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;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you might be looking at that and thinking, is there a way I can apply the two beforehand, so I can use a point free style? And this is where higher order functions coupled with closure comes into play. A &lt;strong&gt;higher order function&lt;/strong&gt; is any function that either takes in or returns another function.&lt;/p&gt;

&lt;p&gt;So, the goal is going to create a new function that takes in a function and some arguments and returns a new function with those arguments partially apply applied.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;partiallyApply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;firstArgs&lt;/span&gt;&lt;span class="p"&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;function&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;remainingArgs&lt;/span&gt;&lt;span class="p"&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;fn&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;firstArgs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;remainingArgs&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;partialAdd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;partiallyApply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&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="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;partialAdd&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function partiallyApply takes in a function and a list of initial arguments and returns a new function that will take in any remaining args. The initial function and firstArgs are saved because they are closed over by the lexical scope of the returning function. The inner workings of closure and scope deservers an entire article to itself.&lt;/p&gt;

&lt;p&gt;You can then use the partiallyApply function to create a new partialAdd function that already has one of the arguments already applied. As a result, you are now able to line up our parameters and use a point free style!&lt;/p&gt;

&lt;p&gt;The partiallyApply function, as its name might suggest, is an example of a &lt;strong&gt;partial application&lt;/strong&gt;. Most functional libraries will already have this implemented for you. Very similar and related is &lt;strong&gt;currying&lt;/strong&gt;. Both currying and partial application take a function and create a more specific function to use. While partial application takes a function and returns a new function with a reduced number of args, currying will create a chain of new functions that each take in one argument. Currying can also be used as a way of creating a more specific function to utilize the point free style in a similar way. The following uses a currying function as it would be seen in a functional library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;add2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;curry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&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="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Chaining and Composing
&lt;/h2&gt;

&lt;p&gt;A fun side effect of map (and some other array methods returning a new array), is that you can chain multiple array methods together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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;1&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isOdd&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;multiplyByTwo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// [2, 6]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, filter will iterate over each item in the initial array and add it to a new array if the item satisfies the condition in the function passed into filter. The map function will then be called on the new array returned from filter, iterate through each of those items and perform the action described in the function passed into map. The result of map will be yet another new array being returned. The initial array is never modified.&lt;/p&gt;

&lt;p&gt;Knowing that you can chain things together, you might get the idea that you can have multiple maps that each transform the data in the array in some way. This may even look declarative:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addTwo&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;multiplyByThree&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, there is a problem here. You are now doing multiple iterations when only one is needed. Wouldn't it be nice if you could apply both mapping transformations in the same loop? Well you can! And it's easy as composing two functions together.&lt;/p&gt;

&lt;p&gt;A function is just a building block of a larger program. Often you'll see the output of one function become the input of another function. In this case you can create a new function that is the &lt;strong&gt;composition&lt;/strong&gt; of the other two functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&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;x&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;mutliplyByThree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&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;x&lt;/span&gt; &lt;span class="o"&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;addTwoMultiplyByThree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;multiplyByThree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;addTwo&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;addTwoMultiplyByThree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// num == 18&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compose function takes two or more functions and returns a new function. In the case above, when the new function, addTwoMultiplyByThree, is called, it sends it's input to the addTwo function. The output of addTwo becomes the input of multiplyByThree and so on. You can visualize the composition similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;multiplyByThree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because of this composition, you can rewrite the double map function to use one iteration instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;addTwoMultiplyByThree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;multiplyByThree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;addTwo&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;oldArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addTwoMultiplyByThree&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you get comfortable with function composition, you'll learn that it is the foundation of any functional program. You'll take small reusable pieces of functionality and compose them together to larger pieces. Those larger pieces can also be composed with other larger pieces. And before you know it you have an entire application to manipulate data in different ways.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting Small to Learn Big
&lt;/h2&gt;

&lt;p&gt;By working with array methods you'll be taking the initial baby steps to learning a lot that functional programming has to offer from pure functions, immutability, composition, declarative style, and even point free style, currying, partial application, and higher order functions. And this was accomplished without referring to any big terms such as functors or monads. Though believe it or not, you already were using functors throughout (though I'll leave that up to you to learn for now).&lt;/p&gt;

&lt;p&gt;The goal is not to pick up everything at once. Rather, the takeaway should be to start using array methods when manipulating data and seeing how you can learn functional concepts from it. Some may argue array methods do not follow a pure functional style, but by starting small and using them now, you can make big gains as a developer. And hopefully as you see improvements, you can use that as a gateway to learning more about functional programming and how to apply it to your code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
      <category>arrays</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
