06 Aug 2013, 15:03

Data Models, functional composition and validation

While I am working my way through the Functional Javascript book I came across something that could be really useful, especially when dealing with AJAX requests.

Functional Composition

In a nutshell you start with simple functions that do a single job and then chain them up to perform some larger task. In this example we want to validate that the json being returned from an AJAX call contains the properties we require. To do that we start with the following function:

function validator(message, fun){
    var f = function(){
        return fun.apply(fun, arguments);
    f['message'] = message;
    return f;

which takes a message string and a function as parameters and returns an object what can be called with an arbitrary number of arguments to perform a task. The returned object has a ‘message’ property assigned also.

Next we need a ‘driver’ function:

function checker(){  
    var validators = _.toArray(arguments);  
    return function(obj){  
        return _.reduce(validators, function(errs, check){  
                return errs;  
                return _.chain(errs).push(check.message).value();  
        }, []);  

that should be called with a list of validators as its arguments. It loads these into an array and returns a function which uses this array to validate the object ‘obj’. It will loop over each of the validators in the array and checks to see if the obj object passes each of them. If the check fails then it appends the validators ‘message’ to the list it will return. If all validators pass then the array returned will be empty.

Putting it all together

I can use my checker and validator functions to create a validation function like the following:

function isValidResourceItem(){
    var validators = checker(

        validator('obj can not be nul',
            function(obj){return !_.isNull(obj);}),

        validator('obj must contain title',
            function(obj){return !_.isNull(obj.title);}),

        validator('obj must contain canonicalVolumeLink',
            function(obj){return !_.isNull(obj.canonicalVolumeLink);}),

        validator('obj must contain description',
            function(obj){return !_.isNull(obj.description);})
    return validators;

which will help to ensure the data was returned in a format I expect and cut down on those ‘undefined’ errors you would get without something like this.


I reused an example from an earlier jsFiddle to test the idea out, I want to be sure the search results json object contains the properties I need to produce a results list.

Whats the point?

Well I guess it shows an easy way to take pieces of functionality, place them into small reusable functions and build upon them into a pretty powerful set of tools.

21 Jul 2013, 15:03

Functional javascript using underscore

Functional javascript using underscore


A little while ago I picked up Functional Javascript by Michael Fogus. I’m only part way through it and to be honest I should probably have waited until I finished it before starting this post but…

There is a central theme to this book which intrigued me, indeed its one of the main tenets of functional programming. It can be summed up in this quote: >“It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures.” - Alan Perlis

This is very different concept to what is normally seen in large enterprise level projects and applications. I have to say that I have spent a large amount of time writing code to map one model to another and back again. Its not fun. This functional stuff might be on to something.


Well Michaels book uses the underscore library for all of his examples and its a really powerful tool. I have started using it here and there on smaller projects and the more I use it the more I like it.


Project Euler is probably the go-to set of problems when developers try to learn a new language or framework. I don’t see why I should be any different. I have started a small project to play around with this and you can see it over on github.

Problem 1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

Can be solved with a few lines of code like the following: return _.reduce(_.range(0,1000), function(memo, number){ if( number%3===0 || number%5==0) return memo+number; return memo;} );


Unit testing javascript has come a long way in the last few years and I have added a simple QUnit test runner to the repository.