It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Importance of Curry

Importance of Curry

Postby bardonw on Mon Apr 11, 2011 2:43 pm

In JavaScript, the curry function is not native, but if you use a library such as jPaq, you can use it. At first glance, this functional method may seem insignificant, but with a little bit of thought, you may come up with many different reasons to use it. For instance, you may know that newer browsers add support for the string trimming functions. Still, in order to provide this functionality in every browser, you will need some extra code. If you have a library that provides the curry function, you can use the following four lines to define trim, trimLeft, and trimRight:
Code: Select all
var p = String.prototype, r = p.replace;
p.trim = p.trim || r.curry(/^[\s\u00A0]+|[\s\u00A0]+$/g, "");
p.trimLeft = p.trimLeft || r.curry(/^[\s\u00A0]+/, "");
p.trimRight = p.trimRight || r.curry(/[\s\u00A0]+$/, "");


How cool is that!?!?!? These four lines will only define the trim functions if they are not defined. If you want proof that this approach really works, execute the jPaq example which is found here. On that page, you will also find other one-liners.

Even though JavaScript is not a functional programming language, the fact that you can turn it into one is awesome!
bardonw
 
Posts: 7
Joined: Tue Feb 15, 2011 4:16 pm

Who is online

Users browsing this forum: No registered users and 2 guests