Functional JavaScript With Ramda
Written by Ian Elliot   
Monday, 02 June 2014

Ramda is a library of functions designed to make functional programming in JavaScript easy and powerful without making it any less like JavaScript.

 

ramdaicon

 

Ramda - the name is a joke based on lamb-da as in lambda calculus and lamb as in a young sheep and Ram as in a male sheep. It aims to add functional programming to JavaScript without changing its nature and feel. This "work with the language" approach is something that you will appreciate if you actually like JavaScript. There are lots of aspects of JavaScript that make it look a lot like a functional language, but it lacks a few important features - automatic currying being the most important.

In JavaScript you can supply a function with a variable number of arguments. If you supply too few then usually the function evaluation doesn't work. If you supply too many the extras are just ignored. To curry a function means to supply too few arguments to get a new function with just the unspecified parameters. 

For example, in Ramda the prop(p,obj) method returns the specified property p of the object obj. For example:

var value=R.prop('myProperty',obj);

returns the value of obj.myProperty as both parameters are specified. However, if you write:

var getprop=R.prop('myProperty');

then prop is curried to create a new function which returns the value of myProperty on the specified object, e.g.

var value=getprop(obj);

returns obj.myProperty. 

Currying is something you come to rely on in functional programming as it provides a way to build new functions using composition. For example add(a,b) adds the two numbers together and mult(a,b) multiplies them. So you can use composition and autocurrying to build up a new function:

var plusonetimestwo=R.compose(R.mult(2),R.add(1));

Composition applies the functions one after the other, with autocurrying if needed. The first function add is curried to create an add one function then the mult function is curried to create a multiply by 2 function and this is applied to the add one function. That is:

R.mult(2,R.add(1,arg));

where arg is the single remaining parameter. 

So in this case if you write

var value=plusonetimetwo(3); 

you get 

R.mult(2,R.add(1,3));

Autocurrying in Ramda gives you more of the flavour of functional programming than if you have to implement currying manually.  

There are already functional style libraries for JavaScript - Underscore and Lodash are the best known - but Ramda claims that its way of doing things provides a better and more efficient form of functional programming. In particular it takes a function first approach in designing its methods. This means that most methods take any functions needed as their first parameter, which makes composition much easier. 

You can use Ramda with Node.js or in browser and it is open source. It looks like a well thought out and implemented library, but currently it has one problem - difficult documentation. You can read through the generated documentation next to the source code and find everything you need to know, but this requires a level of commitment that usually only comes after you know what is available. A simple index of functions grouped by type with a small example of each would make the whole project much more accessible.

If you know you want to write functional style JavaScript without losing any of its existing features, then it is worth checking out Ramda.

 ramdaicon

Banner


Copilot Improves Code Quality
27/11/2024

Findings from GitHub show that code authored with Copilot has increased functionality and improved readability, is of better quality, and receives higher approval rates than code authored without it.

 [ ... ]



Google Releases Gemini 2 And Jules Code Agent
18/12/2024

Google has announced an updated version of Gemini, saying that Gemini 2.0 Flash Experimental will "enable even more immersive and interactive applications", along with new coding agents that can take  [ ... ]


More News

 

espbook

 

Comments




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

Last Updated ( Monday, 02 June 2014 )