-
Notifications
You must be signed in to change notification settings - Fork 35
Lambda Expression
Jing Lu edited this page May 10, 2013
·
8 revisions
A function with a default return is lambda expression in ReoScript.
var plus = (a, b) => a + b;
It is same as
function plus(a, b) {
return a + b;
}
Using Lambda
var result = plus(1, 2);
result is
3
Here is a function named 'where' which find matched element over array by given predicate lambda expression.
Array.prototype.where = predicate => {
var result = [];
for(var element in this)
if( predicate(element) )
result.push(element);
return result;
};
Now use 'where' to find number less than 10:
var arr = [1, 2, 5, 7, 8, 10, 12, 15, 17, 18, 19];
var result = arr.where( n => n > 10 );
The result is:
[12, 15, 17, 18, 19]