Skip to content

Instantly share code, notes, and snippets.

@mfandl
Last active March 2, 2017 15:31
Show Gist options
  • Save mfandl/45528a93fa58feb254db65792c75c0a6 to your computer and use it in GitHub Desktop.
Save mfandl/45528a93fa58feb254db65792c75c0a6 to your computer and use it in GitHub Desktop.
recursiveInterpolation in javascript
function interpolatedPairs (array) {
return array.slice(0, array.length - 1).map((v, i) => [v, interpolate(v, array[i + 1])])
}
function interpolate(a, b) {
return (a + b) / 2
}
function flatten (arr) {
return arr.reduce((acc, v) => acc.concat(v), [])
}
function interpolatedArray (array) {
return flatten(interpolatedPairs(array)).concat(array.slice(-1))
}
function recursiveInterpolation (arr, i) {
return i === 1 ? interpolatedArray(arr) : recursiveInterpolation(interpolatedArray(arr), i - 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment