Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lbvf50mobile/a5bf38d9d6551aacbe2d503591c987ef to your computer and use it in GitHub Desktop.
Save lbvf50mobile/a5bf38d9d6551aacbe2d503591c987ef to your computer and use it in GitHub Desktop.
javascript#each_cons
// https://developer.mozilla.org/ja/New_in_JavaScript_1.7#Array_comprehensions
function range (begin, end) {
for (let i = begin; i < end; ++i) {
yield i;
}
};
// http://code.activestate.com/recipes/347689-ruby-arrayeach_cons-each_slice-overlapping-and-non/
function each_cons (l, n) {
return [l.slice(i, i + n) for (i in (range(0, l.length - n + 1)))]
};
each_cons([1,2,3,4,5,6,7], 2); // => [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment