Created
February 3, 2016 20:18
-
-
Save tuscen/b33d7bd0ee9eeec1284c to your computer and use it in GitHub Desktop.
Clojure's partition for arrays in Javascript
This file contains hidden or 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 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