Learn Ramda.js The Interactive Way
Written by Nikos Vaggalis   
Tuesday, 17 December 2019

Ramda, a library which makes functional programming in JavaScript easy, gets its own playground. We take a look around and come up with a recommendation.

But why RamdaJS?

Can't I do FP in vanilla Javascript?

Sure you can, but the difference is that RamdaJS is a library specifically designed for the functional style of programming:

RamdaJS emphasizes a purer functional style. Immutability and side-effect free functions are at the heart of its design philosophy. This can help you get the job done with simple, elegant code.

Ramda functions are automatically curried. This allows you to easily build up new functions from old ones simply by not supplying the final parameters.

The parameters to Ramda functions are arranged to make it convenient for currying. The data to be operated on is generally supplied last.

The problem is that Ramda has nearly 200 functions so it can be difficult to know which to use and when. For that reason there's a cheatsheet that "lists the most common actions you may wish to perform and the functions that are most likely to be suited to the task".

It acts like an index. For example, when I'm looking to "change every value", I find the entry which points to the related function - in this case it's "map".

Very helpful, but there is room  for improvement. and this is provided by the "Learn Ramda, the interactive way" online playground which takes help to another level. Instead of looking up in a static list, this is a UI with which you can find the appropriate method interactively!

It starts with a simple "I have a" dropdown list of scenarios,which matches against a "would like to" dropdown with actions. Making your choice forms a sentence like "I have a list and I would like to change every value", an action that reveals the related Rambda documentation and code samples.

For the example above, and the map function:

R.map
http://ramdajs.com/docs#map

Functor f => (a -> b) -> f a -> f b

  • Takes a function and a functor, applies the function to each of the functor's values, and returns a functor of the same shape.

Ramda provides suitable map implementations for Array and Object, so this function may be applied to [1, 2, 3] or
{x: 1, y: 2, z: 3}.

  • Dispatches to the map method of the second argument, if present.
  • Acts as a transducer if a transformer is given in list position.
  • Also treats functions as functors and will compose them together.


const double = x => x * 2;
R.map(double, [1, 2, 3]); //=> [2, 4, 6]
R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}

 

Another case could be "I have a list and I would like to select values from the start",and so on.

The scenarios dropdown box also contains object, function, logic, relation and math, so one might read " I have a function and I would like to partially apply a function".

A nice side effect of the playground is that, together with learning how Ramda goes about functional programming, you also discover the essence of the functional concepts themselves by examining the scenarios and actions that can be performed. For example, I observe the possible actions I can perform under the this paradigm:

  • I can combine promise returning functions
  • apply a list of functions to each argument and collect the results
  • partially apply a function
  • curry a function
  • know if a list value satisfies two predicates
  • select list values based on custom logic
  • know if an object's specific key's value satisfies a custom predicate

If you want to really learn about Ramda I would recommend starting with the great (and free)  online class Functional Programming Patterns With RamdaJS from Educative and use this interactive cheatsheet in the course of the class.

 

More Information

Learn Ramda.js, the interactive way

 

Related Articles

Functional Programming Patterns With RamdaJS

Functional JavaScript With Ramda

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

Banner


CSS Ecosystem In the Spotlight
06/11/2024

The 2024 edition of the State of CSS has been posted, revealing that the latest features of the language not only do away with extra tooling, but even start taking on tasks that previously requir [ ... ]



Random Gifts For Programmers
24/11/2024

Not really random. Not even pseudo random, more stuff that caught my attention and that I, for one, would like to be given. And, yes, if I'm not given them, I'd probably buy some for myself.


More News

 

espbook

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Tuesday, 17 December 2019 )