From aa1a2e74cfce13e543793b264c7d911c43626e32 Mon Sep 17 00:00:00 2001 From: David Majda Date: Thu, 1 Sep 2016 13:55:41 +0200 Subject: [PATCH] Replace suitable for loops with Array methods (in /examples) See #441. --- examples/arithmetics.pegjs | 24 +++++++-------------- examples/css.pegjs | 26 +++++++---------------- examples/javascript.pegjs | 43 ++++++++++---------------------------- examples/json.pegjs | 10 ++++----- 4 files changed, 31 insertions(+), 72 deletions(-) diff --git a/examples/arithmetics.pegjs b/examples/arithmetics.pegjs index e7bdc44..5cd36cf 100644 --- a/examples/arithmetics.pegjs +++ b/examples/arithmetics.pegjs @@ -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 diff --git a/examples/css.pegjs b/examples/css.pegjs index 60e26d0..0a6895d 100644 --- a/examples/css.pegjs +++ b/examples/css.pegjs @@ -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); } } diff --git a/examples/javascript.pegjs b/examples/javascript.pegjs index e39babe..3ca4fc3 100644 --- a/examples/javascript.pegjs +++ b/examples/javascript.pegjs @@ -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 diff --git a/examples/json.pegjs b/examples/json.pegjs index d0ba9f4..e0afbbf 100644 --- a/examples/json.pegjs +++ b/examples/json.pegjs @@ -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; }