Skip to content

Instantly share code, notes, and snippets.

@SeeThruHead
Last active August 29, 2015 14:26
Show Gist options
  • Save SeeThruHead/e60613535dab860e3eca to your computer and use it in GitHub Desktop.
Save SeeThruHead/e60613535dab860e3eca to your computer and use it in GitHub Desktop.
function biggestGap(values) {
function diff(curr, before) {
return curr - Math.min.apply(null, before);
}
function diffs(values) {
return values.map(function(x, i, ar) {
return diff(x, ar.slice(0, i));
});
}
return Math.max.apply(null, diffs(values));
}
// OR Thanks David Chambers
// max :: [a] -> a
const max = Function.apply.bind(Math.max, Math);
// biggestGap :: [Number] -> Number
const biggestGap = xs => max(xs.map((x, idx, xs) => max(xs.slice(idx)) - x));
@SeeThruHead
Copy link
Author

Finds the biggest gap between elements in an array. Where the smaller element must be in front of the larger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment