Skip to content

Instantly share code, notes, and snippets.

@thauber
Last active May 19, 2020 21:20
Show Gist options
  • Save thauber/f3644d5c4e4c5145a655088676cf2451 to your computer and use it in GitHub Desktop.
Save thauber/f3644d5c4e4c5145a655088676cf2451 to your computer and use it in GitHub Desktop.
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