Last active
March 2, 2017 15:31
-
-
Save mfandl/45528a93fa58feb254db65792c75c0a6 to your computer and use it in GitHub Desktop.
recursiveInterpolation in javascript
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
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