-
-
Save thauber/f3644d5c4e4c5145a655088676cf2451 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require('underscore'); | |
const machineData = [1,2,3,2,3,2,3,4,5,5,5,6,5,4,4,3,1]; | |
const info = _.reduce(machineData, (memo, current, index) => { | |
const { last, lastSlope, minIndexes, maxIndexes } = memo | |
const slope = last ? current-last : 0; | |
if (slope != 0 && lastSlope && Math.sign(slope) != Math.sign(lastSlope)) { | |
if (slope > 0) { | |
minIndexes.push(index-1); | |
} else { | |
maxIndexes.push(index-1); | |
} | |
} | |
memo.lastSlope = slope == 0 ? memo.lastSlope : slope; | |
memo.last = current; | |
return memo; | |
}, {lastSlope: null, minIndexes:[], maxIndexes:[], last:null}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment