<?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: Bryan Acosta</title>
    <description>The latest articles on DEV Community by Bryan Acosta (@bryaneduardoga).</description>
    <link>https://dev.to/bryaneduardoga</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%2F584652%2Fb24300fb-fddb-420e-8879-45604ff55706.png</url>
      <title>DEV Community: Bryan Acosta</title>
      <link>https://dev.to/bryaneduardoga</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bryaneduardoga"/>
    <language>en</language>
    <item>
      <title>A brief introduction to functional programming concepts in JavaScript</title>
      <dc:creator>Bryan Acosta</dc:creator>
      <pubDate>Fri, 12 Mar 2021 09:47:08 +0000</pubDate>
      <link>https://dev.to/bryaneduardoga/a-brief-introduction-to-functional-programming-concepts-in-javascript-5bg9</link>
      <guid>https://dev.to/bryaneduardoga/a-brief-introduction-to-functional-programming-concepts-in-javascript-5bg9</guid>
      <description>&lt;p&gt;Functional programming is a highly valued programming paradigm, so is a way of thinking about software construction by creating pure functions. It avoids concepts of shared state, mutable data observed in Object-Oriented Programming.&lt;/p&gt;

&lt;p&gt;But what are all these buzzwords I'm talking about?&lt;/p&gt;

&lt;p&gt;Well. As everyone knows, functions are pieces of code that can be reused once, and once again, they can receive some inputs, compute that input, and finally return an output, piece of cake, huh?&lt;/p&gt;

&lt;h2&gt;
  
  
  Functional code is characterized by:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The absence of side effects.&lt;/li&gt;
&lt;li&gt;Pure functions.&lt;/li&gt;
&lt;li&gt;Stateless.&lt;/li&gt;
&lt;li&gt;First-class functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's find an easy way to define all these concepts.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a side-effect?
&lt;/h2&gt;

&lt;p&gt;Side-effects are the process of modifying any variable or object property (e.g., a global variable, or a variable in the parent function scope chain).&lt;/p&gt;

&lt;p&gt;Another side-effect would be print something into the console.&lt;br&gt;
No having side effects means that the function doesn't change the outer world.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a pure function?
&lt;/h2&gt;

&lt;p&gt;A pure function is a function which:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take its input and use that and only that to compute an output and then return it.&lt;/li&gt;
&lt;li&gt;They can't use variables either functions out of their scope.&lt;/li&gt;
&lt;li&gt;Produces no side effects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Not pure function&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjrlenyeuxygzew0wbri.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjrlenyeuxygzew0wbri.png" alt="Alt Text" width="800" height="755"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Pure function&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5z1oml3bm4xnaxhyw1rw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5z1oml3bm4xnaxhyw1rw.png" alt="Alt Text" width="800" height="636"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What does stateless mean?
&lt;/h2&gt;

&lt;p&gt;That means values don't mutate.&lt;/p&gt;

&lt;h2&gt;
  
  
  First-class functions.
&lt;/h2&gt;

&lt;p&gt;When functions are treated like any other variable.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For instance:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functions can be stored in variables.&lt;/li&gt;
&lt;li&gt;Passed around as arguments, or even returned from other functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to do functional programming?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Don't iterate, map, filter, and reduce instead.&lt;/li&gt;
&lt;li&gt;Use higher-order functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Higher-order function&lt;/strong&gt;&lt;br&gt;
A higher-order function meets at least one of the following conditions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Takes functions as input.&lt;/li&gt;
&lt;li&gt;Return another function as output.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4kxpbeiceeotr8r8nlqo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4kxpbeiceeotr8r8nlqo.png" alt="Alt Text" width="800" height="717"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Holy Trinity of Functional Programming&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;map() instead of for&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm4fe7zpdr5xti58ff2zj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm4fe7zpdr5xti58ff2zj.png" alt="Alt Text" width="800" height="659"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;map() does not mutate the array, it makes a copy.&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9p4gvm7z56fvetxnzaz6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9p4gvm7z56fvetxnzaz6.png" alt="Alt Text" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;filter()&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc5u7x3k6g50tbf6x8shb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc5u7x3k6g50tbf6x8shb.png" alt="Alt Text" width="800" height="525"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;reduce() takes an array and return just one reduced element&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu28xoxf1okvbxqeg5s7m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu28xoxf1okvbxqeg5s7m.png" alt="Alt Text" width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of functional programming.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;They don't raise any side-effects.&lt;/li&gt;
&lt;li&gt;Easy to refactor.&lt;/li&gt;
&lt;li&gt;Better encapsulation.&lt;/li&gt;
&lt;li&gt;Increase reusability.&lt;/li&gt;
&lt;li&gt;Modularity.&lt;/li&gt;
&lt;li&gt;Easy to test.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well, that's all about this brief introduction to functional programming concepts. Thank's for reading.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;More information about map, filter, and reduce:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Array/map" rel="noopener noreferrer"&gt;map&lt;/a&gt; &lt;a href="https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Array/filter" rel="noopener noreferrer"&gt;filter&lt;/a&gt; &lt;a href="https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce" rel="noopener noreferrer"&gt;reduce&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>functional</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
