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:
returns the value of obj.myProperty as both parameters are specified. However, if you write:
then prop is curried to create a new function which returns the value of myProperty on the specified object, e.g.
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:
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
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.
More InformationRelated ArticlesIntercooler.js Declarative Ajax Adobe Snap.svg JavaScript Library Vanilla JS Used On More Sites Than jQuery
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
Comments
or email your comment to: comments@i-programmer.info |
Last Updated ( Monday, 02 June 2014 ) |