Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate C# Lambda methods in Javascript

I would like to simulate the C# Any() method, which can be used to determine whether if a collection has any matching objects based on a lambda expression.

I used jQuery's $.grep to make things easier:

Array.prototype.any = function (expr) {

    if (typeof jQuery === 'undefined')
        throw new ReferenceError('jQuery not loaded');

    return $.grep(this, function (x, i) {
        return eval(expr);
    }).length > 0;

};

var foo = [{ a: 1, b: 2 }, { a:1, b: 3 }];

console.log(foo.any('x.a === 1')); //true
console.log(foo.any('x.a === 2')); //false

I know that eval() is bad practice for obvious reasons. But is it ok in this case, since I won't use this with anything related to some user inputs?

Can this be done without eval()? I can't figure out a way to pass an expression to the function without evaluating it.

http://jsfiddle.net/dgGvN/

like image 968
Johan Avatar asked May 14 '13 13:05

Johan


People also ask

How do I simulate C code?

How to simulate C code. By default no C code will be simulated until the C Simulation button is enabled. C Code icons will simply be skipped by the simulator. The C Simulation button can be found on the main toolbar and via the DEBUG menu. C Simulation is enabled when the button icon is highlighted by a bounding square.

How do I use the C Simulation button?

C Code icons will simply be skipped by the simulator. The C Simulation button can be found on the main toolbar and via the DEBUG menu. C Simulation is enabled when the button icon is highlighted by a bounding square. When simulating C code, a helpful Conversion Messages window will appear.

How to use calculator program in C?

This calculator program in C helps the user to enter the Operator (+, -, *, or /) and two values. Using those two values and operand, it will perform Arithmetic Operations. For this C calculator program example, we used the Switch case to check which operand is inserted by the user.

What is C online compiler?

The user friendly C online compiler that allows you to Write C code and run it online. The C text editor also supports taking input from the user and standard libraries. It uses the GCC C compiler to compile code.


2 Answers

I suggest you take a good look at JS closures. In particular, what you did there can be done natively in JS by using the Array.some method:

[{ a: 1, b: 2 }, { a:1, b: 3 }].some(function(x) { return x.a === 1; }); // true
[{ a: 1, b: 2 }, { a:1, b: 3 }].some(function(x) { return x.a === 2; }); // false

edit: in this case we're not really using closures but rather plain simple anonymous functions...

like image 193
CAFxX Avatar answered Oct 08 '22 09:10

CAFxX


Pass in a function:

Array.prototype.any = function (expr) {

    if (typeof jQuery === 'undefined')
        throw new ReferenceError('jQuery not loaded');

    return $.grep(this, expr).length > 0;

};

var foo = [{ a: 1, b: 2 }, { a:1, b: 3 }];

console.log(foo.any(function(x, i){return x.a === 1})); //true
console.log(foo.any(function(x, i){return x.a === 2})); //false
like image 25
Naftali Avatar answered Oct 08 '22 10:10

Naftali