<?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: Kyle Bremner</title>
    <description>The latest articles on DEV Community by Kyle Bremner (@_kbremner).</description>
    <link>https://dev.to/_kbremner</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%2F241689%2F69480b2e-f82b-40cc-8827-b0c64256bdef.jpg</url>
      <title>DEV Community: Kyle Bremner</title>
      <link>https://dev.to/_kbremner</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/_kbremner"/>
    <language>en</language>
    <item>
      <title>3 lessons I learned getting started with Ramda</title>
      <dc:creator>Kyle Bremner</dc:creator>
      <pubDate>Tue, 16 Mar 2021 20:27:20 +0000</pubDate>
      <link>https://dev.to/_kbremner/3-lessons-i-learned-getting-started-with-ramda-4je5</link>
      <guid>https://dev.to/_kbremner/3-lessons-i-learned-getting-started-with-ramda-4je5</guid>
      <description>&lt;p&gt;I did a functional programming course once.&lt;/p&gt;

&lt;p&gt;As much as I tried at the time, I couldn't wrap my head around it. I'd struggle to write anything coherent in Haskell today, I still couldn't tell you what a monad is, or explain the ins and outs of the other mathematical concepts related to functional programming.&lt;/p&gt;

&lt;p&gt;With time, though, I have come to really appreciate the approach that functional programming paradigms encourage. The concepts, in many cases, lead to code that is much easier to reason about, test, compose, and adapt over time.&lt;/p&gt;

&lt;p&gt;I have a project that I've worked on for years. One of those, "I'll finish it someday" projects. I often use it to try different approaches and libraries, as it has enough complexity to make it an interesting example and I'm not too concerned about when I finish it. Sometimes, as the saying goes, it's about the journey more than the destination!&lt;/p&gt;

&lt;p&gt;In this post, I'll go through some lessons that I learned while adding &lt;a href="https://ramdajs.com" rel="noopener noreferrer"&gt;Ramda&lt;/a&gt; to that project, to help with taking a more functional approach.&lt;/p&gt;

&lt;p&gt;Ramda is a collection of functions that "makes it easy to create functional pipelines". It's pretty easy to get started with, but after a while, I did find myself learning a few lessons around how to get the most out of it.&lt;/p&gt;

&lt;p&gt;If you're looking for more of an explainer on function programming, there's plenty of other great articles for that. Here's one example:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/mr_bertoli" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F234162%2F9fb8a364-6624-483b-833b-3effafd779a4.jpg" alt="mr_bertoli"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/mr_bertoli/an-adequate-introduction-to-functional-programming-1gcl" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;An Adequate Introduction to Functional Programming&lt;/h2&gt;
      &lt;h3&gt;Andrea Bertoli ・ Oct 18 '19&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#functional&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#intermediate&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;
&lt;br&gt;
This list of libraries and other resources is also worth a look:&lt;br&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/stoeffel" rel="noopener noreferrer"&gt;
        stoeffel
      &lt;/a&gt; / &lt;a href="https://github.com/stoeffel/awesome-fp-js" rel="noopener noreferrer"&gt;
        awesome-fp-js
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      😎 A curated list of awesome functional programming stuff in js
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Leason 1 - Ramda function naming can be confusing
&lt;/h2&gt;

&lt;p&gt;Ramda has a lot of functions, so if you need to do something, there's probably a function for it.&lt;/p&gt;

&lt;p&gt;However, I found it difficult to find what I was looking for because some of the namings seemed a bit non-obvious. For example, I found &lt;a href="https://ramdajs.com/docs/#evolve" rel="noopener noreferrer"&gt;evolve&lt;/a&gt; useful in multiple cases for applying different transforms to each property in an object, but I only found it by chance.&lt;/p&gt;

&lt;p&gt;Make sure to &lt;a href="https://ramdajs.com/docs" rel="noopener noreferrer"&gt;explore the docs&lt;/a&gt;, you'll find some interesting tools!&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson 2 - Keep it simple
&lt;/h2&gt;

&lt;p&gt;You've had a look through the docs and found a few interesting methods. All of these methods are great for more complicated logic, but they can also make a relatively simple piece of logic much more difficult to read.&lt;/p&gt;

&lt;p&gt;Let's take an example - say you want to take the first element from an array, and return the element if it is greater than 0, else return undefined. With Ramda, that could look something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;R.ifElse(
  R.pipe(R.nth(0), R.gt(R.__, 0)),
  R.identity,
  R.always(undefined)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the other hand, an equivalent lambda function could look something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;([elem]) =&amp;gt; elem &amp;gt; 0 ? elem : undefined;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While all the methods that Ramda offers can be useful, it's pretty easy to end up staring at some code you've just written, trying to remember what on earth you were even trying to do.&lt;/p&gt;

&lt;p&gt;Ramda is there to help write code in a more functional way, not to be the only way to write code that is functional.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson 3 - Need to pass variables through to a middle pipe stage? Use a lambda!
&lt;/h2&gt;

&lt;p&gt;Once you start writing more complex pipelines, you'll eventually reach a point where you have a stage that needs access to a parameter that's passed in at the start of the pipeline.&lt;/p&gt;

&lt;p&gt;You have a couple of options. The first is to change every other stage before the one that needs the parameter to accept and pass through that parameter. This is messy and quickly becomes complicated.&lt;/p&gt;

&lt;p&gt;The alternative, as I found, is just to wrap the pipeline in a lambda:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(arg1, arg2) =&amp;gt; R.pipe(
  stage1,
  stage2,
  stage3(arg2),
  stage4,
)(arg1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, none of the previous stages are affected and the code is easier to read.&lt;/p&gt;

&lt;p&gt;These are as much for my reference as anything else, but I hope these lessons are useful to anyone else getting started with Ramda. If there's anything you found useful when getting started with Ramda, or if you have questions/thoughts about the above, please share in the comments!&lt;/p&gt;

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