Replace suitable for loops with Array methods (in /examples)

See #441.
redux
David Majda 8 years ago
parent 9fbb45a2c9
commit aa1a2e74cf

@ -7,26 +7,18 @@
Expression
= head:Term tail:(_ ("+" / "-") _ Term)* {
var result = head, i;
for (i = 0; i < tail.length; i++) {
if (tail[i][1] === "+") { result += tail[i][3]; }
if (tail[i][1] === "-") { result -= tail[i][3]; }
}
return result;
return tail.reduce(function(result, element) {
if (element[1] === "+") { return result + element[3]; }
if (element[1] === "-") { return result - element[3]; }
}, head);
}
Term
= head:Factor tail:(_ ("*" / "/") _ Factor)* {
var result = head, i;
for (i = 0; i < tail.length; i++) {
if (tail[i][1] === "*") { result *= tail[i][3]; }
if (tail[i][1] === "/") { result /= tail[i][3]; }
}
return result;
return tail.reduce(function(result, element) {
if (element[1] === "*") { return result * element[3]; }
if (element[1] === "/") { return result / element[3]; }
}, head);
}
Factor

@ -25,15 +25,9 @@
}
function extractList(list, index) {
var result = [], i;
for (i = 0; i < list.length; i++) {
if (list[i][index] !== null) {
result.push(list[i][index]);
}
}
return result;
return list
.map(function(element) { return element[index]; })
.filter(function(element) { return element !== null; });
}
function buildList(head, tail, index) {
@ -41,18 +35,14 @@
}
function buildExpression(head, tail) {
var result = head, i;
for (i = 0; i < tail.length; i++) {
result = {
return tail.reduce(function(result, element) {
return {
type: "Expression",
operator: tail[i][0],
operator: element[0],
left: result,
right: tail[i][1]
right: element[1]
};
}
return result;
}, head);
}
}

@ -39,13 +39,8 @@
};
function filledArray(count, value) {
var result = new Array(count), i;
for (i = 0; i < count; i++) {
result[i] = value;
}
return result;
return Array.apply(null, new Array(count))
.map(function() { return value; });
}
function extractOptional(optional, index) {
@ -53,49 +48,33 @@
}
function extractList(list, index) {
var result = new Array(list.length), i;
for (i = 0; i < list.length; i++) {
result[i] = list[i][index];
}
return result;
return list.map(function(element) { return element[index]; });
}
function buildList(head, tail, index) {
return [head].concat(extractList(tail, index));
}
function buildTree(head, tail, builder) {
var result = head, i;
for (i = 0; i < tail.length; i++) {
result = builder(result, tail[i]);
}
return result;
}
function buildBinaryExpression(head, tail) {
return buildTree(head, tail, function(result, element) {
return tail.reduce(function(result, element) {
return {
type: "BinaryExpression",
operator: element[1],
left: result,
right: element[3]
};
});
}, head);
}
function buildLogicalExpression(head, tail) {
return buildTree(head, tail, function(result, element) {
return tail.reduce(function(result, element) {
return {
type: "LogicalExpression",
operator: element[1],
left: result,
right: element[3]
};
});
}, head);
}
function optionalList(value) {
@ -639,14 +618,14 @@ MemberExpression
}
)*
{
return buildTree(head, tail, function(result, element) {
return tail.reduce(function(result, element) {
return {
type: "MemberExpression",
object: result,
property: element.property,
computed: element.computed
};
});
}, head);
}
NewExpression
@ -681,11 +660,11 @@ CallExpression
}
)*
{
return buildTree(head, tail, function(result, element) {
return tail.reduce(function(result, element) {
element[TYPES_TO_PROPERTY_NAMES[element.type]] = result;
return element;
});
}, head);
}
Arguments

@ -52,13 +52,11 @@ object
head:member
tail:(value_separator m:member { return m; })*
{
var result = {}, i;
var result = {};
result[head.name] = head.value;
for (i = 0; i < tail.length; i++) {
result[tail[i].name] = tail[i].value;
}
[head].concat(tail).forEach(function(element) {
result[element.name] = element.value;
});
return result;
}

Loading…
Cancel
Save