-
-
Save robotlolita/3172348 to your computer and use it in GitHub Desktop.
Creating n sized lists
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 last(xs){ return xs[xs.length - 1] } | |
function split(predicate, xs) { | |
return reduce( xs | |
, function(rv, x, i) { return predicate(x, i)? rv.concat([[x]]) | |
: /* otherwise */ (last(rv).push(x), rv) } | |
, [[ ]]) } | |
function splitEvery(n, xs) { return split(function(x, i){ return i > 0 && i % n == 0 }, xs) } |
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
// reduce is Array.prototype.reduce.call.bind(Array.prototype.reduce) | |
reduce(list, intoTriplet, []) | |
reduce(list, intoTuple, []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment