Functional JavaScript With Ramda
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


Keep It SImple - Users Are Stupid
30/11/2016

Over two thirds of the adult population in developed countries have poor or non-existent computer skills. By and large this is the audience you are developing apps and interfaces for - and it is impor [ ... ]



The Weekly Top 10: More PHP Web Development Resources
01/12/2016

The focus returns to PHP for this week's round up of posts about web development selected from other people's blogs. There are a number of how tos and you can find out why Slack chose PHP,   [ ... ]


More News

 

 
 
 

blog comments powered by Disqus

Last Updated ( Monday, 02 June 2014 )
 
 

   
RSS feed of news items only
I Programmer News
Copyright © 2016 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.