The JavaScript RegEx API

Regular Expressions are about as elegant as a pig on a bicycle. Using a regular expression feels like resorting to machine code when all those patterns we’re taught to love just aren’t up to the job. Which, I suppose, is also a reason to like them. They have a brute force directness, free from pattern politics and endless analysis.

And they work. Eventually.

If the JavaScript Regular Expressions API makes your head spin then this might be for you. I’ll document the basics and demonstrate how you might use them to full effect.

For the sake of brevity (not to mention my own lack of regex proficiency) I won’t discuss the syntax of the expressions themselves. Suffice to say, JavaScript regEx syntax is Perl based. There are many excellent online resources for this, as well as some nice online RegEx testers.
Continue reading

JavaScript Partials

In a previous post I introduced the curry function. To recap, currying creates a new function with the first n arguments pre-assigned:-

var subtract = function(a,b) {
    return a - b;
}

var subtractFrom8 = subtract.curry(8);

subtractFrom8(2); //6

Currying is an expressive and compact alternative to manually wrapping anonymous functions. I use it a lot. But sometimes its not enough – the problem is you can only pre-assign the first n arguments. What if we wanted to make a function and pre-assign the rightmost argument, or maybe the middle two? Enter partial:-

var subtract5 = subtract.partial(___,5);

subtract5(13); //8;

Continue reading

express yourself: rapid function definition

The native JavaScript function indexOf, applied to a String, returns the index of the first occurrence of a specified value. Useful as it is, I often find myself wishing for a variant method that will return the index after the occurrence so that I can pass the result to a slice call and return everything after a given character.

With this in mind, I created a method called express (as in expression, and alsoquick) which returns a new function by applying the expression argument to the result of the old function. Its partly inspired by the Haskell language and by Oliver Steele’s lambda function.

String.prototype.indexAfter = String.prototype.indexOf.express('r + 1');

Continue reading

JavaScript and valueOf

In JavaScript, valueOf and toString are sister methods inherited by every object. One of these methods will get invoked whenever an expression encounters a complex object where a primitive value was expected. For example :-

alert(myHamster);
var result = 2 + myHamster;

In broad terms, if the expression hints at the need for a string then toString is invoked,  otherwise its valueOf . If either method returns a non-primitive, the other method gets a try. The above examples expected myHamster to be a string and a number respectively so they are evaluated as:-

alert(myHamster.toString()); //interpreter expected a string
var result = 2 + myHamster.valueOf(); //expected a number

Continue reading

JavaScript and Russian Dolls

In JavaScript, functions are variables which means they can be created and replaced at run time. Thanks to the pioneering efforts of Richard Cornford (Russian Doll Pattern, 2004), Peter Michaux (Lazy Function Definition pattern, 2007) Oliver Steele (One-Line Memoization, 2006) there are nifty techniques that exploit this capability.

First, a very simple example to illustrate the principle:-

var pushTheRedButton = function() {
    //reassign a new function to the variable pushTheRedButton
    pushTheRedButton = function() {
        //this line gets called on all subsequent visits</span>
        alert("Now look what you've done!");
    }
    //this line only gets called on the first visit</span>
    alert("Don't ever push this button again!");
}

pushTheRedButton(); //"Don't ever push this button again!"
pushTheRedButton(); //"Now look what you've done!"

Continue reading