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.




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:


where arg is the single remaining parameter. 

So in this case if you write

var value=plusonetimetwo(3); 

you get 


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.



pgxman - PostgreSQL Extension Manager

pgxman is a package manager like npm, but instead of Javascript packages, it is for PostgreSQL extensions. It detects and streamlines extension operations and looks after dependency manageme [ ... ]

Gain A Python Professional Certificate From edX

From now until the end of February edX is offering a saving of up to 30% on some of its expert-led courses and program bundles, which is a good incentive for going from thinking about enrolling to act [ ... ]

More News


raspberry pi books



or email your comment to:

Last Updated ( Monday, 02 June 2014 )