Refactoring JavaScript: Turning Bad Code Into Good Code by Burchard Evan

Refactoring JavaScript: Turning Bad Code Into Good Code by Burchard Evan

Author:Burchard, Evan [Burchard, Evan]
Language: eng
Format: azw3
Publisher: O'Reilly Media
Published: 2017-03-13T04:00:00+00:00


classify: function(chords){ const classified = new Map(); Array.from(this.labelProbabilities.entries()).map( (labelWithProbability) => { const totalLikelihood = chords.reduce((total, chord) => { const probabilityOfChordInLabel = this.probabilityOfChordsInLabels.get(labelWithProbability[0])[chord]; if(probabilityOfChordInLabel){ return total * (probabilityOfChordInLabel + this.smoothing); }else{ return total; } }, this.labelProbabilities.get(labelWithProbability[0]) + this.smoothing); classified.set(labelWithProbability[0], totalLikelihood); }); return classified; }

Unfortunately, the Map object doesn’t have a map function (yes, it bums me out, too.) So our first step involves pulling an array out of the entries in the Map so that we can use map through it. The second change is that we end up with a slightly less convenient object (labelWithProbability instead of difficulty), which requires us to take the first index of [0]. In other words, difficulty is replaced with labelWithProbability[0]. But we can initialize difficulty as a new const:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.