Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

JavaScript Lambda Expressions

5.00/5 (4 votes)
2 Jun 2014CPOL 24.3K   32  
This is a jQuery plugin which can be used to evaluate lambda expressions

Introduction

This is a jquery plugin to evaluate lambda expressions. After the plugin code, I've given an example to illustrate how we can use this plugin. As you know, JavaScript doesn't support operator overloading. We have to provide lambda expression in string format instead.

Using the Code

Plugin code is given below. Only limited supporting expressions (min, max first, last) are given in the below code and if you want the full version of this plugin, you can download and can use orderby, where expressions also.

JavaScript
(function ($, window, undefined) {
    var helpers = {
        getEvaluator: function (options) {
            var evaluator = options.exp.match(/\(?(.*)\)?\s*=>\s*(.*)/),
                p = [], b = "";
 
            if (evaluator.length < 3)
                throw new Error('Invalid lambda expression');
            if (evaluator.length > 0) evaluator.shift();
            if (evaluator.length > 0) b = evaluator.pop();
            if (evaluator.length > 0) p = evaluator.pop()
             .replace(/^\s*|\s(?=\s)|\s*$|,/g, '').split(' ');
 
            return {
                evaluator: evaluator,
                p: p,
                b: b
            };
        }
    };
 
    var evaluators = {
        first: function () {
            var obj = $(this);
 
            try {
                return obj[0];
            }
            catch (e) { throw new Error(e.message); }
        },
        last: function () {
            var obj = $(this);
            try {
                return obj[obj.length - 1];
            }
            catch (e) { throw new Error(e.message); }
        },
        min: function (options) {
            var object = $(this),
                result = [],
                paramEval = helpers.getEvaluator(options),
                evaluator = paramEval.evaluator,
                p = paramEval.p,
                b = paramEval.b;
 
            evaluator = ((! /\s*return\s+/.test(b)) ? "return " : "") + b;
            p.push(evaluator);
 
            try {
                var executor = Function.apply({}, p);
                object.each(function (index, value) {
                    if (result.length == 0) result.push(value);
                    else if (executor(value) < executor(result[0])) {
                        result.length = 0; result.push(value);
                    } else if (executor(value) === executor(result[0]))
                        result.push(value);
                });
                return result;
            }
            catch (e) { throw new Error(e.message); }
        },
        max: function (options) {
            var object = $(this),
                result = [],
                paramEval = helpers.getEvaluator(options),
                evaluator = paramEval.evaluator,
                p = paramEval.p,
                b = paramEval.b;
 
            evaluator = ((! /\s*return\s+/.test(b)) ? "return " : "") + b;
            p.push(evaluator);
            try {
 
                var executor = Function.apply({}, p);
                object.each(function (index, value) {
                    if (result.length == 0) result.push(value);
                    else if (executor(value) > executor(result[0])) {
                        result.length = 0; result.push(value);
                    } else if (executor(value) === executor(result[0]))
                        result.push(value);
                });
                return result;
            }
            catch (e) { throw new Error(e.message); }
        }
    };
 
    $.fn.lambda = function (options) {
        if (Object.keys(options).length != 2)
            throw new Error('Invalid information');
        if (evaluators[options.method]) {
            return evaluators[options.method].apply(this, arguments);
        }
        throw new Error('Method "' + options.method + '" does not exist on lambda');
    };
})(jQuery, window, undefined);

Here is a simple example how you can use the above plugin library to evaluate lambda expressions.

JavaScript
var myArray = [];
   myArray.push({ data: { value: 10 } });
   myArray.push({ data: { value: 3 } });
   myArray.push({ data: { value: 1400 } });
   myArray.push({ data: { value: 2 } });
   myArray.push({ data: { value: 24 } });
   myArray.push({ data: { value: 34 } });
   myArray.push({ data: { value: 50 } });
   myArray.push({ data: { value: 1 } });
   myArray.push({ data: { value: 10 } });
   var obj = $(myArray).lambda({ method: 'min', exp: " p  => p.data.value" });

Points of Interest

Here, I've only given commonly used lambda expressions such as min, max, where, etc. If you want, you can extend this to support any lambda expression.

History

Updates are pending...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)