Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
removed utils/clone, added .cloneDeep() where clone() was used (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
abeledovictor authored and evykassirer committed May 15, 2018
1 parent e5a69b7 commit 8a8f26b
Show file tree
Hide file tree
Showing 29 changed files with 73 additions and 126 deletions.
5 changes: 2 additions & 3 deletions lib/equation/Equation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const math = require('mathjs');

const clone = require('../util/clone');
const printNode = require('../util/print');

// This represents an equation, made up of the leftNode (LHS), the
Expand Down Expand Up @@ -31,8 +30,8 @@ class Equation {
}

clone() {
const newLeft = clone(this.leftNode);
const newRight = clone(this.rightNode);
const newLeft = this.leftNode.cloneDeep();
const newRight = this.rightNode.cloneDeep();
return new Equation(newLeft, newRight, this.comparator);
}
}
Expand Down
20 changes: 9 additions & 11 deletions lib/node/Status.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const clone = require('../util/clone');

const ChangeTypes = require('../ChangeTypes');
const Type = require('./Type');

Expand Down Expand Up @@ -29,7 +27,7 @@ class Status {
}

Status.resetChangeGroups = function(node) {
node = clone(node);
node = node.cloneDeep();
node.filter(node => node.changeGroup).forEach(change => {
delete change.changeGroup;
});
Expand Down Expand Up @@ -60,8 +58,8 @@ Status.nodeChanged = function(
// updated to have the newNode/oldNode metadata (changeGroups)
// e.g. (2 + 2) + x --> 4 + x has to update the left argument
Status.childChanged = function(node, childStatus, childArgIndex=null) {
const oldNode = clone(node);
const newNode = clone(node);
const oldNode = node.cloneDeep();
const newNode = node.cloneDeep();
let substeps = childStatus.substeps;

if (!childStatus.oldNode) {
Expand All @@ -80,8 +78,8 @@ Status.childChanged = function(node, childStatus, childArgIndex=null) {
oldNode.content = childStatus.oldNode;
newNode.content = childStatus.newNode;
substeps = updateSubsteps(substeps, (step) => {
const oldNode = clone(node);
const newNode = clone(node);
const oldNode = node.cloneDeep();
const newNode = node.cloneDeep();
oldNode.content = step.oldNode;
newNode.content = step.newNode;
step.oldNode = oldNode;
Expand All @@ -94,8 +92,8 @@ Status.childChanged = function(node, childStatus, childArgIndex=null) {
oldNode.args[childArgIndex] = childStatus.oldNode;
newNode.args[childArgIndex] = childStatus.newNode;
substeps = updateSubsteps(substeps, (step) => {
const oldNode = clone(node);
const newNode = clone(node);
const oldNode = node.cloneDeep();
const newNode = node.cloneDeep();
oldNode.args[childArgIndex] = step.oldNode;
newNode.args[childArgIndex] = step.newNode;
step.oldNode = oldNode;
Expand All @@ -107,8 +105,8 @@ Status.childChanged = function(node, childStatus, childArgIndex=null) {
oldNode.args[0] = childStatus.oldNode;
newNode.args[0] = childStatus.newNode;
substeps = updateSubsteps(substeps, (step) => {
const oldNode = clone(node);
const newNode = clone(node);
const oldNode = node.cloneDeep();
const newNode = node.cloneDeep();
oldNode.args[0] = step.oldNode;
newNode.args[0] = step.newNode;
step.oldNode = oldNode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const clone = require('../../util/clone');

const ChangeTypes = require('../../ChangeTypes');
const Node = require('../../node');

Expand All @@ -12,7 +10,7 @@ function convertMixedNumberToImproperFraction(node) {
}

const substeps = [];
let newNode = clone(node);
let newNode = node.cloneDeep();

// e.g. 1 2/3
const wholeNumber = Node.MixedNumber.getWholeNumberValue(node); // 1
Expand Down
3 changes: 1 addition & 2 deletions lib/simplifyExpression/basicsSearch/rearrangeCoefficient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const checks = require('../../checks');
const clone = require('../../util/clone');

const ChangeTypes = require('../../ChangeTypes');
const Node = require('../../node');
Expand All @@ -12,7 +11,7 @@ function rearrangeCoefficient(node) {
return Node.Status.noChange(node);
}

let newNode = clone(node);
let newNode = node.cloneDeep();

const polyNode = new Node.PolynomialTerm(newNode.args[0]);
const constNode = newNode.args[1];
Expand Down
4 changes: 1 addition & 3 deletions lib/simplifyExpression/basicsSearch/removeAdditionOfZero.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const clone = require('../../util/clone');

const ChangeTypes = require('../../ChangeTypes');
const Node = require('../../node');

Expand All @@ -12,7 +10,7 @@ function removeAdditionOfZero(node) {
const zeroIndex = node.args.findIndex(arg => {
return Node.Type.isConstant(arg) && arg.value === '0';
});
let newNode = clone(node);
let newNode = node.cloneDeep();
if (zeroIndex >= 0) {
// remove the 0 node
newNode.args.splice(zeroIndex, 1);
Expand Down
6 changes: 3 additions & 3 deletions lib/simplifyExpression/basicsSearch/removeDivisionByOne.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const clone = require('../../util/clone');

const ChangeTypes = require('../../ChangeTypes');
const Negative = require('../../Negative');
const Node = require('../../node');
Expand All @@ -14,7 +12,9 @@ function removeDivisionByOne(node) {
if (!Node.Type.isConstant(denominator)) {
return Node.Status.noChange(node);
}
let numerator = clone(node.args[0]);
// It's taken 40ms on average to pass distribution test,
// TODO: see if we should keep using utils/clone here
let numerator = node.args[0].cloneDeep();

// if denominator is -1, we make the numerator negative
if (parseFloat(denominator.value) === -1) {
Expand Down
3 changes: 1 addition & 2 deletions lib/simplifyExpression/basicsSearch/removeExponentBaseOne.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const checks = require('../../checks');
const clone = require('../../util/clone');

const ChangeTypes = require('../../ChangeTypes');
const Node = require('../../node');
Expand All @@ -11,7 +10,7 @@ function removeExponentBaseOne(node) {
checks.resolvesToConstant(node.args[1]) && // a power not a symbol and
Node.Type.isConstant(node.args[0]) && // a constant base
node.args[0].value === '1') { // of value 1
const newNode = clone(node.args[0]);
const newNode = node.args[0].cloneDeep();
return Node.Status.nodeChanged(
ChangeTypes.REMOVE_EXPONENT_BASE_ONE, node, newNode);
}
Expand Down
4 changes: 1 addition & 3 deletions lib/simplifyExpression/basicsSearch/removeExponentByOne.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const clone = require('../../util/clone');

const ChangeTypes = require('../../ChangeTypes');
const Node = require('../../node');

Expand All @@ -9,7 +7,7 @@ function removeExponentByOne(node) {
if (node.op === '^' && // exponent of anything
Node.Type.isConstant(node.args[1]) && // to a constant
node.args[1].value === '1') { // of value 1
const newNode = clone(node.args[0]);
const newNode = node.args[0].cloneDeep();
return Node.Status.nodeChanged(
ChangeTypes.REMOVE_EXPONENT_BY_ONE, node, newNode);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const clone = require('../../util/clone');

const ChangeTypes = require('../../ChangeTypes');
const Negative = require('../../Negative');
const Node = require('../../node');
Expand Down Expand Up @@ -36,10 +34,10 @@ function removeMultiplicationByNegativeOne(node) {
return Node.Status.noChange(node);
}

let newNode = clone(node);
let newNode = node.cloneDeep();

// Get rid of the -1
nodeToCombine = Negative.negate(clone(nodeToCombine));
nodeToCombine = Negative.negate(nodeToCombine.cloneDeep());

// replace the node next to -1 and remove -1
newNode.args[nodeToCombineIndex] = nodeToCombine;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const clone = require('../../util/clone');

const ChangeTypes = require('../../ChangeTypes');
const Node = require('../../node');

Expand All @@ -13,7 +11,7 @@ function removeMultiplicationByOne(node) {
return Node.Type.isConstant(arg) && arg.value === '1';
});
if (oneIndex >= 0) {
let newNode = clone(node);
let newNode = node.cloneDeep();
// remove the 1 node
newNode.args.splice(oneIndex, 1);
// if there's only one operand left, there's nothing left to multiply it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const clone = require('../../util/clone');

const ChangeTypes = require('../../ChangeTypes');
const Node = require('../../node');

Expand All @@ -12,7 +10,7 @@ function simplifyDoubleUnaryMinus(node) {
const unaryArg = node.args[0];
// e.g. in - -x, -x is the unary arg, and we'd want to reduce to just x
if (Node.Type.isUnaryMinus(unaryArg)) {
const newNode = clone(unaryArg.args[0]);
const newNode = unaryArg.args[0].cloneDeep();
return Node.Status.nodeChanged(
ChangeTypes.RESOLVE_DOUBLE_MINUS, node, newNode);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const clone = require('../../util/clone');
const print = require('../../util/print');

const ChangeTypes = require('../../ChangeTypes');
Expand Down Expand Up @@ -91,14 +90,14 @@ LikeTermCollector.collectLikeTerms = function(node) {
termTypesSorted.forEach(termType => {
const termsOfType = terms[termType];
if (termsOfType.length === 1) {
const singleTerm = clone(termsOfType[0]);
const singleTerm = termsOfType[0].cloneDeep();
singleTerm.changeGroup = changeGroup;
newOperands.push(singleTerm);
}
// Any like terms should be wrapped in parens.
else {
const termList = clone(Node.Creator.parenthesis(
Node.Creator.operator(op, termsOfType)));
const termList = Node.Creator.parenthesis(
Node.Creator.operator(op, termsOfType)).cloneDeep();
termList.changeGroup = changeGroup;
newOperands.push(termList);
}
Expand All @@ -113,7 +112,7 @@ LikeTermCollector.collectLikeTerms = function(node) {
newOperands = newOperands.concat(terms[OTHER]);
}

const newNode = clone(node);
const newNode = node.cloneDeep();
newNode.args = newOperands;
return Node.Status.nodeChanged(
ChangeTypes.COLLECT_LIKE_TERMS, node, newNode, false);
Expand Down
11 changes: 5 additions & 6 deletions lib/simplifyExpression/collectAndCombineSearch/addLikeTerms.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const checks = require('../../checks');
const clone = require('../../util/clone');
const evaluateConstantSum = require('./evaluateConstantSum');

const ChangeTypes = require('../../ChangeTypes');
Expand Down Expand Up @@ -56,7 +55,7 @@ function addLikeNthRootTerms(node) {
// belonging to a subclass of Term
function addLikeTermNodes(node, termSubclass, changeType) {
const substeps = [];
let newNode = clone(node);
let newNode = node.cloneDeep();

// STEP 1: If any nodes have no coefficient, make it have coefficient 1
// (this step only happens under certain conditions and later steps might
Expand Down Expand Up @@ -96,7 +95,7 @@ function addLikeTermNodes(node, termSubclass, changeType) {
// e.g. 2x + x -> 2x + 1x
// Returns a Node.Status object.
function addPositiveOneCoefficient(node, termSubclass) {
const newNode = clone(node, false);
const newNode = node.cloneDeep();
let change = false;

let changeGroup = 1;
Expand Down Expand Up @@ -132,7 +131,7 @@ function addPositiveOneCoefficient(node, termSubclass) {
// e.g. 2x - x -> 2x - 1x
// Returns a Node.Status object.
function addNegativeOneCoefficient(node, termSubclass) {
const newNode = clone(node);
const newNode = node.cloneDeep();
let change = false;

let changeGroup = 1;
Expand Down Expand Up @@ -166,7 +165,7 @@ function addNegativeOneCoefficient(node, termSubclass) {
// e.g. 2x^2 + 3x^2 + 5x^2 -> (2+3+5)x^2
// Returns a Node.Status object.
function groupCoefficientsForAdding(node, termSubclass) {
let newNode = clone(node);
let newNode = node.cloneDeep();

const termList = newNode.args.map(n => new termSubclass(n));
const coefficientList = termList.map(term => term.getCoeffNode(true));
Expand Down Expand Up @@ -196,7 +195,7 @@ function evaluateCoefficientSum(node) {
// the node is now always a * node with the left child the coefficent sum
// e.g. (2 + 4 + 5) and the right node the symbol part e.g. x or y^2
// so we want to evaluate args[0]
const coefficientSum = clone(node).args[0];
const coefficientSum = node.cloneDeep().args[0];
const childStatus = evaluateConstantSum(coefficientSum);
return Node.Status.childChanged(node, childStatus, 0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const addConstantAndFraction = require('../fractionsSearch/addConstantAndFraction');
const addConstantFractions = require('../fractionsSearch/addConstantFractions');
const arithmeticSearch = require('../arithmeticSearch');
const clone = require('../../util/clone');

const ChangeTypes = require('../../ChangeTypes');
const Node = require('../../node');
Expand Down Expand Up @@ -35,7 +34,7 @@ function evaluateConstantSum(node) {
}
}

let newNode = clone(node);
let newNode = node.cloneDeep();
const substeps = [];
let status;

Expand Down Expand Up @@ -100,7 +99,7 @@ function groupConstantsAndFractions(node) {
// set the changeGroup - this affects both the old and new node
node.changeGroup = 1;
// clone so that node and newNode aren't stored in the same memory
return clone(node);
return node.cloneDeep();
});
// wrap in parenthesis if there's more than one, to group them
if (constants.length > 1) {
Expand All @@ -114,7 +113,7 @@ function groupConstantsAndFractions(node) {
// set the changeGroup - this affects both the old and new node
node.changeGroup = 2;
// clone so that node and newNode aren't stored in the same memory
return clone(node);
return node.cloneDeep();
});
// wrap in parenthesis if there's more than one, to group them
if (fractions.length > 1) {
Expand Down
5 changes: 2 additions & 3 deletions lib/simplifyExpression/collectAndCombineSearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const addLikeTerms = require('./addLikeTerms');
const checks = require('../../checks');
const clone = require('../../util/clone');
const multiplyLikeTerms = require('./multiplyLikeTerms');

const ChangeTypes = require('../../ChangeTypes');
Expand Down Expand Up @@ -58,7 +57,7 @@ function collectAndCombineLikeTerms(node) {
function collectAndCombineOperation(node) {
let substeps = [];

const status = LikeTermCollector.collectLikeTerms(clone(node));
const status = LikeTermCollector.collectLikeTerms(node.cloneDeep());
if (!status.hasChanged()) {
return status;
}
Expand Down Expand Up @@ -87,7 +86,7 @@ function collectAndCombineOperation(node) {
// returns a list of combine steps
function combineLikeTerms(node) {
const steps = [];
let newNode = clone(node);
let newNode = node.cloneDeep();

for (let i = 0; i < node.args.length; i++) {
let child = node.args[i];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const arithmeticSearch = require('../arithmeticSearch');
const checks = require('../../checks');
const clone = require('../../util/clone');
const ConstantOrConstantPower = require('./ConstantOrConstantPower');
const multiplyFractionsSearch = require('../multiplyFractionsSearch');

Expand Down Expand Up @@ -51,7 +50,7 @@ function multiplyNthRoots(node) {
return Node.Status.noChange(node);
}

let newNode = clone(node);
let newNode = node.cloneDeep();

// Array of radicands of all the nthRoot terms being multiplied
const radicands = node.args.map(term => NthRoot.getRadicandNode(term));
Expand All @@ -76,7 +75,7 @@ function multiplyPolynomialTerms(node) {
}

const substeps = [];
let newNode = clone(node);
let newNode = node.cloneDeep();

// STEP 1: If any term has no exponent, make it have exponent 1
// e.g. x -> x^1 (this is for pedagogy reasons)
Expand Down Expand Up @@ -131,7 +130,7 @@ function multiplyPolynomialTerms(node) {
// e.g. 10^2 * 10 -> 10^2 * 10^1
// Returns a Node.Status object.
function addOneExponent(node) {
const newNode = clone(node);
const newNode = node.cloneDeep();
let change = false;

let changeGroup = 1;
Expand Down
Loading

0 comments on commit 8a8f26b

Please sign in to comment.