Skip to content

Commit

Permalink
Get rid of for-of loops in fractions-common, see phetsims/tasks#972
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Dec 11, 2018
1 parent f31d4e7 commit 20ddcc3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions js/common/EnumerationMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ define( require => {
// @private {Enumeration}
this._enumeration = enumeration;

for ( let entry of enumeration.VALUES ) {
enumeration.VALUES.forEach( entry => {
assert && assert( this[ entry ] === undefined, 'Enumeration key override problem' );
this[ entry ] = factory( entry );
}
} );
}

/**
Expand Down
11 changes: 7 additions & 4 deletions js/common/model/PrimeFactorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ define( require => {
binaryOperation( factorization, operation ) {
const primes = _.uniq( [ ...this.factors, ...factorization.factors ].map( f => f.prime ) ).sort( ( a, b ) => a - b );
const factors = [];
for ( let prime of primes ) {
primes.forEach( prime => {
const order = operation( this.getOrder( prime ), factorization.getOrder( prime ) );
if ( order ) {
factors.push( new PrimeFactor( prime, order ) );
}
}
} );
return new PrimeFactorization( factors );
}

Expand Down Expand Up @@ -164,7 +164,8 @@ define( require => {
* @returns {boolean}
*/
divides( factorization ) {
for ( let factor of this.factors ) {
for ( let i = 0; i < this.factors.length; i++ ) {
const factor = this.factors[ i ];
if ( factor.order > factorization.getOrder( factor.prime ) ) {
return false;
}
Expand Down Expand Up @@ -236,7 +237,9 @@ define( require => {

const factors = [];

for ( let prime of Primes.primes ) {
const primes = Primes.primes;
for ( let i = 0; i < primes.length; i++ ) {
const prime = primes[ i ];
// A prime that is a divisor (not equal to our number) would be less than the max divisor
if ( prime > maxDivisor ) { break; }

Expand Down
8 changes: 4 additions & 4 deletions js/common/model/PrimeFactorizationTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ define( require => {
QUnit.test( `Prime factorizations of everything up to ${MAX_NUMBER}`, assert => {
for ( let n = 2; n < MAX_NUMBER; n++ ) {
const factorization = PrimeFactorization.factor( n );
for ( let factor of factorization.factors ) {
factorization.factors.forEach( factor => {
assert.ok( Primes.isPrime( factor.prime ), `Prime of factorization of ${n} is prime` );
}
} );
assert.equal( n, factorization.number, `Prime factorization of ${n} is consistent` );
}
} );
Expand Down Expand Up @@ -62,9 +62,9 @@ define( require => {
const factorization = PrimeFactorization.factor( a );
const divisors = factorization.divisors;

for ( let divisor of divisors ) {
divisors.forEach( divisor => {
assert.equal( a / divisor.number, factorization.divided( divisor ).number, `Division of ${a} divided by ${divisor.number}` );
}
} );
}
} );

Expand Down
11 changes: 6 additions & 5 deletions js/game/model/CollectionFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ define( require => {
const constraintIndex = findMinConstraintIndex();
const divisorDenominators = constraintDenominators[ constraintIndex ];

for ( let uncomputedDenominator of filterUncomputedDenominators( divisorDenominators ) ) {
filterUncomputedDenominators( divisorDenominators ).forEach( uncomputedDenominator => {
entries.push( new Entry( uncomputedDenominator, lcm.divided( uncomputedDenominator ) ) );
}
} );
for ( let i = 0; i < constraintDivisors.length; i++ ) {
const divisor = constraintDivisors[ i ];
const divisorDenominators = constraintDenominators[ i ];
Expand All @@ -95,9 +95,9 @@ define( require => {
constraintDivisors.splice( constraintIndex, 1 );
constraintDenominators.splice( constraintIndex, 1 );
}
for ( let uncomputedDenominator of filterUncomputedDenominators( denominators ) ) {
filterUncomputedDenominators( denominators ).forEach( uncomputedDenominator => {
entries.push( new Entry( uncomputedDenominator, lcm.divided( uncomputedDenominator ) ) );
}
} );

// @private {Array.<Entry>}
this.entries = entries;
Expand Down Expand Up @@ -182,7 +182,8 @@ define( require => {
coefficients.push( coefficient );

let constraintsSatisfied = true;
for ( let constraint of entry.constraints ) {
for ( let i = 0; i < entry.constraints.length; i++ ) {
const constraint = entry.constraints[ i ];
if ( !constraint.satisfies( r, coefficients ) ) {
constraintsSatisfied = false;
break;
Expand Down
6 changes: 4 additions & 2 deletions js/game/model/FractionChallenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ define( require => {
// If we already have one out, don't look for more
if ( groupArray.length >= 2 ) { return; }

for ( let groupStack of stackArray ) {
for ( let i = 0; i < stackArray.length; i++ ) {
const groupStack = stackArray[ i ];
if ( !groupStack.isEmpty() ) {
const group = groupStack.array.pop();
group.clear();
Expand Down Expand Up @@ -560,7 +561,8 @@ define( require => {

const possibilities = fractionPossibilities[ i ];

for ( let possibility of possibilities ) {
for ( let i = 0; i < possibilities.length; i++ ) {
const possibility = possibilities[ i ];
if ( currentCollection.contains( possibility.total ) ) {
currentCollection = currentCollection.minus( possibility.total );

Expand Down

0 comments on commit 20ddcc3

Please sign in to comment.