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


Go At Highest Rank Ever in TIOBE Index
20/11/2024

Go is currently in 7th place in the TIOBE Index for November 2024. Not only is this is the highest position it has ever had, it's percentage rating is almost equal to its all-time-high. Will Go contin [ ... ]



Personal Picks For Holiday Gifts
29/11/2024

It's Black Friday, the traditional day to indulge in online shopping. Not every item that is included in my selection of gifts is subject to a promotional offer, but where they aren't you might be poi [ ... ]


More News

 

espbook

 

Comments




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

Last Updated ( Monday, 02 June 2014 )