Skip to content

Instantly share code, notes, and snippets.

@tuscen
Created February 3, 2016 20:18
Show Gist options
  • Select an option

  • Save tuscen/b33d7bd0ee9eeec1284c to your computer and use it in GitHub Desktop.

Select an option

Save tuscen/b33d7bd0ee9eeec1284c to your computer and use it in GitHub Desktop.
Clojure's partition for arrays in Javascript
function partition(coll, size, step, pad) {
if (size < 1) {
return coll;
}
function iter(acc, coll) {
if (!coll.length) {
return acc;
}
if (coll.length < size) {
return pad ? iter(acc.concat([coll]), []) : iter(acc, []);
}
const part = coll.slice(0, size);
return iter(acc.concat([part]), coll.slice(step));
}
return iter([], coll);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment