I try to use in my app, simple comparator to filter some data with passing string filter instead function as eg. passed to [].filter
Comparator should return function which will be a filter.
var comparator = function( a, b, c ) {
switch( b ){
case '>=': return function() { return this[a] >= c;}; break;
case '<=': return function() { return this[a] <= c;}; break;
case '<': return function() { return this[a] < c;}; break;
case '>': return function() { return this[a] > c;}; break;
case '=': return function() { return this[a] == c;}; break;
case '==': return function() { return this[a] === c;}; break;
case '!=': return function() { return this[a] != c;}; break;
default: return null;
};
}
Assume that i get this function by:
var filterFn = comparator.apply({}, /(.+)(=|>=|<=|<|>|!=|==|!==)(.+)/.exec( "id<4" ).slice(1) );
someModel = someModel.objects.filter( filterFn );
The target it will look:
someModel.get = function( filter ){
return new Model(
this.objects.filter(
comparator.apply({}, /(.+)(=|>=|<=|<|>|!=|==|!==)(.+)/.exec( "id<4" ).slice(1)
)
);
};
var filtered = someModel.get( "id<4" );
Question is - I assume that it will be a lot more operators and I have no idea how to write it more simply.
Using Eval is out of question.
This code didn't was both executed and tested I wrote it just to show what I mean.
Store every function in an object, either pre-defined, or dynamically.
If you want to dyanmically create the set of functions, define the comparator object as shown below. I assumed that you did not extend the Object.prototype. If you did, operators.hasOwnProperty(property) has to be used within the first loop.
// Run only once
var funcs = {}; // Optionally, remove `funcs` and swap `funcs` with `operators`
var operators = { // at the first loop.
'>=': '>=',
'<=': '<=',
'<' : '<',
'>' : '>',
'=' : '==', //!!
'==':'===', //!!
'!=': '!='
}; // Operators
// Function constructor used only once, for construction
for (var operator in operators) {
funcs[operator] = Function('a', 'c',
'return function() {return this[a] ' + operator + ' c};');
}
// Run later
var comparator = function(a, b, c) {
return typeof funcs[b] === 'function' ? funcs[b](a, c) : null;
};
When comparator is invoked, the returned function looks like:
function() { return this[a] < c; }// Where a, c are pre-determined.
This method can be implemented in this way (demo at JSFiddle):
// Assumed that funcs has been defined
function implementComparator(set, key, operator, value) {
var comparator, newset = [], i;
if (typeof funcs[operator] === 'function') {
comparator = funcs[operator](key, value);
} else { //If the function does not exist...
throw TypeError("Unrecognised operator");
}
// Walk through the whole set
for (i = 0; i < set.length; i++) {
// Invoke the comparator, setting `this` to `set[i]`. If true, push item
if (comparator.call(set[i])) {
newset.push(set[i]);
}
}
return newset;
}
var set = [ {meow: 5}, {meow: 3}, {meow: 4}, {meow: 0}, {meow: 9}]
implementComparator( set , 'meow', '<=', 5);
// equals: [ {meow: 5}, {meow: 3}, {meow: 4}, {meow: 0} ]
For clarification, I constructed this answer, while keeping the following in mind:
The OP requests an simple, easily extensible method with an unknown/dynamic set of operators.
The code is based on the pseudo-code at the OP, without changing anything which could affect the intent of the OP. With some adjustments, this function can also be used for Array.prototype.filter or Array.prototype.sort.
eval (or Function) should not be used at every call to comparator
Don't do it so dynamically....it would be far more efficient to only create the functions once, rather than each time they are called, as that would create a new funciton and so a memory leak each time the compare is done.
var comparator = {
">=": function(a, b) { return a >= b;},
"<=": function(a, b) { return a <= b;},
"add": function(a, b) { return a + b; },
compare: function(typeString, a, b){
if(comparator.hasOwnProperty(typeString) == true){
var theFunction = comparator[typeString];
return theFunction(a, b);
}
else{
alert("typeString '" + typeString + "' not supported.");
}
}, };
var test = comparator.compare(">=", 5, 4);
var comparator = (function () {
var fns = {
'>=': function (a, c) { return a >= c; },
'<=': function (a, c) { return a <= c; },
'<': function (a, c) { return a < c; },
'>': function (a, c) { return a > c; },
'=': function (a, c) { return a == c; },
'==': function (a, c) { return a === c; },
'!=': function (a, c) { return a != c; }
};
return function (b) {
return fns.hasOwnProperty(b) ? fns[b] : null;
};
}());
At this point you can see that nothing is more efficient than an inline expression. It is not clear to me why you think you need to be so dynamic beforehand.
Related
I am trying to evaluate a prefix expression. I got the code from link but for some reason I run into a maximum call size exceeded and not able to debug. Can someone help me please?
var operators = {
'+': function(a, b) {
return a + b;
},
'-': function(a, b) {
return a - b;
},
'*': function(a, b) {
return a * b;
},
'/': function(a, b) {
return a / b;
},
};
var precedence = [
['*', '/'],
['+', '-']
]
function evalPrefix(input, variable) {
// process at this level
// return the result of this level and what we didn't use
// return a null value if we fail at any point
function step(current) {
// directly return numbers
if (!isNaN(parseFloat(current[0]))) {
return {
value: parseFloat(current[0]),
rest: current.slice(1)
};
}
// otherwise, we're going to have to recur
else {
var f = operators[current[0]];
// recur for left, use that rest for right
var left = step(current.slice(1));
if (left.value == null) return {
value: null,
rest: []
};
var right = step(left.rest);
if (right.value == null) return {
value: null,
rest: []
};
// return at my level
return {
value: f(left.value, right.value),
rest: right.rest
};
}
}
return step(input).value;
}
console.log(evalPrefix('+ x y', {x: 1, y: 5})); //6
console.log(evalPrefix('+ 1 5')); //6
Any help to get this work is highly appreciated. Please advice.
I found this code in a book, how do you write or define the code for mybind
var concat = function(a, b) { return a + " " + b;}
var good = mybind(concat, "good");
good("night") == "good night"
To create a new function, you can either create it yourself:
function mybind(f, a) {
return function (b) {
return f(a, b);
}
}
var concat = function(a, b) { return a + " " + b;}
var good = mybind(concat, "good");
console.log(good("night"));
or for your scenario you can use function.bind to create one for you
function mybind(f, a) {
return f.bind(null, a);
}
var concat = function(a, b) { return a + " " + b;}
var good = mybind(concat, "good");
console.log(good("night"));
Like this:
var concat = function(a, b) { return a + " " + b;}
var mybind = function (fn, arg1) {
return function (arg2) {
return fn(arg1, arg2);
};
}
var good = mybind(concat, "good");
console.log(good("night") === "good night")
The following will make your comparison return true. myBind should create a new function bound to b. that's what bind does.
var mybind = function( fn, b ) { return fn.bind(this, b); };
var add = function(a, b) {
return a + b;
}
var addOne =add.bind(null,1);
var result = addOne(4);
console.log(result);
Here the binded value of a is 1 and b is 4.
How to assign the binding value i.e)1 to the second argument of the function without using spread operator(...)
You could take a swap function with binding the final function.
var add = function (a, b) { console.log(a, b); return a + b; },
swap = function (a, b) { return this(b, a); },
addOne = swap.bind(add, 1),
result = addOne(4);
console.log(result);
With decorator, as georg suggested.
var add = function (a, b) { console.log(a, b); return a + b; },
swap = function (f) { return function (b, a) { return f.call(this, a, b) }; },
addOne = swap(add).bind(null, 1),
result = addOne(4);
console.log(result);
You could use the arguments object for reordering the parameters.
var add = function (a, b, c, d, e) {
console.log(a, b, c, d, e);
return a + b + c + d + e;
},
swap = function (f) {
return function () {
var arg = Array.apply(null, arguments);
return f.apply(this, [arg.pop()].concat(arg));
};
},
four = swap(add).bind(null, 2, 3, 4, 5),
result = four(1);
console.log(result);
You can use the following way
var add = function(x){
return function(y){
return x+y;
}
}
add(2)(3); // gives 5
var add5 = add(5);
add5(10); // gives 15
here add5() would set x = 5 for the function
This will help you what you need
var add = function(a) {
return function(b) {
return a + b;
};
}
var addOne = add(1);
var result = addOne(4);
console.log(result);
You can try this
function add (n) {
var func = function (x) {
if(typeof x==="undefined"){
x=0;
}
return add (n + x);
};
func.valueOf = func.toString = function () {
return n;
};
return func;
}
console.log(+add(1)(2));
console.log(+add(1)(2)(3));
console.log(+add(1)(2)(5)(8));
If I have an array like this:
var tab = ['1185 Design','3 D Exhibits','44Doors', '4Concepts','ABC Data','acceleration'];
And I want to sort it so that small letter 'a' element comes before capital letter 'A' element.
Use Array#sort() method with String#localeCompare()
var tab = ['1185 Design', '3 D Exhibits', 'nb', 'N', 'cd', '44Doors', '4Concepts', 'ABC Data', 'acceleration'];
tab.sort(function(a, b) {
return sortFn(a, b);
});
function sortFn(a, b) {
// if both are equal return 0
if (a == b) return 0;
// if first characters are equal call the same function with remaining (recursion)
if (a.charAt(0) == b.charAt(0)) return sortFn(a.slice(1), b.slice(1))
// check lowercase or uppercase based on that return value
if (/^[a-z]/.test(a.charAt(0)) && /^[A-Z]/.test(b.charAt(0))) return -1;
if (/^[a-z]/.test(b.charAt(0)) && /^[A-Z]/.test(a.charAt(0))) return 1;
// otherwise ude normal compare function
return a.localeCompare(b);
}
console.log(tab);
UPDATE : In case if you want to sort with alphabetical order and small letter should have higher precedence only if they are equal then do something like.
var tab = ['1185 Design', '3 D Exhibits', 'nb', 'N', 'cd', '44Doors', '4Concepts', 'ABC Data', 'acceleration'];
tab.sort(function(a, b) {
return sortFn(a, b);
});
function sortFn(a, b) {
// if both are equal return 0
if (a == b) return 0;
// if first characters are equal call the same function with remaining (recursion)
if (a.charAt(0) == b.charAt(0)) return sortFn(a.slice(1), b.slice(1))
// check lowercase or uppercasebased on that return value in case the letters are equal
if (a.charAt(0).toLowerCase() == b.charAt(0).toLowerCase()) {
if (/^[a-z]/.test(a.charAt(0)) && /^[A-Z]/.test(b.charAt(0))) return -1;
if (/^[a-z]/.test(b.charAt(0)) && /^[A-Z]/.test(a.charAt(0))) return 1;
}
// otherwise ude normal compare function
return a.localeCompare(b);
}
console.log(tab);
You could use sorting with map with a reversion of case.
// the array to be sorted
var list = ['1185 Design', '3 D Exhibits', '44Doors', '4Concepts', 'ABC Data', 'acceleration'];
// temporary array holds objects with position and sort-value
var mapped = list.map(function (el, i) {
return { index: i, value: el.split('').map(function (a) {
var b = a.toUpperCase();
return a === b ? a.toLowerCase(): b;
}).join('')};
});
// sorting the mapped array containing the reduced values
mapped.sort(function (a, b) {
return +(a.value > b.value) || +(a.value === b.value) - 1;
});
// container for the resulting order
var result = mapped.map(function (el) {
return list[el.index];
});
console.log(result);
I am beginner in Javascript
I am always getting true returned whatever the values of my variables,
It should return true if a and b are both even, but false otherwise.
Thanks for your help.
https://repl.it/9nH/1675
var a = 4;
var b= 5;
function areBothEqual (a, b) {
if(a===b) {
return true;
}else {
return false
}
}
var result = areBothEqual();
document.write(result)
you are not passing the arguments to your function:
areBothEqual(a,b)
you had:
areBothEqual()
cheers
The issue is that you are requesting the arguments a and b in the line function areBothEqual (a, b), but they are never actually passed. Just replace the (a, b) with empty brackets (), as you can use the previously defined a and b instead.
var a = 4;
var b= 5;
function areBothEqual() {
if(a===b) {
return true;
} else {
return false
}
}
areBothEqual()
var a = 4;
var b= 5;
function areBothEqual (a, b) {
console.log(a);
console.log(b);
if(a===b) {
return true;
}else {
return false
}
}
areBothEqual();
They are 'undefined' both are equals...