Click here to Skip to main content
Click here to Skip to main content
Go to top

JavaScript Lambda Expressions

, 2 Jun 2014
Rate this:
Please Sign up or sign in to vote.
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.

(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.

 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)

Share

About the Author

Sameera Millavithanachchi
Software Developer (Senior)
Sri Lanka Sri Lanka
No Biography provided

Comments and Discussions

 
Suggestionevaluators refactored. Pinmembersobo1239-Jun-14 6:52 
GeneralRe: evaluators refactored. PinmemberSameera Millavithanachchi10-Jun-14 7:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 3 Jun 2014
Article Copyright 2014 by Sameera Millavithanachchi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid